How can I configure Structuremap to auto scan type in Assembly and Cache by Singleton?

ⅰ亾dé卋堺 提交于 2019-12-04 05:47:55

Using the new API in 2.6, ITypeScanner is deprecated. This should be implemented as a convention instead. A simple example is you want to register a convention that all types of a particular interface are a singleton:

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

Then:

    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);
        }
    }

You'll need to implement the ITypeScanner similar to what Jeremy Miller outlines at http://codebetter.com/blogs/jeremy.miller/archive/2009/01/20/create-your-own-auto-registration-convention-with-structuremap.aspx

So for your Controllers for instance, I would change that Scan call to be:

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

Then I would define a class elsewhere that looked something like this:

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);
    }
}

This should do the trick for you.

Expanding upon the answer from @Eric Hauser creating a more readily usable solution

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);
}

With this established base class once, you can then implement conventions without having to muck around with the type checking code.

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

Much simpler class in the end.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!