我存儲兩個接口:
public interface IDomainEvent { }
和
public interface IHandle<T> where T: IDomainEvent, new()
EventDispatcher
類被定義有還有:
public static class EventDispatcher {
[ThreadStatic]
private static List<Delegate> actions;
[ThreadStatic]
private static List<Object> handlers;
public static List<Object> Handlers {
get { return handlers; }
set { handlers = value; }
}
public static void Register<T>(Action<T> callback) where T : IDomainEvent, new() {
if(null == actions) {
actions = new List<Delegate>();
actions.Add(callback);
}
}
public static void ClearCallbacks() {
actions = null;
}
public static void Raise<T>(T @event) where T : IDomainEvent, new() {
if(null != Handlers) {
foreach(var handler in Handlers.Where(h => h is IHandle<T>)) {
((IHandle<T>)handler).Handle(@event);
}
}
if(null != actions) {
foreach(var action in actions) {
if(action is Action<T>) {
((Action<T>)action)(@event);
}
}
} // if(null != actions) {
}
}
有在表示層組件模塊:
public class EventDispatchingModule : NinjectModule {
public override void Load() {
// EventDispatcher.Handlers = this.Kernel.GetAll(IHandle<T>); Can't do that!
// Bind<IHandle<CarHasBeenRegisteredEvent>>().To<CarHasBeenRegisteredHandler();
}
}
所以我不能叫Kernel.GetAll(IHandle<T>)
那裏,因爲它解決不了T
參數。
我該如何解決這個問題?
謝謝!
你看我不想做內核的副本,使用服務位置這個類駐留在領域層/單獨的程序集中我嘗試了一個類似於你的解決方案:我使用動態屬性來獲取一個內核的副本如果這樣做,你需要添加一個對Ninject程序集的引用到一個域層,這樣它可以調用擴展方法'GetAll(serviceArgs)'並且我知道你的容器,但我只是想試着用這種方式) – lexeme
@brick:我已經更新了代碼,並解決了您的服務位置問題。 – jgauffin