我想裝飾使用城堡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