StructureMap,扫描组件和范围

时间:2009-02-19 19:35:39

标签: c# .net structuremap

如何在扫描程序集时添加一些作用域? Google对“structuremap scan cacheby”:/

似乎并不满意
ObjectFactory.Configure(registry =>
{
    registry.Scan(x =>
    {
        x.AssemblyContainingType(typeof(IRepository<>));
        x.With<DefaultConventionScanner>();
    });
}

2 个答案:

答案 0 :(得分:6)

以下是一种使用更新的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>();
    });
});

答案 1 :(得分: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
}