GithubHelp home page GithubHelp logo

Are Decorators Supported? about mediatr HOT 8 CLOSED

jbogard avatar jbogard commented on August 16, 2024
Are Decorators Supported?

from mediatr.

Comments (8)

jbogard avatar jbogard commented on August 16, 2024

I don't know, are there?

from mediatr.

jbogard avatar jbogard commented on August 16, 2024

I use decorators all the time with StructureMap using MediatR if that's what you're asking.

from mediatr.

mpaul31 avatar mpaul31 commented on August 16, 2024

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.

bapti avatar bapti commented on August 16, 2024

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.

maldworth avatar maldworth commented on August 16, 2024

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.

sstorie avatar sstorie commented on August 16, 2024

@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.

maldworth avatar maldworth commented on August 16, 2024

@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.

sstorie avatar sstorie commented on August 16, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.