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

3

7

I am using mvc.net with StructureMap to scan and register all repositories and services for me. Now I want to register and cache by Singleton. How can I do?

 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", ""));
            });
        });
Sorilda answered 10/6, 2009 at 20:4 Comment(0)
S
19

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);
        }
    }
Schaefer answered 5/2, 2010 at 18:2 Comment(3)
+1 BTW, no need to call a.AddAllTypesOf<IMyPluginType>(); in Scan() as the SingletonConvention.Process() will reg the type(s).Basso
Why do I need those 3 lines in Scan? What's the difference between a.With(new SingletonConvention<IMyPluginType>()); and a.AddAllTypesOf<IMyPluginType>();? Are those two really necessary? And if I have other interface, do I need to use both lines again?Oilskin
Overly verbose, but worked like a champ. Really think that if statement should need used, but i'm not going to change the API. After saying that i won't change the api, it still doesn't mean i can't just do betterRhea
W
2

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.

Whiles answered 30/6, 2009 at 15:10 Comment(0)
R
0

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.

Rhea answered 10/3, 2014 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.