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