【问题标题】:StructureMap, scan assemblies and scopingStructureMap,扫描程序集和范围
【发布时间】:2010-07-14 00:19:52
【问题描述】:

我如何在扫描程序集时添加一些范围? Google 似乎对“structuremap scan cacheby”不太满意:/

ObjectFactory.Configure(registry =>
{
    registry.Scan(x =>
    {
        x.AssemblyContainingType(typeof(IRepository<>));
        x.With<DefaultConventionScanner>();
    });
}

【问题讨论】:

    标签: c# .net structuremap


    【解决方案1】:

    以下是使其与较新的 IRegistrationConvention 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】:

      我解决这个问题的方法是构建一个自定义约定扫描器:

      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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-18
        • 1970-01-01
        相关资源
        最近更新 更多