StructureMap,扫描组件和范围

mat*_*ieu 3 .net c# structuremap

如何在扫描程序集时添加一些作用域?谷歌似乎对"结构图扫描缓存"感到满意:/ /

ObjectFactory.Configure(registry =>
{
    registry.Scan(x =>
    {
        x.AssemblyContainingType(typeof(IRepository<>));
        x.With<DefaultConventionScanner>();
    });
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ann 6

以下是一种使用更新的IRegistrationConvention API的方法:

public class SingletonConvention : IRegistrationConvention
{
    #region IRegistrationConvention Members

    public void Process(Type type, Registry registry)
    {
        registry.For(type).Singleton();
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

它可以像这样使用:

container.Configure(registry =>
{
    registry.Scan(x =>
    {
        x.AssemblyContainingType<Foo>();
        x.AddAllTypesOf<IFoo>();
        x.Convention<SingletonConvention>();
    });
});
Run Code Online (Sandbox Code Playgroud)