How to register common interfaces in StructureMap

How to register all instances of the universal interface in Structured Map?

I know how to do this for a non-generic interface:

internal class MVCDemoRegistry : Registry
    {
        public MVCDemoRegistry()
        {
            Scan(x =>
            {
                x.Assembly("MVCDemo");
                x.Assembly("MVCDemo.Infrastructure");
                x.Assembly("MVCDemo.Services");

                x.AddAllTypesOf<ISupplyView>();
            });
        }
    }
+5
source share
1 answer

I would go with something like

// in IToaster.cs
public interface IToaster<T> {}

// in your StructureMap registry
Scan(x =>
{
    x.Assembly("MVCDemo");
    x.Assembly("MVCDemo.Infrastructure");
    x.Assembly("MVCDemo.Services");

    x.AddAllTypesOf(typeof(IToaster<>))
});

The key here is that this approach uses the non-generic overload of AddAllTypesOf (). Otherwise, it will become sticky widgets.

See this SO thread for a good discussion around these issues: StructureMap Automatically register for generic types using Scan

This should do the trick, if only something about your approach is missing me - feel free to update if that is the case.

+10

All Articles