1
0
mirror of synced 2024-11-16 06:43:51 +00:00
Tapeti/Tapeti.SimpleInjector/SimpleInjectorDependencyResolver.cs

70 lines
2.1 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using SimpleInjector;
namespace Tapeti.SimpleInjector
{
public class SimpleInjectorDependencyResolver : IDependencyContainer
{
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
{
if (CanRegisterDefault<TService>())
2016-12-11 14:08:58 +00:00
container.Register<TService, TImplementation>();
}
public void RegisterDefault<TService>(Func<TService> factory) where TService : class
{
if (CanRegisterDefault<TService>())
container.Register(factory);
}
public void RegisterDefaultSingleton<TService, TImplementation>() where TService : class where TImplementation : class, TService
{
if (CanRegisterDefault<TService>())
container.RegisterSingleton<TService, TImplementation>();
}
public void RegisterDefaultSingleton<TService>(TService instance) where TService : class
{
if (CanRegisterDefault<TService>())
container.RegisterSingleton(instance);
}
public void RegisterDefaultSingleton<TService>(Func<TService> factory) where TService : class
{
if (CanRegisterDefault<TService>())
container.RegisterSingleton(factory);
}
2016-12-11 14:08:58 +00:00
public void RegisterController(Type type)
{
2016-12-11 14:08:58 +00:00
container.Register(type);
}
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));
}
}
}