gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:44:32 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用 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.

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com