简体   繁体   中英

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)
    2. C# Class Library (.NET2)

The class library DLL is included as a reference in the web application.

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 .

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 . 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.

new Bootstrapper().Bootstrap();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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