StructureMap Auto registration for generic types using Scan

六眼飞鱼酱① 提交于 2019-11-27 13:46:35
JD Courtoy

There is an easier way to do this. Please see this blog posting for details: Advanced StructureMap: connecting implementations to open generic types

public class HandlerRegistry : Registry
{
    public HandlerRegistry()
    {
        Scan(cfg =>
        {
            cfg.TheCallingAssembly();
            cfg.IncludeNamespaceContainingType<FakeRepositories.FakeClientRepository>();
            cfg.ConnectImplementationsToTypesClosing(typeof(IRepository<>));
        });
    }
}

Doing it this way avoids having to create your own ITypeScanner or conventions.

Andrew Bullock

Thanks Chris, thats exactly what I needed. For clarity, heres what I did from your link:

Scan(x =>
{
    x.TheCallingAssembly();
        x.IncludeNamespaceContainingType<FakeRepositories.FakeClientRepository>();
    x.With<FakeRepositoryScanner>(); 
});


private class FakeRepositoryScanner : ITypeScanner
{
    public void Process(Type type, PluginGraph graph)
    {
        Type interfaceType = type.FindInterfaceThatCloses(typeof(IRepository<>));
        if (interfaceType != null)
        {
            graph.AddType(interfaceType, type);
        }
    }
} 

Take a look at this discussion from the StructureMap users group: http://groups.google.com/group/structuremap-users/browse_thread/thread/649f5324c570347d

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