using Autofac; using IService; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using TT.Service.Interface; using TT.Service.Realize; namespace InjectionServer { public class Dependency { public static Assembly Assemblys { set; get; } = Assembly.Load("TT.Service"); static Dependency() { if (Builder == null) { Builder = new ContainerBuilder();//初始化容器 Builder.RegisterAssemblyTypes(Assemblys) .Where(o => o != typeof(IRepository<>)) .AsImplementedInterfaces().PropertiesAutowired().InstancePerDependency(); InstanceContainer = Builder.Build(); } } public Dependency() { var type = this.GetType(); foreach (var item in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { try { string key = "TT.Service.Interface." + $"I{item.Name.Replace("Example", "")}"; Type t = Assemblys.GetType(key); var method = type.GetMethod("GetInstance", BindingFlags.Static | BindingFlags.NonPublic) .MakeGenericMethod(t); item.SetValue(this, method.Invoke(null, null)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } /// <summary> /// 取出對象管理 /// </summary> internal static IContainer InstanceContainer { get; set; } /// <summary> /// 注冊對象管理 /// </summary> internal static ContainerBuilder Builder { get; set; } internal static T GetInstance<T>() where T : class { var instance = InstanceContainer.Resolve<T>(); return instance; } internal static void Register<T, K>() where T : class where K : class { Builder.RegisterType<T>().As<K>().PropertiesAutowired().InstancePerDependency(); } public IUsers UsersExample { set; get; } public ITest TestExample { set; get; } } }
之前的代碼
1 using Autofac; 2 using IService; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Reflection; 7 using System.Text; 8 using System.Threading.Tasks; 9 using TT.Service.Interface; 10 using TT.Service.Realize; 11 12 namespace InjectionServer 13 { 14 public class Dependency 15 { 16 static Dependency() 17 { 18 if (Builder == null) 19 { 20 Builder = new ContainerBuilder(); 21 Register(); 22 InstanceContainer = Builder.Build(); 23 } 24 } 25 26 #region 容器管理 27 /// <summary> 28 /// 取出對象管理 29 /// </summary> 30 internal static IContainer InstanceContainer { get; set; } 31 32 /// <summary> 33 /// 注冊對象管理 34 /// </summary> 35 internal static ContainerBuilder Builder { get; set; } 36 37 /// <summary> 38 /// 獲取指定接口的實例 39 /// </summary> 40 /// <typeparam name="T"></typeparam> 41 /// <returns></returns> 42 internal static T GetInstance<T>() 43 where T : class 44 { 45 var instance = InstanceContainer.Resolve<T>(); 46 return instance; 47 } 48 49 /// <summary> 50 /// 注冊並且設置為在一個生命周期域中,每一個依賴或調用創建一個單一的共享的實例,且每一個不同的生命周期域,實例是唯一的,不共享的。 51 /// </summary> 52 /// <typeparam name="T"></typeparam> 53 /// <typeparam name="K"></typeparam> 54 /// <param name="t"></param> 55 /// <param name="k"></param> 56 internal static void Register<T, K>() 57 where T : class 58 where K : class 59 { 60 Builder.RegisterType<T>().As<K>().PropertiesAutowired().InstancePerDependency(); 61 } 62 #endregion 63 64 #region 容器注冊 65 internal static void Register() 66 { 67 68 69 Register<UserService, IUserService>(); 70 //Register<UserService, IUserService>(); 71 //Register<UserService, IUserService>(); 72 73 } 74 #endregion 75 76 public IUsers UsersExample { get { return GetInstance<IUsersService>(); } } 77 78 79 } 80 }