2016-12-05 08:00:09 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2016-11-20 14:34:50 +01:00
|
|
|
|
using SimpleInjector;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.SimpleInjector
|
|
|
|
|
{
|
2019-08-14 12:20:53 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dependency resolver and container implementation for SimpleInjector.
|
|
|
|
|
/// </summary>
|
2017-01-31 12:01:08 +01:00
|
|
|
|
public class SimpleInjectorDependencyResolver : IDependencyContainer
|
2016-11-20 14:34:50 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly Container container;
|
2022-11-23 09:13:38 +01:00
|
|
|
|
private readonly Lifestyle? defaultsLifestyle;
|
|
|
|
|
private readonly Lifestyle? controllersLifestyle;
|
2016-11-20 14:34:50 +01:00
|
|
|
|
|
2021-05-29 21:51:58 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// </summary>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
public SimpleInjectorDependencyResolver(Container container, Lifestyle? defaultsLifestyle = null, Lifestyle? controllersLifestyle = null)
|
2016-11-20 14:34:50 +01:00
|
|
|
|
{
|
|
|
|
|
this.container = container;
|
2018-12-28 15:34:58 +01:00
|
|
|
|
this.defaultsLifestyle = defaultsLifestyle;
|
|
|
|
|
this.controllersLifestyle = controllersLifestyle;
|
2016-11-20 14:34:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 12:20:53 +02:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2016-11-20 14:34:50 +01:00
|
|
|
|
public T Resolve<T>() where T : class
|
|
|
|
|
{
|
|
|
|
|
return container.GetInstance<T>();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 12:20:53 +02:00
|
|
|
|
/// <inheritdoc />
|
2016-12-11 15:08:58 +01:00
|
|
|
|
public object Resolve(Type type)
|
2016-12-05 08:00:09 +01:00
|
|
|
|
{
|
2016-12-11 15:08:58 +01:00
|
|
|
|
return container.GetInstance(type);
|
2016-12-05 08:00:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-14 12:20:53 +02:00
|
|
|
|
/// <inheritdoc />
|
2016-12-11 15:08:58 +01:00
|
|
|
|
public void RegisterDefault<TService, TImplementation>() where TService : class where TImplementation : class, TService
|
2016-12-05 08:00:09 +01:00
|
|
|
|
{
|
2018-12-28 15:34:58 +01:00
|
|
|
|
if (!CanRegisterDefault<TService>())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (defaultsLifestyle != null)
|
|
|
|
|
container.Register<TService, TImplementation>(defaultsLifestyle);
|
|
|
|
|
else
|
2016-12-11 15:08:58 +01:00
|
|
|
|
container.Register<TService, TImplementation>();
|
2016-12-05 08:00:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 12:20:53 +02:00
|
|
|
|
/// <inheritdoc />
|
2017-02-05 23:22:34 +01:00
|
|
|
|
public void RegisterDefault<TService>(Func<TService> factory) where TService : class
|
2017-01-31 12:01:08 +01:00
|
|
|
|
{
|
2018-12-28 15:34:58 +01:00
|
|
|
|
if (!CanRegisterDefault<TService>())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (defaultsLifestyle != null)
|
|
|
|
|
container.Register(factory, defaultsLifestyle);
|
|
|
|
|
else
|
2017-02-05 23:22:34 +01:00
|
|
|
|
container.Register(factory);
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 12:20:53 +02:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2017-02-05 23:22:34 +01:00
|
|
|
|
public void RegisterDefaultSingleton<TService, TImplementation>() where TService : class where TImplementation : class, TService
|
|
|
|
|
{
|
|
|
|
|
if (CanRegisterDefault<TService>())
|
|
|
|
|
container.RegisterSingleton<TService, TImplementation>();
|
|
|
|
|
}
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2019-08-14 12:20:53 +02:00
|
|
|
|
/// <inheritdoc />
|
2017-02-05 23:22:34 +01:00
|
|
|
|
public void RegisterDefaultSingleton<TService>(TService instance) where TService : class
|
2016-11-20 14:34:50 +01:00
|
|
|
|
{
|
2017-02-05 23:22:34 +01:00
|
|
|
|
if (CanRegisterDefault<TService>())
|
2018-06-11 09:59:16 +02:00
|
|
|
|
container.RegisterInstance(instance);
|
2016-11-20 14:34:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 12:20:53 +02:00
|
|
|
|
/// <inheritdoc />
|
2017-02-05 23:22:34 +01:00
|
|
|
|
public void RegisterDefaultSingleton<TService>(Func<TService> factory) where TService : class
|
|
|
|
|
{
|
|
|
|
|
if (CanRegisterDefault<TService>())
|
|
|
|
|
container.RegisterSingleton(factory);
|
|
|
|
|
}
|
2016-11-20 14:34:50 +01:00
|
|
|
|
|
2019-08-14 12:20:53 +02:00
|
|
|
|
/// <inheritdoc />
|
2016-12-11 15:08:58 +01:00
|
|
|
|
public void RegisterController(Type type)
|
2016-11-20 14:34:50 +01:00
|
|
|
|
{
|
2018-12-28 15:34:58 +01:00
|
|
|
|
if (controllersLifestyle != null)
|
|
|
|
|
container.Register(type, type, controllersLifestyle);
|
|
|
|
|
else
|
|
|
|
|
container.Register(type);
|
2016-11-20 14:34:50 +01:00
|
|
|
|
}
|
2017-02-05 23:22:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private bool CanRegisterDefault<TService>() where TService : class
|
|
|
|
|
{
|
|
|
|
|
// ReSharper disable once SimplifyLinqExpression - not a fan of negative predicates
|
|
|
|
|
return !container.GetCurrentRegistrations().Any(ip => ip.ServiceType == typeof(TService));
|
|
|
|
|
}
|
2016-11-20 14:34:50 +01:00
|
|
|
|
}
|
|
|
|
|
}
|