ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

register delegate ServiceFactory MediatR #250

Closed mdissel closed 4 years ago

mdissel commented 4 years ago

How can I register required delegate ServiceFactory defined in the popular MediatR package?

public delegate object ServiceFactory(Type serviceType);

For now I'm registering the necessary types for MediatR as:

c.Export(typeof(RequestPreProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>));
c.Export(typeof(RequestPostProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>));
c.Export(typeof(RequestExceptionActionProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>));
c.Export(typeof(RequestExceptionProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>));
c.Export<Mediator>().As(typeof(IMediator)).Lifestyle.Singleton();

c.ExportAssemblies(assemblies).ByInterface(typeof(IRequestHandler<,>));
c.ExportAssemblies(assemblies).ByInterface(typeof(INotificationHandler<>));
c.ExportAssemblies(assemblies).ByInterface(typeof(IRequestPreProcessor<>));
c.ExportAssemblies(assemblies).ByInterface(typeof(IRequestPostProcessor<,>));
c.ExportAssemblies(assemblies).ByInterface(typeof(IRequestExceptionHandler<,,>));
c.ExportAssemblies(assemblies).ByInterface(typeof(IRequestExceptionAction<,>));

Thanks

ipjohnson commented 4 years ago

Give this a whirl

c.ExportFunc<ServiceFactory>(scope => scope.Locate);