using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; using LightInject; namespace EveOPreview { // Adapts LighInject to the generic IoC interface sealed class LightInjectContainer : IIocContainer { private readonly ServiceContainer _container; public LightInjectContainer() { this._container = new ServiceContainer(ContainerOptions.Default); } public bool IsRegistered() { return this._container.CanGetInstance(typeof(TService), ""); } public void Register(Type serviceType, Assembly container) { if (!serviceType.IsInterface) { this._container.Register(serviceType, new PerContainerLifetime()); return; } if (serviceType.IsInterface && serviceType.IsGenericType) { this._container.RegisterAssembly( container, (st, it) => st.IsConstructedGenericType && st.GetGenericTypeDefinition() == serviceType); } else { foreach (TypeInfo implementationType in container.DefinedTypes) { if (!implementationType.IsClass || implementationType.IsAbstract) { continue; } if (serviceType.IsAssignableFrom(implementationType)) { this._container.Register(serviceType, implementationType, new PerContainerLifetime()); } } } } public void Register() { this.Register(typeof(TService), typeof(TService).Assembly); } public void Register() where TImplementation : TService { this._container.Register(); } public void Register(Expression> factory) { this._container.Register(f => factory); } public void Register(Expression> factory) { this._container.Register(f => factory); } public void RegisterInstance(TService instance) { this._container.RegisterInstance(instance); } public TService Resolve() { return this._container.GetInstance(); } public IEnumerable ResolveAll() { return this._container.GetAllInstances(); } public object Resolve(Type serviceType) { return this._container.GetInstance(serviceType); } public IEnumerable ResolveAll(Type serviceType) { return this._container.GetAllInstances(serviceType); } } }