Code Monkey home page Code Monkey logo

Comments (8)

jbogard avatar jbogard commented on June 26, 2024

That's possible today in MediatR, provides your handler factory knows how
to create it. StructureMap for example can do that.

On Wednesday, February 17, 2016, Dan C [email protected] wrote:

Hi @jbogard https://github.com/jbogard ,

I was wondering if it's possible to extend this framework to handle
generic types in requests? Here's an example of what I was hoping to
achieve:

mediator.Send(new CreateItemCommand(item))

public class CreateItemCommand : IRequest where T:
ISomeInterface

(Apologies if this isn't the correct place to ask this question.)


Reply to this email directly or view it on GitHub
#69.

from mediatr.

dannyc02 avatar dannyc02 commented on June 26, 2024

Thanks for the speedy response @jbogard!

I've got this working by explicitly registering the in StructureMap, like so:

For<IRequestHandler<CreateItemCommand<Ping>, Ping>>().Use<CreateItemCommandHandler<Ping>>();

Is there a way to register this in StructureMap to handle all object types passed in? Something along these lines:

For<IRequestHandler<CreateItemCommand<T>, T>>().Use<CreateItemCommandHandler<T>>();

from mediatr.

khellang avatar khellang commented on June 26, 2024

From 6 year younger @jbogard: Advanced StructureMap: connecting implementations to open generic types 😉

from mediatr.

dannyc02 avatar dannyc02 commented on June 26, 2024

@khellang I'd attempted to use that method from that article without much luck :(
The article only has one 'level' deep of generics, like this:

public interface IHandler<TEvent>

where what I'm trying to achieve is this:

public interface IHandler<TEvent<TObjectType>>

Here's a snippet of what I've got in a single test - any help would be greatly appreciated.

[Test]
        public void ShouldResolveGenericHandler()
        {
            var container = new Container(cfg =>
            {
                cfg.Scan(scanner =>
                {
                    scanner.TheCallingAssembly();
                    scanner.IncludeNamespaceContainingType<Ping>();
                    scanner.WithDefaultConventions();
                    scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
                    scanner.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>));
                });
                cfg.For<SingleInstanceFactory>().Use<SingleInstanceFactory>(ctx => t => ctx.GetInstance(t));
                cfg.For<MultiInstanceFactory>().Use<MultiInstanceFactory>(ctx => t => ctx.GetAllInstances(t));
                cfg.For<IMediator>().Use<Mediator>();
            });

            var mediator = container.GetInstance<IMediator>();

            var response = mediator.Send(new CreateItemCommand<Ping>());

            response.Message.Should().NotBeNull();
        }


        public class CreateItemCommand<T> : IRequest<T>
        {

        }

        public class CreateItemCommandHandler<T> : IRequestHandler<CreateItemCommand<T>, T>
        {
            public T Handle(CreateItemCommand<T> message)
            {
                return default(T);
            }
        }

When looking at WhatDoIHave() - I can see the IRequestHandler in there:

IRequestHandler<CreateItemCommand<T>, T> MediatR Transient CreateItemCommandHandler<T> (Default)

However, when calling it, I get the following error:
----> StructureMap.StructureMapConfigurationException : No default Instance is registered and cannot be automatically determined for type 'IRequestHandler<CreateItemCommand<Ping>, Ping>'

Any ideas how to configure this with structuremap please @jbogard @khellang?

from mediatr.

jbogard avatar jbogard commented on June 26, 2024

I think you'll have to ask the StructureMap people on this one. But likely
you'll need to create a custom type scanner.

On Thursday, February 18, 2016, Dan C [email protected] wrote:

@khellang https://github.com/khellang I'd attempted to use that method
from that article without much luck :(
The article only has one 'level' deep of generics, like this:

public interface IHandler

where what I'm trying to achieve is this:

public interface IHandler<TEvent>

Here's a snippet of what I've got in a single test - any help would be
greatly appreciated.

[Test]
public void ShouldResolveGenericHandler()
{
var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.TheCallingAssembly();
scanner.IncludeNamespaceContainingType();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
scanner.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>));
});
cfg.For().Use(ctx => t => ctx.GetInstance(t));
cfg.For().Use(ctx => t => ctx.GetAllInstances(t));
cfg.For().Use();
});

        var mediator = container.GetInstance<IMediator>();

        var response = mediator.Send(new CreateItemCommand<Ping>());

        response.Message.Should().NotBeNull();
    }


    public class CreateItemCommand<T> : IRequest<T>
    {

    }

    public class CreateItemCommandHandler<T> : IRequestHandler<CreateItemCommand<T>, T>
    {
        public T Handle(CreateItemCommand<T> message)
        {
            return default(T);
        }
    }

When looking at WhatDoIHave() - I can see the IRequestHandler in there:

IRequestHandler<CreateItemCommand, T> MediatR Transient
CreateItemCommandHandler (Default)

However, when calling it, I get the following error:
----> StructureMap.StructureMapConfigurationException : No default
Instance is registered and cannot be automatically determined for type
'IRequestHandler<CreateItemCommand, Ping>'

Any ideas how to configure this with structuremap please @jbogard
https://github.com/jbogard @khellang https://github.com/khellang?


Reply to this email directly or view it on GitHub
#69 (comment).

from mediatr.

dannyc02 avatar dannyc02 commented on June 26, 2024

Ok thanks @jbogard.

Will report back findings.

from mediatr.

jeremydmiller avatar jeremydmiller commented on June 26, 2024

@dannyc02 See this http://structuremap.github.io/generics/ and this http://structuremap.github.io/registration/auto-registration-and-conventions/. I'd urge you to be cautious about doing this though. You could easily get into a situation where the team just isn't going to be able to unravel the very fancy generics auto-registrations.

from mediatr.

dannyc02 avatar dannyc02 commented on June 26, 2024

@jeremydmiller thanks for the info, really helped.

I managed to get it working using a custom convention, gist here: https://gist.github.com/dannyc02/6d42b90154d8478dc6fd

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.