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
|
|
|
|
|
{
|
2017-01-31 12:01:08 +01:00
|
|
|
|
public class SimpleInjectorDependencyResolver : IDependencyContainer
|
2016-11-20 14:34:50 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly Container container;
|
|
|
|
|
|
2016-12-11 15:08:58 +01:00
|
|
|
|
public SimpleInjectorDependencyResolver(Container container)
|
2016-11-20 14:34:50 +01:00
|
|
|
|
{
|
|
|
|
|
this.container = container;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T Resolve<T>() where T : class
|
|
|
|
|
{
|
|
|
|
|
return container.GetInstance<T>();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2017-02-05 23:22:34 +01:00
|
|
|
|
if (CanRegisterDefault<TService>())
|
2016-12-11 15:08:58 +01:00
|
|
|
|
container.Register<TService, TImplementation>();
|
2016-12-05 08:00:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2017-02-05 23:22:34 +01:00
|
|
|
|
if (CanRegisterDefault<TService>())
|
|
|
|
|
container.Register(factory);
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2016-12-11 15:08:58 +01:00
|
|
|
|
public void RegisterController(Type type)
|
2016-11-20 14:34:50 +01:00
|
|
|
|
{
|
2016-12-11 15:08:58 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|