1
0
mirror of synced 2024-11-22 01:13:49 +00:00

Added lifestyle parameters to SimpleInjectorDependencyResolver

This commit is contained in:
Mark van Renswoude 2018-12-28 15:34:58 +01:00
parent 1a5dcbf74d
commit 4ab6f67ea6

View File

@ -7,10 +7,14 @@ namespace Tapeti.SimpleInjector
public class SimpleInjectorDependencyResolver : IDependencyContainer public class SimpleInjectorDependencyResolver : IDependencyContainer
{ {
private readonly Container container; private readonly Container container;
private readonly Lifestyle defaultsLifestyle;
private readonly Lifestyle controllersLifestyle;
public SimpleInjectorDependencyResolver(Container container) public SimpleInjectorDependencyResolver(Container container, Lifestyle defaultsLifestyle = null, Lifestyle controllersLifestyle = null)
{ {
this.container = container; this.container = container;
this.defaultsLifestyle = defaultsLifestyle;
this.controllersLifestyle = controllersLifestyle;
} }
public T Resolve<T>() where T : class public T Resolve<T>() where T : class
@ -26,13 +30,23 @@ namespace Tapeti.SimpleInjector
public void RegisterDefault<TService, TImplementation>() where TService : class where TImplementation : class, TService public void RegisterDefault<TService, TImplementation>() where TService : class where TImplementation : class, TService
{ {
if (CanRegisterDefault<TService>()) if (!CanRegisterDefault<TService>())
return;
if (defaultsLifestyle != null)
container.Register<TService, TImplementation>(defaultsLifestyle);
else
container.Register<TService, TImplementation>(); container.Register<TService, TImplementation>();
} }
public void RegisterDefault<TService>(Func<TService> factory) where TService : class public void RegisterDefault<TService>(Func<TService> factory) where TService : class
{ {
if (CanRegisterDefault<TService>()) if (!CanRegisterDefault<TService>())
return;
if (defaultsLifestyle != null)
container.Register(factory, defaultsLifestyle);
else
container.Register(factory); container.Register(factory);
} }
@ -56,6 +70,9 @@ namespace Tapeti.SimpleInjector
public void RegisterController(Type type) public void RegisterController(Type type)
{ {
if (controllersLifestyle != null)
container.Register(type, type, controllersLifestyle);
else
container.Register(type); container.Register(type);
} }