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
|
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
public class SimpleInjectorDependencyResolver : IDependencyInjector
|
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
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
// ReSharper disable once SimplifyLinqExpression - not a fan of negative predicates
|
|
|
|
|
if (!container.GetCurrentRegistrations().Any(ip => ip.ServiceType == typeof(TService)))
|
|
|
|
|
container.Register<TService, TImplementation>();
|
2016-12-05 07:00:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-12-11 14:08:58 +00:00
|
|
|
|
public void RegisterPublisher(Func<IPublisher> publisher)
|
2016-11-20 13:34:50 +00:00
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
// ReSharper disable once SimplifyLinqExpression - still not a fan of negative predicates
|
|
|
|
|
if (!container.GetCurrentRegistrations().Any(ip => ip.ServiceType == typeof(IPublisher)))
|
|
|
|
|
container.Register(publisher);
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|