在结构图中为 DecorateAllWith() 方法定义过滤器 3
Define filter for DecorateAllWith() method in structure-map 3
我使用以下语句用 Decorator1<>
装饰我所有的 ICommandHandlers<>
:
ObjectFactory.Configure(x =>
{
x.For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(Decorator1<>));
});
但是因为 Decorator1<>
实现了 ICommandHandlers<>
,Decorator1<>
class 也装饰了自己。
所以,问题是 Decorator1
在我注册所有 ICommandHandler<>
时无意中注册了。
我如何过滤 DecorateWithAll()
来装饰除 Decorator1<>
之外的所有 ICommandHandler<>
?
ObjectFactory
已过时。
您可以使用如下代码排除 Decorator1<>
注册为香草 ICommandHandler<>
:
var container = new Container(config =>
{
config.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(ICommandHandler<>));
scanner.Exclude(t => t == typeof(Decorator1<>));
scanner.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
});
config.For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(Decorator1<>));
});
更新
对于多个模块,您可以使用 The Registry DSL 组成应用程序的一部分
using StructureMap;
using StructureMap.Configuration.DSL;
public class CommandHandlerRegistry : Registry
{
public CommandHandlerRegistry()
{
Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(ICommandHandler<>));
scanner.Exclude(t => t == typeof(Decorator1<>));
scanner.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
});
For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(Decorator1<>));
}
}
只有组合根需要知道所有 Registry
实现。
var container = new Container(config =>
{
config.AddRegistry<CommandHandlerRegistry>();
});
您甚至可以选择在运行时查找所有 Registry
个实例(您仍然需要确保加载所有必需的程序集)
var container = new Container(config =>
{
var registries = (
from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.DefinedTypes
where typeof(Registry).IsAssignableFrom(type)
where !type.IsAbstract
where !type.Namespace.StartsWith("StructureMap")
select Activator.CreateInstance(type))
.Cast<Registry>();
foreach (var registry in registries)
{
config.AddRegistry(registry);
}
});
我使用以下语句用 Decorator1<>
装饰我所有的 ICommandHandlers<>
:
ObjectFactory.Configure(x =>
{
x.For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(Decorator1<>));
});
但是因为 Decorator1<>
实现了 ICommandHandlers<>
,Decorator1<>
class 也装饰了自己。
所以,问题是 Decorator1
在我注册所有 ICommandHandler<>
时无意中注册了。
我如何过滤 DecorateWithAll()
来装饰除 Decorator1<>
之外的所有 ICommandHandler<>
?
ObjectFactory
已过时。
您可以使用如下代码排除 Decorator1<>
注册为香草 ICommandHandler<>
:
var container = new Container(config =>
{
config.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(ICommandHandler<>));
scanner.Exclude(t => t == typeof(Decorator1<>));
scanner.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
});
config.For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(Decorator1<>));
});
更新
对于多个模块,您可以使用 The Registry DSL 组成应用程序的一部分
using StructureMap;
using StructureMap.Configuration.DSL;
public class CommandHandlerRegistry : Registry
{
public CommandHandlerRegistry()
{
Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(ICommandHandler<>));
scanner.Exclude(t => t == typeof(Decorator1<>));
scanner.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
});
For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(Decorator1<>));
}
}
只有组合根需要知道所有 Registry
实现。
var container = new Container(config =>
{
config.AddRegistry<CommandHandlerRegistry>();
});
您甚至可以选择在运行时查找所有 Registry
个实例(您仍然需要确保加载所有必需的程序集)
var container = new Container(config =>
{
var registries = (
from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.DefinedTypes
where typeof(Registry).IsAssignableFrom(type)
where !type.IsAbstract
where !type.Namespace.StartsWith("StructureMap")
select Activator.CreateInstance(type))
.Cast<Registry>();
foreach (var registry in registries)
{
config.AddRegistry(registry);
}
});