c# - 在温莎城堡注册通用装饰器?

标签 c# inversion-of-control castle-windsor

<分区>

我正在尝试使用 caSTLe windsor 装饰我的命令处理程序,但似乎我的注册不正确,因为类没有装饰。

我有以下安装程序:

internal class CommandsInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
            Component.For<IDbConnection>()
                .UsingFactoryMethod(() => ConnectionHelper.GetOpenDbConnection(Connection.DatabaseName.ReedOnline))
                .LifestylePerWebRequest());

        container.Register(
            Classes
                .FromAssemblyContaining<EcruiterCommands>()
                .Where(t => t.Name.EndsWith("Commands"))
                .WithService
                .AllInterfaces().LifestylePerWebRequest());

        container.Register(
            Classes
                .FromAssemblyContaining<EcruiterCommands>()
                .Where(t => t.Name.EndsWith("CommandHandler"))
                .WithService.AllInterfaces()
                .LifestylePerWebRequest());

        container.Register(Component.For(typeof (ICommandHandler<>))
            .ImplementedBy(typeof (TransactionCommandHandlerDecorator<>))
            .IsDefault()
            .LifestylePerWebRequest());

        container.Register(Component.For(typeof (ICommandHandler<>))
            .ImplementedBy(typeof (ExceptionHandlingCommandHandlerDecorator<>))
            .IsDefault()
            .LifestylePerWebRequest());            
    }
}

这是我的装饰器:

namespace TempSearch.Ioc.Decorators.CommandHandlers
{
    public class TransactionCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
    {
        private readonly ICommandHandler<TCommand> decorated;

        public TransactionCommandHandlerDecorator(ICommandHandler<TCommand> decorated)
        {
            this.decorated = decorated;
        }

        public void Handle(TCommand command)
        {
            using (var scope = new TransactionScope())
            {
                decorated.Handle(command);
                scope.Complete();
            }
        }
    }
}

此外,我想知道我的装饰器是否应该存在于合成根中或它们正在装饰的类的程序集中。现在我已将它们移动到组合根目录,因为温莎城堡试图将我的装饰器与其他类一起注册,我会收到错误消息:

Component TempSearch.Command.Data.Decorators.TransactionCommandHandlerDecorator`1 could not be registered.
There is already a component with that name.
Did you want to modify the existing component instead?
If not, make sure you specify a unique name.

最佳答案

首先,关于“already registered”的错误,你正在注册你的组件两次

container.Register(
    Classes
        .FromAssemblyContaining<EcruiterCommands>()
        .BasedOn(typeof (ICommandHandler<>))
        .WithService.AllInterfaces()
        .LifestylePerWebRequest());

这会注册所有基于 ICommandHandler<> 的类, 所以 TransactionCommandHandlerDecorator已经注册

关于您要使用的装饰器模式,我会使用城堡的 interceptors 来实现它反而。我发现 Decorators 模式是 not very easy在城堡里做;链接的答案很旧,CaSTLe 从那以后发生了变化,所以我可能是错的,但拦截器就是你想要的。

关于c# - 在温莎城堡注册通用装饰器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29626130/

相关文章:

c# - DotNetNuke ModuleAction 服务器端处理

c# - 如何使用 Prism 和 Unity 显示一个 View 的多个实例

c# - 如何使用 Ioc Unity 注入(inject)依赖属性

c# - 使用 Windsor CaSTLe 从目录中检索 DLL

c# - 将 ComponentRegistration 向下转换为非泛型类型

c# - 如何使用 CaSTLe Windsor 通过 WPF 应用程序实现正确的 Di

c# - 解决文件路径太长异常的最佳方法

c# - 覆盖 syndicationfeed 中的根元素,将 namespace 添加到根元素

dependency-injection - 如何使用 Ninject 在子类中创建实例?

c# - 在 CaSTLe Windsor 中调用 ResolveAll 时的顺序