我想在簡單的注入器中使用一些不錯的功能。簡單的注入器打開通用裝飾器
我目前有裝修問題,他們也沒有被打到,當我也期待他們。
我註冊它們是這樣的:
container.RegisterManyForOpenGeneric(
typeof(ICommandHandler<>),
AppDomain.CurrentDomain.GetAssemblies());
container.RegisterDecorator(
typeof(ICommandHandler<>),
typeof(CreateValidFriendlyUrlCommandHandler<>),
context => context.ServiceType == typeof(ICommandHandler<CreateProductCommand>)
);
container.RegisterDecorator(
typeof(ICommandHandler<>),
typeof(CreateProductValidationCommandHandler<>),
context => context.ServiceType == typeof(ICommandHandler<CreateProductCommand>)
);
我想我必須失去了一些東西,因爲我期待,爲ICommandHandler<CreateProductCommand>
一個通話會在運行本身之前調用CreateValidFriendlyUrlCommandHandler<>
和CreateProductValidationCommandHandler<>
。
我已經嘗試了不同的配準是這樣的:
container.RegisterManyForOpenGeneric(
typeof(ICommandHandler<>),
AppDomain.CurrentDomain.GetAssemblies());
container.RegisterDecorator(
typeof(ICommandHandler<>),
typeof(CreateValidFriendlyUrlCommandHandler<>),
context => context.ImplementationType == typeof(CreateProductCommandHandler)
);
container.RegisterDecorator(
typeof(ICommandHandler<>),
typeof(CreateProductValidationCommandHandler<>),
context => context.ImplementationType == typeof(CreateProductCommandHandler)
);
正如我認爲在型ICommandHandler<CreateProductCommand>
註冊一個裝飾爲ICommandHandler<CreateProductCommand>
當CreateProductValidationCommandHandler
和CreateValidFriendlyUrlCommandHandler
實施ICommandHandler<CreateProductCommand>
可能擊中位循環引用的。
但改變沒有區別。
這裏是我的CreateProductValidationCommandHandler<TCommand>
:
public class CreateProductValidationCommandHandler<TCommand>
: ICommandHandler<CreateProductCommand>
{
private readonly ICommandHandler<TCommand> decorated;
private readonly IValidationService validationService;
public CreateProductValidationCommandHandler(
ICommandHandler<TCommand> decorated,
IValidationService validationService)
{
this.decorated = decorated;
this.validationService = validationService;
}
public void Handle(CreateProductCommand command)
{
if (!validationService.IsValidFriendlyName(
command.Product.ProductFriendlyUrl))
{
command.ModelStateDictionary.AddModelError(
"ProductFriendlyUrl",
"The Friendly Product Name is not valid...");
return;
}
if (!validationService.IsUniqueFriendlyName(
command.Product.ProductFriendlyUrl))
{
command.ModelStateDictionary.AddModelError(
"ProductFriendlyUrl",
"The Friendly Product Name is ...");
return;
}
}
}
這是我CreateValidFriendlyUrlCommandHandler<TCommand>
:
public class CreateValidFriendlyUrlCommandHandler<TCommand>
: ICommandHandler<CreateProductCommand>
{
private readonly ICommandHandler<TCommand> decorated;
public CreateValidFriendlyUrlCommandHandler(ICommandHandler<TCommand> decorated)
{
this.decorated = decorated;
}
public void Handle(CreateProductCommand command)
{
if (string.IsNullOrWhiteSpace(
command.Product.ProductFriendlyUrl))
{
command.Product.ProductFriendlyUrl =
MakeFriendlyUrl(command.Product.Name);
}
}
}
你的裝飾器不應該是通用的。事實上,他們在技術上沒有裝飾者:-) – Steven
請注意,'Appdomain.CurrentDomain.GeAssemblies'只返回已經加載的程序集。如果所有實現都與'ICommandHandler'在同一個程序集中,那麼這不會成爲問題,但要小心。 –
Steven