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 6022393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:51:07+00:00 2026-05-23T03:51:07+00:00

I am having a rough time implementing eventing in a recent project. I have

  • 0

I am having a rough time implementing eventing in a recent project.

I have verified that structuremap is scanning properly assemble and adding EventHandlers

Scan(cfg =>
            {
               cfg.TheCallingAssembly();
                cfg.IncludeNamespace("ABC.EventHandler");
                cfg.ConnectImplementationsToTypesClosing(typeof(IHandle<>));

           });

 public class StructureMapEventDispatcher : IEventDispatcher
    {

        public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent
        {

            foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>())
            {

                handler.Handle(eventToDispatch);

            }

        }

    }

Before I used to fire Event from Domain. Somthing like Dispatcher.RaiseEvent(new [domainEvent class](x,y,z));

and the event will get fired up. I had to change the design where I am now collectiong events in a collection

_domainEvents = new Collection<IDomainEvent>();

and then raising it after I have saved the domain to Repository

 public static void Raise(ICollection<IDomainEvent> domainEvents)
        {
            foreach (var domainEvent in domainEvents)
            {
                DomainEventDispatcher.Raise(domainEvent);
            }

        }

but now

ObjectFactory.GetAllInstances<IHandle<TEvent>>() returns 0 count of handlers

if I watch for

ObjectFactory.GetAllInstances<IHandle<DomainEventClass>>() it returns collection of handlers properly ( currently I have 2 and it shows 2 count)

… I am assuming this has something to do with events being raised as of type IDomainEvent instead of actual type and that is making it hard for structuremap to resolve it.

How can I solve this issue?

Regards,

The Mar

—

Edit 1:

I have conformed that struturemap container contains all event handlers scanned from the assembly.

Edit 2

I dont know how to make this question attract more attention. I am adding bounty for a solution to achieve the results desired. If the question is not clear, please ask.

Basically I want the ObjectFactory.GetAllInstances<IHandle<TEvent>>() to return handlers for TEvent where TEvent is of Type IDomainEvent. Events to be raised are stored in Collection of IDomainEvent and raised after the fact that Domain was saved (from service layer).

I am thinking there should be some way to make structuremap know that the event raised as IDomainEvent is actually of Type DomainEvent

var eventsToRaise= dealer.EventsToRaise();
Adding Information from Debug Window:

After the events have been raised in the dispatcher window

enter image description here

Edit 3:
Eventhough eventToRaise shows as “DealerName Changed” and “DealerCommunicationChanged”
typeof(TEvent) gives Type as Domain.IDomainEvent

I guesss if it is possible to get be able to cast to right type ( from whereever VS watch window is getting info) the problem could get resolved

—– Result—

Both approach worked. I put both approached to 2 other members in my team and we felt that solution without reflection to be selected as right answer.

Today we will be doing a test with changed implementation and see if there are any issues with this solution in the solution.

I have upvoted reflection based solution as it is also right answer.


  • 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-05-23T03:51:07+00:00Added an answer on May 23, 2026 at 3:51 am

    Instead of a reflection-based approach, I would recommend using a record to hold onto the type information for you. Something like this:

    interface IEventRecord
    {
        void Dispatch(IEventDispatcher dispatcher);
    }
    
    public class EventRecord<TEvent> : IEventRecord where TEvent : IDomainEvent
    {
        TEvent theEvent;
    
        public EventRecord(TEvent theEvent)
        {
            this.theEvent = theEvent;
        }
    
        public void Dispatch(IEventDispatcher dispatcher)
        {
            dispatcher.Dispatch(theEvent);
        }
    }
    

    If you find instantiating an event records to be cumbersome, a helper could infer the type parameter like this:

    public static EventRecord<TEvent> CreateEventRecord<TEvent>(TEvent theEvent) where TEvent : IDomainEvent
    {
        return new EventRecord<TEvent>(theEvent);
    }
    

    Which would allow you to instantiate event records like this:

    var record = CreateEventRecord(myDomainEvent);
    

    Then instead of holding onto a collection of IDomainEvents, hold onto a collection of IEventRecords that hold the necessary type data to raise themselves:

    foreach (var eventRecord in Records)
    {
        eventRecord.Dispatch(myDispatcher);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a rough time wrapping my head around this. I have an HTML
I'm using coleifer/Django-Relationships package for my project. I've been having a rough time in
I'm having a pretty rough time trying to make use of interprolog I have
I'm having a rough time with this membership stuff. OK, so, it's really odd.
I have a rough php script that sees if a user has filled in
Man, I'm having a rough day. I have a ASP.NET 2.0 application with AJAX
I'm having a rough time with Apache and my Rails app on my production
I'm having a rough time here with Resque, firstly, in development when running rake
I'm having a bit of a rough time laying out how I would count
Having a rough time getting this working. I convert this text: [url]http://www.stackoverflow.com[/url] to <a

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.