0

I have multiple implementations of this interface:

 public interface IDomainEventHandler<TDomainEventContext> 
    where TDomainEventContext : IDomainEventContext
{
    void Handle(TDomainEventContext context, object sender); 
}

and I want to resolve some of them on run time according to the type of the event context, so i added typed factory to my system:

ublic interface IDomainEventHandlerFactory : IDisposable
{
   IEnumerable<IDomainEventHandler<TDomainEventContext>> ResolveAll<TDomainEventContext>() 
        where TDomainEventContext : IDomainEventContext;
}

and this is my registration:

container.Register(Classes.FromAssemblyContaining<SomeClass>().
         BasedOn(typeof(IDomainEventHandler<>)).WithServiceAllInterfaces().
         LifestyleTransient());

Now I want to do the next thing:

adding Name property to the IDomainEventHandler interface :

public interface IDomainEventHandler<TDomainEventContext> 
    where TDomainEventContext : IDomainEventContext
{
    string Name { get; }

    void Handle(TDomainEventContext context, object sender); 
}

Then I want to register each event handler with this Name property (by convention), and then I want to be able to resolve all the components with the same name (and the same type of event context)

I hope my goal is clear enough

so my questions are:

1) how am i doing the registration part (against the Name field in my interface)?

2) is it possible for two event handlers to register with the same name?

3) how the factory should looks like?

again my goal is to be able to say: i want all the event handlers handling event context of type "AddingItemEventContext" but only those with name "ElectronicItem" (and then i get all the handlers that relevant to me)

Eitan
  • 125
  • 10
  • 1
    This sounds like a good candidate for the [Strategy Pattern](https://sourcemaking.com/design_patterns/strategy). See [Dependency injection type-selection](https://stackoverflow.com/a/34331154) for an implementation that selects multiple services for a single event. – NightOwl888 Oct 15 '17 at 02:03

1 Answers1

1

is it possible for two event handlers to register with the same name?

No. Hence resolving with container won't work with this design.

What you can do in your factory is to resolve all handlers matching required type and then filter out those that don't match provided name.

Jan Muncinsky
  • 4,282
  • 4
  • 22
  • 40