嗨我開始使用Simple Injector DI container爲我的所有項目,並希望如何適合一些建議是強大的功能,以我的要求。簡單的噴油器使用不同的裝飾器爲不同的命令
我有一對夫婦的命令處理程序裝飾的將包裹命令:
public class TransactionCommandHandlerDecorator<TCommand>
: ICommandHandler<TCommand>
{
private readonly ICommandHandler<TCommand> handlerToCall;
private readonly IUnitOfWork unitOfWork;
public TransactionCommandHandlerDecorator(
IUnitOfWork unitOfWork,
ICommandHandler<TCommand> decorated)
{
this.handlerToCall = decorated;
this.unitOfWork = unitOfWork;
}
public void Handle(TCommand command)
{
this.handlerToCall.Handle(command);
unitOfWork.Save();
}
}
互斥裝飾:
public class TransactionCommandHandlerWithMutexDecorator<TCommand>
: ICommandHandler<TCommand>
{
private readonly ICommandHandler<TCommand> handlerToCall;
private readonly IUnitOfWork unitOfWork;
private static object Lock = new object();
public TransactionCommandHandlerWithMutexDecorator(
IUnitOfWork unitOfWork,
ICommandHandler<TCommand> decorated)
{
this.handlerToCall = decorated;
this.unitOfWork = unitOfWork;
}
public void Handle(TCommand command)
{
lock (Lock)
{
this.handlerToCall.Handle(command);
unitOfWork.Save();
}
}
}
在某些情況下,它纔有意義包裹在這種阻塞方式的一些命令(通過使用TransactionCommandHandlerWithMutexDecorator),並允許其他人在所有線程(使用TransactionCommandHandlerDecorator)進行無障礙訪問,此外,如果不在命令類型之間共享互斥鎖 - 使用我當前的代碼,該鎖定是靜態的並且是在所有類型之間共享。
所以到我的問題:
1)如何我可以申請TransactionCommandHandlerWithMutexDecorator到一個特定的命令或命令,並使用TransactionCommandHandlerDecorator對於其餘的 - 我會用ExpressionBuilt事件?
2)我是否需要爲每個要裝飾的命令創建一個新類(以確保每個命令有唯一的鎖對象),還是有一些方法(使用攔截)?
欣賞上述最佳方法的建議。
感謝,
克里斯
謝謝史蒂文我剛剛意識到閱讀你的回覆你是對的,就像我需要/想要開箱即用的具體實現一樣。 感謝迄今爲止的幫助,我將如何按照第1個問題去裝飾一些命令? – g18c
@ g18c:對不起,我錯過了你最後一個問題。請參閱我的更新。 – Steven