3

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

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

2 回答 2

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>();
    });
});
于 2010-07-13T16:19:52.837 回答
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
}
于 2009-10-06T14:26:26.663 回答