Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

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

Merged
merged 3 commits into from
Jun 30, 2020

Conversation

remcoros
Copy link
Contributor

@remcoros remcoros commented Oct 3, 2019

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
Copy link
Owner

jbogard commented Oct 4, 2019

Test failures :(

@remcoros
Copy link
Contributor Author

remcoros commented Oct 4, 2019

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.

- Fix use of TryAdd, by handling registration of IPipelineBehavior a bit differently
@remcoros
Copy link
Contributor Author

remcoros commented Oct 4, 2019

Got it, let me know what you think.

@FuncLun
Copy link

FuncLun commented Oct 20, 2019

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)
  2. The AddMediatR() main function should not throw on !assemblies.Any()
  3. 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 Make ServiceCollectionExtensions.AddMediatRClasses public #60.
  4. Add AddMediatRClasses() overload for params Assembly[]

Example:
FuncLun@695622b

@jbogard jbogard merged commit a8160b8 into jbogard:master Jun 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants