StructureMap Auto registration for generic types using Scan

前端 未结 3 678
情书的邮戳
情书的邮戳 2020-12-03 06:21

I\'ve got an interface:

IRepository where T : IEntity

while im knocking up my UI im using some fake repository implementations tha

相关标签:
3条回答
  • 2020-12-03 06:29

    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.

    0 讨论(0)
  • 2020-12-03 06:38

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

    0 讨论(0)
  • 2020-12-03 06:47

    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);
            }
        }
    } 
    
    0 讨论(0)
提交回复
热议问题