如何在Singleton中将Structuremap配置为Assembly and Cache中的自动扫描类型?

ens*_*coz 7 structuremap singleton

我正在使用带有StructureMap的mvc.net来扫描和注册所有存储库和服务.现在我想通过Singleton注册和缓存.我能怎么做?

 IContainer container = new Container(x => {
            // Register Repositories and Services
            x.Scan(y => {
                y.AssemblyContainingType<SomeRepository>();
                y.AssemblyContainingType<SomeService>();

                y.IncludeNamespaceContainingType<SomeRepository>();
                y.IncludeNamespaceContainingType<SomeService>();
            });   

            // Register Controllers
            x.Scan(y => {
                y.TheCallingAssembly();
                y.AddAllTypesOf<IController>().NameBy(type => type.Name.Replace("Controller", ""));
            });
        });
Run Code Online (Sandbox Code Playgroud)

Eri*_*ser 19

使用2.6中的新API,不推荐使用ITypeScanner.这应该作为惯例来实现.一个简单的例子是你想要注册一个约定,所有类型的特定接口都是一个单例:

    Scan(a =>
    {
        a.AssemblyContainingType<IMyPluginType>();
        a.With(new SingletonConvention<IMyPluginType>());
        a.AddAllTypesOf<IMyPluginType>();
    });
Run Code Online (Sandbox Code Playgroud)

然后:

    internal class SingletonConvention<TPluginFamily> : IRegistrationConvention
    {
        public void Process(Type type, Registry registry)
        {
            if (!type.IsConcrete() || !type.CanBeCreated() || !type.AllInterfaces().Contains(typeof(TPluginFamily))) return;

            registry.For(typeof(TPluginFamily)).Singleton().Use(type);
        }
    }
Run Code Online (Sandbox Code Playgroud)