扫描程序集时如何添加一些范围?Google 似乎对“structuremap scan cacheby”不太满意:/
ObjectFactory.Configure(registry =>
{
registry.Scan(x =>
{
x.AssemblyContainingType(typeof(IRepository<>));
x.With<DefaultConventionScanner>();
});
}
扫描程序集时如何添加一些范围?Google 似乎对“structuremap scan cacheby”不太满意:/
ObjectFactory.Configure(registry =>
{
registry.Scan(x =>
{
x.AssemblyContainingType(typeof(IRepository<>));
x.With<DefaultConventionScanner>();
});
}
这是使其与较新的 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>();
});
});
我解决这个问题的方法是构建一个自定义约定扫描器:
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
}