-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: #247 Down the rabbit hole replacing the resolution with injection #252
Conversation
…njection where possible Tests are green. Samples are running fine! Though it was needed to turn off constrained stuff for Ninject and Unity. changed: Request / Notification wrappers to RequestMediator / NotificationMediator to represent the capabilities; Handle methods changed to Send / Publish. added: Added both TRequest typed and un-typed IRequestMediator, INotificationMediator for consistency changed: Mediator.PublishCore method was renamed to PublishBehavior to be more friendly, parameters in notification mediator were renamed to. changed: DryIocZero has a few resolution roots, for request and notification mediators only. That's how Pure DI can look like :) removed: IMediator and ServiceFactory registrations from Tests and Samples, cause not needed removed: Caches from Mediator, relying on IoCs lifetime management and cache
What is still missing or can be added:
|
…eptions removed: Unnecessary IMediator resolution root in DryIocZero sample, now it is clean and shiny reverted: Unnecessary changes in tests
After this PR, the More often you may want to inject Given that
The default Then we may add
|
# Conflicts: # src/MediatR/INotificationHandler.cs # test/MediatR.Tests/SendVoidInterfaceTests.cs
I'm not sure I'd inject the individual mediators unless I was only sending one command/notification. Part of the point was instead of Not discounting the value of the composition roots - it's just generally more annoying from the outermost layer. |
You may do both, inject |
added: missing xml docs; moved: PublishCore to NotificationMediator; cleanup
I've likely done with this PR. Confirmed for myself couple of things about IoC lib integration and my DryIoc family. |
var mediator = container.Resolve<IMediator>(); | ||
|
||
return mediator; | ||
return new Mediator(container.Resolve); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd still want to register IMediator, no?
For the sake of this sample and MediatR itself, no. If you want to inject IMediator on consumer side, then register it as any other service. |
So the purpose of the samples is to show how, in normal usage, you'd use MediatR. Consumers would still want to depend on |
This is fine with me. May be just an IHandler is not enough at the moment, given the pipeline behaviors, etc. My PR is just to fill the gaps in abstractions from IHandler to the IMediator. If you will decide to merge, I can revert back samples to register IMediator and ServiceFactory. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how this also gets rid of the type cache dictionary in mediator.
Maybe not register the individual Req/Not.Handler, but move them to 'Internal' namespace (and leave the types public).
src/MediatR/Mediator.cs
Outdated
protected virtual Task PublishCore(IEnumerable<Task> allHandlers) | ||
{ | ||
return Task.WhenAll(allHandlers); | ||
var mediator = (INotificationMediator<TNotification>)_serviceFactory(typeof(INotificationMediator<TNotification>)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be 'notification.GetType() / MakeGenericType' like the original code and how Send does it. So you don't have to pass concrete types (e.g. if you pass an 'IEvent' it resolves the actual type of the object, not of IEvent).
Yes, you are now in full control of lifetime and scoping of mediator(s). No assumptions. Just use your lovely IoC registration features.
May be you're right. I was thinking to get rid off reflection as much as possible. The whole point of explicit I would've do the same thing for |
So I think at the end, this would still increase the registrations people would use? Or they'd have two entry points, one a non-generic interface ( At that point, I'm not sure the generic interfaces are even mediators any more. The mediator pattern is to encapsulate communication, and now what you're communicating is in the type as opposed to the method. This lets for example a web API controller only depend on Currently, you don't have many registrations required for the "normal" usage. You'd do |
It would be plus 1 or plus 2 registrations per whole setup. And you need to understand why are you doing those registrations, which is good in my opinion. But look at registrations in the Examples. It is kinda hard to compare across the containers, even blindly by code size. So adding only two things is good or bad? I am Ok to have those in DryIoc and DIZero. My view, I would rather have two additional registrations with clear goal and control, than a internal magic glue. But it is my view only. |
changed: Mediator default impl. uses Activator.CreateInstance as before PR - no need for additional registrations added: MediatorExtensions with Send and Publish to IMediator added: the DryIoc(Zero) and NoIoC examples use custom InjectingMediator with registered IRequestMediator and INotfiicationMediator - so both approaches valid removed: IRequestMediator and INotfiicationMediator from the rest of Examples and Tests reverted: Rest of examples are registering IMediator and ServiceFactory as prio PR
Hi again :) I have reverted default registration back (almost), so no need for The option is more simple now because of role split between |
@jbogard, I will close the PR now as 5.0 is out. |
added: Added both typed and un-typed IRequestMediator, INotificationMediator to represent the capabilities and the entry points of MediatR
changed: Request / Notification wrappers to RequestMediator / NotificationMediator; Handle methods changed to Send / Publish.
changed: Mediator.PublishCore method was renamed to PublishBehavior to be more friendly, parameters in notification mediator were renamed to.
changed: DryIocZero has a few resolution roots, for request and notification mediators only. That's how Pure DI can look like :)
removed: IMediator and ServiceFactory registrations from Tests and Samples, cause not needed
removed: Caches from Mediator, relying on IoCs lifetime management and cache