Einfache Injektor öffnen generische Dekorateure

Ich versuche, einige der netten Eigenschaften im einfachen Injektor zu nutzen.

Ich habe momentan Probleme mit den Dekorateuren, sie werden nicht getroffen, wenn ich sie auch erwarte.

Ich melde sie so an:

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>)
 );

Ich denke, mir muss etwas fehlen, da ich davon einen Anruf erwarteICommandHandler<CreateProductCommand> wird aufrufenCreateValidFriendlyUrlCommandHandler<> undCreateProductValidationCommandHandler<> bevor sie sich selbst laufen.

Ich habe versucht, mich anders zu registrieren:

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)
 );

Da habe ich mir überlegt einen Dekorateur für einzuschreibenICommandHandler<CreateProductCommand> auf den typICommandHandler<CreateProductCommand> wenn derCreateProductValidationCommandHandler undCreateValidFriendlyUrlCommandHandler implementierenICommandHandler<CreateProductCommand> trifft vielleicht ein bisschen auf einen Zirkelverweis.

Aber das zu ändern, machte keinen Unterschied.

Hier ist meinCreateProductValidationCommandHandler<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;
        }
    }
}

Und das ist meinCreateValidFriendlyUrlCommandHandler<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);
        }
    }
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage