在c#类库DLL中注册域事件处理程序的位置



我有一个解决方案设置如下:

  • 解决方案
    1. Visual Basic.NET Web Application (.NET4)
    2. c#类库(.NET2)

类库DLL包含在web应用程序中作为参考。

类库广泛使用了领域驱动的体系结构。现在,我正在添加域事件,以Udi Dahan的方式。

public static class DomainEvents
{ 
    [ThreadStatic] //so that each thread has its own callbacks
    private static List<Delegate> actions;
    public static IContainer Container { get; set; } //as before
    //Registers a callback for the given domain event
    public static void Register<T>(Action<T> callback) where T : IDomainEvent
    {
        if (actions == null)
            actions = new List<Delegate>();
            actions.Add(callback);
    }
    //Clears callbacks passed to Register on the current thread
    public static void ClearCallbacks ()
    {
        actions = null;
    }
    //Raises the given domain event
    public static void Raise<T>(T args) where T : IDomainEvent
    {
    if (Container != null)
        foreach(var handler in Container.ResolveAll<Handles<T>>())
            handler.Handle(args);
    if (actions != null)
        foreach (var action in actions)
            if (action is Action<T>)
                ((Action<T>)action)(args);
    }
} 

我需要在类库中注册域事件处理程序。类库没有global.asax,所以我不能使用Application_Start。在类库中注册域事件处理程序的最佳位置是哪里?

您的应用程序负责将所有内容粘合在一起。

你要么把一切都挂在Application_Start上,要么在类库中调用一个函数,从那里注册你的处理程序。

new Bootstrapper().Bootstrap();

最新更新