Comments (8)
I don't know, are there?
from mediatr.
I use decorators all the time with StructureMap using MediatR if that's what you're asking.
from mediatr.
i have not had a chance to give it a spin but that makes me feel better knowing you use them all the time.
i have not decided upon a DI framework yet but was thinking StructureMap or SimpleInj. Wasn't looking forward to writing YetAnotherCommandHandler framework ;)
Thanks Jimmy
from mediatr.
I use simple injector and have have used generic and specific decorators with MediatR without issue. Really gets rid of those cross cutting concerns like logging, metrics timing, transactions etc.
from mediatr.
A little late to the party :), but I'm using mediatr with AutoFac and generic decorators as well. Working like a charm. You can see a sample of it working here
from mediatr.
@maldworth Does this work with IAsyncNotification handlers as well? I'm trying to use this approach and can't get it working with notifications when I don't provide any decorators. If I include a decorator then my notifications handlers are called multiple times (twice in the case of adding a single decorator).
I've got a somewhat simpler implementation of your approach using this code:
private static void RegisterHandlers(ContainerBuilder builder, Assembly[] assemblies, Type handlerType, params Type[] decorators)
{
builder.RegisterAssemblyTypes(assemblies)
.As(t => t.GetInterfaces()
.Where(v => v.IsClosedTypeOf(handlerType))
.Select(v => new KeyedService(handlerType.Name, v)));
for (int i = 0; i < decorators.Length; i++)
{
RegisterGenericDecorator(
builder,
decorators[i],
handlerType,
i == 0 ? handlerType : decorators[i - 1],
i != decorators.Length - 1);
}
}
private static void RegisterGenericDecorator(
ContainerBuilder builder,
Type decoratorType,
Type decoratedServiceType,
Type fromKeyType,
bool hasKey)
{
var result = builder.RegisterGenericDecorator(
decoratorType,
decoratedServiceType,
fromKeyType.Name);
if (hasKey)
{
result.Keyed(decoratorType.Name, decoratedServiceType);
}
}
Then the code calling this looks like this:
RegisterHandlers(
builder,
assemblies,
typeof(IAsyncRequestHandler<,>),
typeof(AsyncValidatorHandler<,>),
typeof(AsyncCommandPipeline<,>),
typeof(AsyncAuthorizationHandler<,>));
RegisterHandlers(
builder,
assemblies,
typeof(IAsyncNotificationHandler<>));
However, in this case the notification handlers are never called. If I change that second statement to this instead:
RegisterHandlers(
builder,
assemblies,
typeof(IAsyncNotificationHandler<>),
typeof(AsyncNotificationPipeline<>));
Then each notification handler is called twice, instead of a single time.
I think it's more of a problem with Autofac than anything to do with MediatR, but I started heading in the right direction based on the code you shared, so thank you for that :)
FWIW, I'm using Mediatr-2.0.0-beta-005 at the moment.
from mediatr.
@sstorie The first statement will not work, and I can quickly explain why. When you start registering decorators it takes advantage of the Named/Keyed autofac feature. But when using this feature, that means when resolving a named service, you have to pass in the name. But when we wire up the Mediator resolver:
eg.
// Sets the delegate resolver factories for Mediatr.
// These factories are used by Mediatr to find the appropriate Handlers
builder.Register<SingleInstanceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => c.Resolve(t);
});
builder.Register<MultiInstanceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => (IEnumerable<object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(t));
});
You'll notice that we don't resolve with passing in a name, and that is intentional. So to get around this you'll notice that the helper method has this if statement:
if (hasKey)
{
result.Keyed(decoratorType.Name, decoratedServiceType);
}
Which means, only key this decorator if it is not the outer most decorator. So in the first statement, you only passed in the HandlerType, and no decorators. The HandlerType is always keyed in the helper method.
You can see I was doing this in an ugly way here, before I cleaned it up in a later commit. You'll notice that I commented the outermost decorator, and there is no toKey:
.
Now, why your second one is getting two notification calls? I'm not sure, because I just tested it out using your static helper methods (instead of mine) to register a single notification handler. The notification handler was called once as expected when using the second register statement you provided. I also made sure I was using beta-005.
from mediatr.
@maldworth Thanks for the response. I started to better understand how the decorators in Autofac work as I continued to debug this yesterday, and it makes sense to me now why my first attempt failed. Your explanation helped confirm that I'm understanding this now.
I still don't understand why I get notification handlers called multiple times, but perhaps it's a problem with my decorator itself. I'm not sure though because the decorator is also called twice, so it's like the container is giving the mediator those multiple instances or something. I'll keep digging :)
Thanks again for responding to my question.
from mediatr.
Related Issues (20)
- Can't get IPipelineBehavior to work in F# HOT 3
- I have problem with IRequestPreProcessor pipeline
- Logging any Exception thrown in a handler HOT 2
- Specify Lifetime in Handlers feature missing HOT 2
- Seperate Interfaces to Contract project to provide abstraction layer HOT 8
- Improve base package readme
- Implement IRequest, how to enter Behavior HOT 1
- IRequestExceptionHandler from other project not loaded
- Question:
- Question: How I can setup MediatR 12.1.0 in Ninject module? HOT 2
- How do I register INotification with generics? HOT 2
- long running INotificationHandler HOT 3
- Question: Can multiple MediatR handlers be packaged into a single class HOT 3
- .NET8 upgrade from .NET Core 2.1 : Command cannot be used as type parameter 'TRequest' for 'IRequestHandler<TRequest>' HOT 3
- Irequest in library and handler in web api project HOT 2
- what use net8 aot publish HOT 2
- v12.3.0 Breaking Change HOT 27
- Question: Why IRequestExceptionHandler is not catching? HOT 1
- Question: How to register generic requests HOT 8
- Can't register generic handlers in latest version HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mediatr.