代码之家  ›  专栏  ›  技术社区  ›  mathieu

StructureMap、扫描组件和范围

  •  3
  • mathieu  · 技术社区  · 17 年前

    ObjectFactory.Configure(registry =>
    {
        registry.Scan(x =>
        {
            x.AssemblyContainingType(typeof(IRepository<>));
            x.With<DefaultConventionScanner>();
        });
    }
    
    2 回复  |  直到 17 年前
        1
  •  6
  •   Mark Seemann    16 年前

    以下是使其与较新的IRegistrationConventionneneneba API一起工作的方法:

    public class SingletonConvention : IRegistrationConvention
    {
        #region IRegistrationConvention Members
    
        public void Process(Type type, Registry registry)
        {
            registry.For(type).Singleton();
        }
    
        #endregion
    }
    

    它可以这样使用:

    container.Configure(registry =>
    {
        registry.Scan(x =>
        {
            x.AssemblyContainingType<Foo>();
            x.AddAllTypesOf<IFoo>();
            x.Convention<SingletonConvention>();
        });
    });
    
        2
  •  2
  •   Rogeclub    16 年前

    public class CustomScanner : ITypeScanner
    {
        #region ITypeScanner Members
    
        public void Process(Type type, PluginGraph graph)
        {                                   
            graph.AddType(type);
            var family = graph.FindFamily(type);
            family.AddType(type);
            family.SetScopeTo(InstanceScope.Hybrid);
        }
    
        #endregion
    }
    
    推荐文章