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

时间:2014-07-22 15:08:23

标签: c# events domain-driven-design

我有一个像这样的解决方案:

    1. Visual Basic ASP.NET Web应用程序(.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。在类库中注册域事件处理程序的最佳位置在哪里?

1 个答案:

答案 0 :(得分:1)

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

您可以在Application_Start上挂钩所有内容,也可以在那里调用类库中的一个函数来注册您的处理程序。

new Bootstrapper().Bootstrap();
相关问题