7

我正在使用 mvc.net 和 StructureMap 为我扫描和注册所有存储库和服务。现在我想通过 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", ""));
            });
        });
4

3 回答 3

19

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

    Scan(a =>
    {
        a.AssemblyContainingType<IMyPluginType>();
        a.With(new SingletonConvention<IMyPluginType>());
        a.AddAllTypesOf<IMyPluginType>();
    });

然后:

    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);
        }
    }
于 2010-02-05T18:02:36.727 回答
2

您需要实现类似于 Jeremy Miller 在http://codebetter.com/blogs/jeremy.miller/archive/2009/01/20/create-your-own-auto-registration-convention-with中概述的 ITypeScanner -结构图.aspx

因此,例如,对于您的控制器,我会将 Scan 调用更改为:

x.Scan(y => {
    y.TheCallingAssembly();
    y.With<MyNewTypeScanner>();
});

然后我会在其他地方定义一个看起来像这样的类:

public class MyNewTypeScanner: ITypeScanner
{
    //This method is responsible for determining if a type should be registered
    // and then passing it to RegisterType() if so
    public void Process(Type type, PluginGraph graph)
    {
        //At this point you would probably just test to see if type is IController
        //but you can get more sophisticated without too much headache.

        this.RegisterType(graph, type);
    }


    private void RegisterType(PluginGraph graph, Type implementingType)
    {
        //The argument passed to FindFamily must be the type/class that will be requested out of SM
        var family = graph.FindFamily(implementingType);

        family.AddType(implementingType);
        family.SetScopeTo(InstanceScope.Singleton);
    }
}

这应该为您解决问题。

于 2009-06-30T15:10:17.197 回答
0

扩展@Eric Hauser 的答案,创建更易于使用的解决方案

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

        ApplyConvention(type, registry);
    }

    public abstract void ApplyConvention(Type type, Registry registry);
}

使用这个已建立的基类一次,您就可以实现约定,而不必乱搞类型检查代码。

public class SingletonConvention<TPluginFamily> 
                                : TypedRegistrationConvention<TPluginFamily>
{
    public override void ApplyConvention(Type type, Registry registry)
    {
        registry.For(typeof (TPluginFamily)).Singleton().Use(type);
    }
}

最后更简单的类。

于 2014-03-10T17:59:39.963 回答