Comments (8)
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.
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.
From 6 year younger @jbogard: Advanced StructureMap: connecting implementations to open generic types 😉
from mediatr.
@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.
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.
Ok thanks @jbogard.
Will report back findings.
from mediatr.
@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.
@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)
- IPipelineBehavior<TRequest, TResponse> does not work in Android, but works in Windows HOT 5
- Would you consider a PR for a Roslyn component? HOT 1
- IPipelineBehavior not being executed when I have custom interface HOT 2
- [Question] DbContextPooling issue with INotifications when publish is executed HOT 4
- 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
- .NET 6 Target Dependencies HOT 4
- docs: I'm able to edit the wiki HOT 3
- .NET 8 Calling IPublisher.Publish method throws ObjectDisposedException for IServiceProvider HOT 1
- "No service for type 'MediatR.IRequestHandler' has been registered." issue HOT 8
- Can't register MediatR in WorkerService project HOT 1
- Help registering pipeline behavior HOT 2
- Allow for an optional "Order" parameter to be set on implementations of pre, post, and exception handlers. HOT 5
- Issue : Pipelines does not get invoked when command has a mapping profile sub class HOT 1
- MediatR one notification handler HOT 8
- Issues with MediatR on .Net 8 and AWS Lambda HOT 2
- Order of execution of RequestPreProcessorBehavior changed with explicit processor registration? HOT 2
- IoC container issue with .NET 8 & minimal api HOT 3
- Add assembly with handlers without ServiceCollection HOT 3
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.