jbogard / MediatR.Extensions.Microsoft.DependencyInjection

MediatR extensions for Microsoft.Extensions.DependencyInjection
MIT License
326 stars 90 forks source link

Use TryAdd vs Add to allow multiple calls to AddMediatr(..) #77

Closed remcoros closed 4 years ago

remcoros commented 5 years ago

This allows stuff like

Main program:

foreach(var pluginAssembly in plugins)
{
   // reflection magic, get some IPlugin / IStartup type
  pluginInstance.ConfigureService(...)
}

In PluginFoo:

ConfigureServices() 
{ 
  services.AddMediatr(my own assembly) 
}

This is also the pattern used in MVC et al, to allow multiple calls to AddMvc() without ending up with duplicate registrations in the container.

jbogard commented 5 years ago

Test failures :(

remcoros commented 5 years ago

That's weird, there shouldn't be any behavior change. I'll look into it when I get back home and add specific test cases.

remcoros commented 5 years ago

Got it, let me know what you think.

FuncLun commented 5 years ago

I am also trying to use a composable setup. Changing Add to TryAdd will cause the configuration to be first-writer-wins (possibly from last-writer-wins?), while giving each call to AddMediatR() the impression of being able to set the configuration with each invocation.

I suggest in addition to TryAdd: 1) Add AddMediatR() overload with no Assembly or Type parameter (passing new Assembly[0] to main function) 1) The AddMediatR() main function should not throw on !assemblies.Any() 1) The ServiceRegistrar.AddMediatRClasses() function should be turned into an extension, staying in MediatR.Registration.ServiceRegistrar to not break backwards compatibility and per jimmy's comment "I don't really want to expand the public API in the root namespace" in #60. 1) Add AddMediatRClasses() overload for params Assembly[]

Example: https://github.com/FuncLun/MediatR.Extensions.Microsoft.DependencyInjection/commit/695622b8406f463ede644bf11b1dbadbd67677b7