views:

435

answers:

1

How do I register all the instances of a generic interface in Structured Map?

I know how to do this for a none generic interface:

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

                x.AddAllTypesOf<ISupplyView>();
            });
        }
    }
+4  A: 

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 this becomes a sticky widget indeed.

See this SO thread for a good discussion around these concerns: http://stackoverflow.com/questions/516892/structuremap-auto-registration-for-generic-types-using-scan

This should do the trick unless there's something about your approach I'm missing - feel free to update if so.

CitizenParker