为了使用方便,对Autofac container的简单封装,记录如下,备以后用或分享给大家,欢迎讨论! 1 using Autofac; 2 using Autofac.Core.Lifetime; 3 using Autofac.Integration.Mvc; 4 5 p ...
为了使用方便,对Autofac container的简单封装,记录如下,备以后用或分享给大家,欢迎讨论!
1 using Autofac; 2 using Autofac.Core.Lifetime; 3 using Autofac.Integration.Mvc; 4 5 public static class ContainerManager 6 { 7 private static IContainer _container; 8 9 public static void SetContainer(IContainer container) 10 { 11 _container = container; 12 } 13 14 public static IContainer Container 15 { 16 get { return _container; } 17 } 18 19 public static T Resolve<T>(string key = "", ILifetimeScope scope = null) where T : class 20 { 21 if (scope == null) 22 { 23 //no scope specified 24 scope = Scope(); 25 } 26 if (string.IsNullOrEmpty(key)) 27 { 28 return scope.Resolve<T>(); 29 } 30 return scope.ResolveKeyed<T>(key); 31 } 32 33 public static object Resolve(Type type, ILifetimeScope scope = null) 34 { 35 if (scope == null) 36 { 37 //no scope specified 38 scope = Scope(); 39 } 40 return scope.Resolve(type); 41 } 42 43 public static T[] ResolveAll<T>(string key = "", ILifetimeScope scope = null) 44 { 45 if (scope == null) 46 { 47 //no scope specified 48 scope = Scope(); 49 } 50 if (string.IsNullOrEmpty(key)) 51 { 52 return scope.Resolve<IEnumerable<T>>().ToArray(); 53 } 54 return scope.ResolveKeyed<IEnumerable<T>>(key).ToArray(); 55 } 56 57 public static T ResolveUnregistered<T>(ILifetimeScope scope = null) where T : class 58 { 59 return ResolveUnregistered(typeof(T), scope) as T; 60 } 61 62 public static object ResolveUnregistered(Type type, ILifetimeScope scope = null) 63 { 64 if (scope == null) 65 { 66 //no scope specified 67 scope = Scope(); 68 } 69 var constructors = type.GetConstructors(); 70 foreach (var constructor in constructors) 71 { 72 try 73 { 74 var parameters = constructor.GetParameters(); 75 var parameterInstances = new List<object>(); 76 foreach (var parameter in parameters) 77 { 78 var service = Resolve(parameter.ParameterType, scope); 79 if (service == null) throw new ArgumentException("Unkown dependency"); 80 parameterInstances.Add(service); 81 } 82 return Activator.CreateInstance(type, parameterInstances.ToArray()); 83 } 84 catch (ArgumentException) 85 { 86 87 } 88 } 89 throw new ArgumentException("在所有的依赖项中,未找到合适的构造函数。"); 90 } 91 92 public static bool TryResolve(Type serviceType, ILifetimeScope scope, out object instance) 93 { 94 if (scope == null) 95 { 96 //no scope specified 97 scope = Scope(); 98 } 99 return scope.TryResolve(serviceType, out instance);100 }101 102 public static bool IsRegistered(Type serviceType, ILifetimeScope scope = null)103 {104 if (scope == null)105 {106 //no scope specified107 scope = Scope();108 }109 return scope.IsRegistered(serviceType);110 }111 112 public static object ResolveOptional(Type serviceType, ILifetimeScope scope = null)113 {114 if (scope == null)115 {116 //no scope specified117 scope = Scope();118 }119 return scope.ResolveOptional(serviceType);120 }121 122 public static ILifetimeScope Scope()123 {124 try125 {126 if (HttpContext.Current != null)127 return AutofacDependencyResolver.Current.RequestLifetimeScope;128 129 //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks)130 return Container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);131 }132 catch (Exception)133 {134 //we can get an exception here if RequestLifetimeScope is already disposed135 //for example, requested in or after "Application_EndRequest" handler136 //but note that usually it should never happen137 138 //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks)139 return Container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);140 }141 }142 }
原标题:Autofac Container 的简单的封装重构
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。