简体   繁体   English

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

[英]Where to register domain event handlers in c# class library DLL

I have a solution set up like so: 我有这样一个解决方案设置:

  • Solution
    1. Visual Basic ASP.NET Web Application (.NET4) Visual Basic ASP.NET Web应用程序(.NET4)
    2. C# Class Library (.NET2) C#类库(.NET2)

The class library DLL is included as a reference in the web application. 该类库DLL包含在Web应用程序中作为参考。

The class library makes extensive use of domain driven architecture. 类库大量使用域驱动的体系结构。 Now, I'm in the process of adding domain events, in the way of Udi Dahan . 现在,我正在以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);
    }
} 

I need to register my domain event handlers in the class library. 我需要在类库中注册域事件处理程序。 There is no global.asax for a class library, so I cannot make use of Application_Start . 没有用于类库的global.asax ,因此我无法使用Application_Start Where is the best place to register domain event handlers in a class library? 在类库中注册域事件处理程序的最佳位置在哪里?

Your application is responsible for glueing everything together. 您的应用程序负责将所有内容粘合在一起。

You either hook everything up on Application_Start or you invoke a function in the class library from there that registers your handlers. 您可以将所有内容都挂在Application_Start或者从那里调用类库中的注册处理程序的函数。

new Bootstrapper().Bootstrap();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM