以前的好多代碼都丟失了,加上最近時間空一些,於是想起整理一下以前的個人半拉子項目,試試讓它們重生。自從養成了架構師視覺 搭建框架之后,越來 越看不上以前搭的框架了。先擼個上下文對象加上實現依賴注入。由於還是要依賴.net 4,所以像Autofac這樣的就用不了,於是仿照着實現了。
/// <summary> /// 自定義應用程序上下文對象 /// </summary> public class AppContextExt : IDisposable { /// <summary> /// app.config讀取 /// </summary> public Configuration AppConfig { get; set; } /// <summary> /// 真正的ApplicationContext對象 /// </summary> public ApplicationContext Application_Context { get; set; } //服務集合 public static Dictionary<Type, object> Services = new Dictionary<Type, object>(); //服務訂閱事件集合 public static Dictionary<Type, IList<Action<object>>> ServiceEvents = new Dictionary<Type, IList<Action<object>>>(); //上下文對象的單例 private static AppContextExt _ServiceContext = null; private readonly static object lockObj = new object(); /// <summary> /// 禁止外部進行實例化 /// </summary> private AppContextExt() { } /// <summary> /// 獲取唯一實例,雙鎖定防止多線程並發時重復創建實例 /// </summary> /// <returns></returns> public static AppContextExt GetInstance() { if (_ServiceContext == null) { lock (lockObj) { if (_ServiceContext == null) { _ServiceContext = new AppContextExt(); } } } return _ServiceContext; } /// <summary> /// 注入Service到上下文 /// </summary> /// <typeparam name="T">接口對象</typeparam> /// <param name="t">Service對象</param> /// <param name="servicesChangeEvent">服務實例更新時訂閱的消息</param> public static void RegisterService<T>(T t, Action<object> servicesChangeEvent = null) where T : class { if (t == null) { throw new Exception(string.Format("未將對象實例化,對象名:{0}.", typeof(T).Name)); } if (!Services.ContainsKey(typeof(T))) { try { Services.Add(typeof(T), t); if (servicesChangeEvent != null) { var eventList = new List<Action<object>>(); eventList.Add(servicesChangeEvent); ServiceEvents.Add(typeof(T), eventList); } } catch (Exception ex) { throw ex; } } if (!Services.ContainsKey(typeof(T))) { throw new Exception(string.Format("注冊Service失敗,對象名:{0}.", typeof(T).Name)); } } /// <summary> /// 動態注入dll中的多個服務對象 /// </summary> /// <param name="serviceRuntime"></param> public static void RegisterAssemblyServices(string serviceRuntime) { if (serviceRuntime.IndexOf(".dll") != -1 && !File.Exists(serviceRuntime)) throw new Exception(string.Format("類庫{0}不存在!", serviceRuntime)); try { Assembly asb = Assembly.LoadFrom(serviceRuntime); var serviceList = asb.GetTypes().Where(t => t.GetCustomAttributes(typeof(ExportAttribute), false).Any()).ToList(); if (serviceList != null && serviceList.Count > 0) { foreach (var service in serviceList) { var ifc = ((ExportAttribute)service.GetCustomAttributes(typeof(ExportAttribute), false).FirstOrDefault()).ContractType; //使用默認的構造函數實例化 var serviceObject = Activator.CreateInstance(service, null); if (serviceObject != null) Services.Add(ifc, serviceObject); else throw new Exception(string.Format("實例化對象{0}失敗!", service)); } } else { throw new Exception(string.Format("類庫{0}里沒有Export的Service!", serviceRuntime)); } } catch (Exception ex) { throw ex; } } /// <summary> /// 獲取Service的實例 /// </summary> /// <typeparam name="T">接口對象</typeparam> /// <returns></returns> public static T Resolve<T>() { if (Services.ContainsKey(typeof(T))) return (T)Services[typeof(T)]; return default(T); } /// <summary> /// 重置Service對象,實現熱更新 /// </summary> /// <typeparam name="T">接口對象</typeparam> /// <param name="t">新的服務對象實例</param> public static void ReLoadService<T>(T t) { if (t == null) { throw new Exception(string.Format("未將對象實例化,對象名:{0}.", typeof(T).Name)); } if (Services.ContainsKey(typeof(T))) { try { Services[typeof(T)] = t; if (ServiceEvents.ContainsKey(typeof(T))) { var eventList = ServiceEvents[typeof(T)]; foreach (var act in eventList) { act.Invoke(t); } } } catch (Exception ex) { throw ex; } } else if (!Services.ContainsKey(typeof(T))) { throw new Exception(string.Format("Service實例不存在!對象名:{0}.", typeof(T).Name)); } } /// <summary> /// 激活上下文 /// </summary> public void Start() { GetInstance(); } /// <summary> /// 激活上下文 /// </summary> /// <param name="appContext">真正的ApplicationContext對象</param> public void Start(ApplicationContext appContext) { Application_Context = appContext; GetInstance(); } /// <summary> /// 激活上下文 /// </summary> /// <param name="config">Configuration</param> public void Start(Configuration config) { AppConfig = config; GetInstance(); } /// <summary> /// 激活上下文 /// </summary> /// <param name="appContext">真正的ApplicationContext對象</param> /// <param name="config">Configuration</param> public void Start(ApplicationContext appContext, Configuration config) { AppConfig = config; Application_Context = appContext; GetInstance(); } /// <summary> /// Using支持 /// </summary> public void Dispose() { Services.Clear(); ServiceEvents.Clear(); if (Application_Context != null) { Application_Context.ExitThread(); } } }
使用:
AppContextExt.GetInstance().Start(); AppContextExt.RegisterAssemblyServices(AppDomain.CurrentDomain.BaseDirectory + "ModuleService.dll");
ILogService svr = AppContextExt.Resolve<ILogService>(); if (svr != null) svr.LogInfo("OK");
解決方案截圖: