Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:19:11+00:00 2026-06-13T10:19:11+00:00

I’m using Ninject in an MVC project and am trying to implement Domain Events

  • 0

I’m using Ninject in an MVC project and am trying to implement Domain Events following Udi Dahan’s pattern http://www.udidahan.com/2009/06/14/domain-events-salvation/

In the extract below, the “Container” is used to resolve all the event-handlers for the particular type of event that has been raised.

My question (& apologies if I am missing something basic) is how to do this with Ninject? In other words:

  1. How does the “Container” get set in this static class?

  2. Once I have a Container (Kernel?) what would be the Ninject syntax to resolve all the event handlers (which I’m assuming I would have to register before-hand in a Service Module)?

I keep reading in posts that only constructor injection should be used and everything recursively get resolved from that, and that accessing the Ninject Kernel is a no-no. So any advice on how to do this will be much appreciated.

Extract from the article

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);
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:19:13+00:00Added an answer on June 13, 2026 at 10:19 am

    How does the “Container” get set in this static class?

    You will have to set it during application startup:

    DomainEvents.Container = kernel;
    

    what would be the Ninject syntax to resolve all the event handlers:

    You can do it like this, for instance:

    Container.Get<IEnumerable<Handles<T>>>())
    

    Udi’s static DomainEvents class is an implementation of the Ambient Context anti-pattern (see DI PP&P chapter 5.3). In this case I would rather use dependency injection to inject an IDomainEvents abstraction into code that needs it, instead of letting code depend on a static instance.

    The problem however is that your domain objects will need a dependency on the IDomainEvents and constructor injection is (probably) not possible. The trick is to use method injection in that case.

    In other words, use constructor injection to inject the IDomainEvents into command handlers or services (or what ever you call your business logic that uses the methods on your domain objects) and pass that dependency into the domain object when calling a method that needs it (method injection).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.