using System; using System.Collections.Generic; using System.Linq; using System.Web; using Autofac; using Autofac.Core.Lifetime; using Autofac.Integration.Mvc; namespace PalGain.Core { public class ContainerManager { private readonly IContainer _container; public ContainerManager(IContainer container) { this._container = container; } public virtual IContainer Container { get { return _container; } } public virtual T Resolve(string key = "", ILifetimeScope scope = null) where T : class { if (scope == null) { //no scope specified scope = Scope(); } if (string.IsNullOrEmpty(key)) { return scope.Resolve(); } return scope.ResolveKeyed(key); } public virtual object Resolve(Type type, ILifetimeScope scope = null) { if (scope == null) { //no scope specified scope = Scope(); } return scope.Resolve(type); } public virtual T[] ResolveAll(string key = "", ILifetimeScope scope = null) { if (scope == null) { //no scope specified scope = Scope(); } if (string.IsNullOrEmpty(key)) { return scope.Resolve>().ToArray(); } return scope.ResolveKeyed>(key).ToArray(); } public virtual T ResolveUnregistered(ILifetimeScope scope = null) where T:class { return ResolveUnregistered(typeof(T), scope) as T; } public virtual object ResolveUnregistered(Type type, ILifetimeScope scope = null) { if (scope == null) { //no scope specified scope = Scope(); } var constructors = type.GetConstructors(); foreach (var constructor in constructors) { try { var parameters = constructor.GetParameters(); var parameterInstances = new List(); foreach (var parameter in parameters) { var service = Resolve(parameter.ParameterType, scope); if (service == null) throw new NopException("Unkown dependency"); parameterInstances.Add(service); } return Activator.CreateInstance(type, parameterInstances.ToArray()); } catch (NopException) { } } throw new NopException("No contructor was found that had all the dependencies satisfied."); } public virtual bool TryResolve(Type serviceType, ILifetimeScope scope, out object instance) { if (scope == null) { //no scope specified scope = Scope(); } return scope.TryResolve(serviceType, out instance); } public virtual bool IsRegistered(Type serviceType, ILifetimeScope scope = null) { if (scope == null) { //no scope specified scope = Scope(); } return scope.IsRegistered(serviceType); } public virtual object ResolveOptional(Type serviceType, ILifetimeScope scope = null) { if (scope == null) { //no scope specified scope = Scope(); } return scope.ResolveOptional(serviceType); } public virtual ILifetimeScope Scope() { try { if (HttpContext.Current != null) return AutofacDependencyResolver.Current.RequestLifetimeScope; //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks) return Container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag); } catch (Exception) { //we can get an exception here if RequestLifetimeScope is already disposed //for example, requested in or after "Application_EndRequest" handler //but note that usually it should never happen //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks) return Container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag); } } } }