StructureMap Auto registration for generic types using Scan

前端 未结 3 708
情书的邮戳
情书的邮戳 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();
                cfg.ConnectImplementationsToTypesClosing(typeof(IRepository<>));
            });
        }
    }
    

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

提交回复
热议问题