我想装饰使用城堡windsor我的命令处理程序,但似乎我的注册是不正确的,因为该类没有装饰。注册城堡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.
“_... am am difficult_”会有什么困难?也试着坚持每个帖子一个问题。 –
@PatrickQuirk是对的。我试着在实现编辑之前回答关于装饰器的问题。请回滚并打开另一个问题,而不是 – samy
我在这里打开另一个问题:http://stackoverflow.com/questions/29627510/castle-windsor-instances-are-registered-as-singleton-even-though-explicitly-decl – Xerxes