如何在Autofac中装饰特定类型?

时间:2017-10-16 15:35:32

标签: c# autofac

我有这样的命令:

public class EditComplaintCommandHandler : IHandleCommand<EditComplaintCommand>
{
    private readonly IUnitOfWork _unitOfWork;

    public EditComplaintCommandHandler(
        IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public void Handle(EditComplaintCommand command)
    {
        // do stuff
    }
}

现在我只想装饰这个命令。这是我的装饰师:

public class MySpecificCommandDecorator : IHandleCommand<EditComplaintCommand>
{
    private readonly IUnitOfWork _unitOfWork;

    public MySpecificCommandDecorator(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public void Handle(EditComplaintCommand command)
    {
       // do something
    }
}

所以我希望MySpecificCommandDecorator仅装饰EditComplaintCommandHandler

我试过了:

builder.RegisterAssemblyTypes(assemblies)
       .AsClosedTypesOf(typeof(IHandleCommand<>))
       .Named("handler", typeof(IHandleCommand<>));

builder.Register(c => new MySpecificCommandDecorator(c.Resolve<IUnitOfWork>()))
       .As<IHandleCommand<EditComplaintCommand>>();

但这并不适用,因为我没有将它注册为装饰者。

如何使用Autofac仅使用EditComplaintCommandHandler装饰MySpecificCommandDecorator

0 个答案:

没有答案