2016-12-05 07:00:09 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
using SimpleInjector;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.SimpleInjector
|
|
|
|
|
{
|
2019-08-14 10:20:53 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dependency resolver and container implementation for SimpleInjector.
|
|
|
|
|
/// </summary>
|
2017-01-31 11:01:08 +00:00
|
|
|
|
public class SimpleInjectorDependencyResolver : IDependencyContainer
|
2016-11-20 13:34:50 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly Container container;
|
2018-12-28 14:34:58 +00:00
|
|
|
|
private readonly Lifestyle defaultsLifestyle;
|
|
|
|
|
private readonly Lifestyle controllersLifestyle;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
2021-05-29 19:51:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// </summary>
|
2018-12-28 14:34:58 +00:00
|
|
|
|
public SimpleInjectorDependencyResolver(Container container, Lifestyle defaultsLifestyle = null, Lifestyle controllersLifestyle = null)
|
2016-11-20 13:34:50 +00:00
|
|
|
|
{
|
|
|
|
|
this.container = container;
|
2018-12-28 14:34:58 +00:00
|
|
|
|
this.defaultsLifestyle = defaultsLifestyle;
|
|
|
|
|
this.controllersLifestyle = controllersLifestyle;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:20:53 +00:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2016-11-20 13:34:50 +00:00
|
|
|
|
public T Resolve<T>() where T : class
|
|
|
|
|
{
|
|
|
|
|
return container.GetInstance<T>();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:20:53 +00:00
|
|
|
|
/// <inheritdoc />
|
2016-12-11 14:08:58 +00:00
|
|
|
|
public object Resolve(Type type)
|
2016-12-05 07:00:09 +00:00
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
return container.GetInstance(type);
|
2016-12-05 07:00:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-14 10:20:53 +00:00
|
|
|
|
/// <inheritdoc />
|
2016-12-11 14:08:58 +00:00
|
|
|
|
public void RegisterDefault<TService, TImplementation>() where TService : class where TImplementation : class, TService
|
2016-12-05 07:00:09 +00:00
|
|
|
|
{
|
2018-12-28 14:34:58 +00:00
|
|
|
|
if (!CanRegisterDefault<TService>())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (defaultsLifestyle != null)
|
|
|
|
|
container.Register<TService, TImplementation>(defaultsLifestyle);
|
|
|
|
|
else
|
2016-12-11 14:08:58 +00:00
|
|
|
|
container.Register<TService, TImplementation>();
|
2016-12-05 07:00:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:20:53 +00:00
|
|
|
|
/// <inheritdoc />
|
2017-02-05 22:22:34 +00:00
|
|
|
|
public void RegisterDefault<TService>(Func<TService> factory) where TService : class
|
2017-01-31 11:01:08 +00:00
|
|
|
|
{
|
2018-12-28 14:34:58 +00:00
|
|
|
|
if (!CanRegisterDefault<TService>())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (defaultsLifestyle != null)
|
|
|
|
|
container.Register(factory, defaultsLifestyle);
|
|
|
|
|
else
|
2017-02-05 22:22:34 +00:00
|
|
|
|
container.Register(factory);
|
2017-01-31 11:01:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:20:53 +00:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2017-02-05 22:22:34 +00:00
|
|
|
|
public void RegisterDefaultSingleton<TService, TImplementation>() where TService : class where TImplementation : class, TService
|
|
|
|
|
{
|
|
|
|
|
if (CanRegisterDefault<TService>())
|
|
|
|
|
container.RegisterSingleton<TService, TImplementation>();
|
|
|
|
|
}
|
2017-01-31 11:01:08 +00:00
|
|
|
|
|
2019-08-14 10:20:53 +00:00
|
|
|
|
/// <inheritdoc />
|
2017-02-05 22:22:34 +00:00
|
|
|
|
public void RegisterDefaultSingleton<TService>(TService instance) where TService : class
|
2016-11-20 13:34:50 +00:00
|
|
|
|
{
|
2017-02-05 22:22:34 +00:00
|
|
|
|
if (CanRegisterDefault<TService>())
|
2018-06-11 07:59:16 +00:00
|
|
|
|
container.RegisterInstance(instance);
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:20:53 +00:00
|
|
|
|
/// <inheritdoc />
|
2017-02-05 22:22:34 +00:00
|
|
|
|
public void RegisterDefaultSingleton<TService>(Func<TService> factory) where TService : class
|
|
|
|
|
{
|
|
|
|
|
if (CanRegisterDefault<TService>())
|
|
|
|
|
container.RegisterSingleton(factory);
|
|
|
|
|
}
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
2019-08-14 10:20:53 +00:00
|
|
|
|
/// <inheritdoc />
|
2016-12-11 14:08:58 +00:00
|
|
|
|
public void RegisterController(Type type)
|
2016-11-20 13:34:50 +00:00
|
|
|
|
{
|
2018-12-28 14:34:58 +00:00
|
|
|
|
if (controllersLifestyle != null)
|
|
|
|
|
container.Register(type, type, controllersLifestyle);
|
|
|
|
|
else
|
|
|
|
|
container.Register(type);
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
2017-02-05 22:22:34 +00: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 13:34:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|