1
0
mirror of synced 2024-11-14 14:33:49 +00:00
Tapeti/Tapeti.SimpleInjector/SimpleInjectorDependencyResolver.cs

49 lines
1.4 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using SimpleInjector;
namespace Tapeti.SimpleInjector
{
2016-12-11 14:08:58 +00:00
public class SimpleInjectorDependencyResolver : IDependencyInjector
{
private readonly Container container;
2016-12-11 14:08:58 +00:00
public SimpleInjectorDependencyResolver(Container container)
{
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-11 14:08:58 +00:00
return container.GetInstance(type);
}
2016-12-11 14:08:58 +00:00
public void RegisterDefault<TService, TImplementation>() where TService : class where TImplementation : class, TService
{
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-11 14:08:58 +00:00
public void RegisterPublisher(Func<IPublisher> publisher)
{
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-12-11 14:08:58 +00:00
public void RegisterController(Type type)
{
2016-12-11 14:08:58 +00:00
container.Register(type);
}
}
}