using System; using System.Linq; using SimpleInjector; namespace Tapeti.SimpleInjector { public class SimpleInjectorDependencyResolver : IDependencyContainer { private readonly Container container; public SimpleInjectorDependencyResolver(Container container) { this.container = container; } public T Resolve() where T : class { return container.GetInstance(); } public object Resolve(Type type) { return container.GetInstance(type); } public void RegisterDefault() where TService : class where TImplementation : class, TService { if (CanRegisterDefault()) container.Register(); } public void RegisterDefault(Func factory) where TService : class { if (CanRegisterDefault()) container.Register(factory); } public void RegisterDefaultSingleton() where TService : class where TImplementation : class, TService { if (CanRegisterDefault()) container.RegisterSingleton(); } public void RegisterDefaultSingleton(TService instance) where TService : class { if (CanRegisterDefault()) container.RegisterInstance(instance); } public void RegisterDefaultSingleton(Func factory) where TService : class { if (CanRegisterDefault()) container.RegisterSingleton(factory); } public void RegisterController(Type type) { container.Register(type); } private bool CanRegisterDefault() where TService : class { // ReSharper disable once SimplifyLinqExpression - not a fan of negative predicates return !container.GetCurrentRegistrations().Any(ip => ip.ServiceType == typeof(TService)); } } }