简要:
控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心。
经典的IOC框架AutoFac:
在此前已经写过一篇对Autofac的介绍和使用,如果想了解可以去看一下.连接如下:
自己写个类Utility:
dbUtilitys是存储容器,通过表名可以获取相应的IDataBase对象实例.
namespace YZR.Core{ using YZR.Data; ////// DbUtility IOC By YZR /// public class RUtility { private DictionarydbUtilitys;//存储Db键值对 public static string DefaultDbUtilityName;//Db默认key值 private static RUtility instance;//实例 public static RUtility Instance { get { return RUtility.instance; } set { RUtility.instance = value; } } static RUtility() { DefaultDbUtilityName = "default"; instance = new RUtility(); }//只执行一次的操作,初始化RUtility public RUtility() { this.dbUtilitys = new Dictionary (); }//得到Db键值对 public IDataBase GetDbUtility(string name) { if (this.dbUtilitys[name]==null) { throw new Exception("表名映射实体出错"); } return this.dbUtilitys[name]; }//根据key获取DbUtility public void RegistDbUtility(string name,IDataBase dbUtility) { this.dbUtilitys[name] = dbUtility; }//往Db键值对注册 public IDataBase DefaultDbUtility { get { return this.dbUtilitys[DefaultDbUtilityName]; } }//获取默认DbUtility }}
一个管理者类,实现往容器注册实例的方法RegistDbUtility();
namespace YZR.Core{ using YZR.Data; public static class DbUtilityManager { public static RUtility getInstance()//暂时无用 { return RUtility.Instance; } ////// 注册实体实例 /// /// /// public static void RegistDbUtility(string name, object dbUtility) { RUtility.Instance.RegistDbUtility(name, dbUtility as IDataBase); } }}
在应用程序一开始的时候实现注入:
mvc:
using System.Reflection; using YZR.Config; using YZR.Core; using YZR.Entity; public class RUtilityConfig { ////// IOC By YZR /// public static void Register() { string Title = WebSettingManager.WebSetting.BaseSetting.title + " - " + WebSettingManager.WebSetting.BaseSetting.subTitle; int count = WebSettingManager.WebSetting.StrConnSetting.Count; Dictionarydic = new Dictionary (); for (int i = 0; i < count; i++) { connSection section = WebSettingManager.WebSetting.StrConnSetting[i]; dic.Add(section.Rname, section.Rvalue); } //string strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["default"].ConnectionString; //string connString = System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ToString(); string ProviderName = System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ProviderName; string path = AppDomain.CurrentDomain.BaseDirectory; Assembly ass = Assembly.LoadFrom(path + "bin\\YZR.Data.dll"); //Assembly ass = Assembly.Load("YZR.Entity"); //TODO:Oracle数据库使用RAtion,Sqlserver使用RMotion Type type = null; string value = dic["Oracle"]; if (value.Equals(ProviderName, StringComparison.OrdinalIgnoreCase)) { type = ass.GetType("YZR.Data." + "RAction"); } else { value = dic["Sqlserver"]; if (value.Equals(ProviderName, StringComparison.OrdinalIgnoreCase)) { type = ass.GetType("YZR.Data." + "RMotion"); } else throw new Exception("配置节点出错"); } //ConstructorInfo c = type.GetConstructor(new Type[] { typeof(string) });//构造函数的实参类型 //c.Invoke(new object[] { "SYS_ADMIN" });//实参 //TODO:在这里需要将所有Entity注册进DbUtilityManager中 if (type != null) { Type tableType = typeof(TableNames); string[] tableNames = tableType.GetEnumNames(); for (int i = 0; i < tableNames.Length; i++) { object fobj = Activator.CreateInstance(type, new object[] { tableNames[i] }); DbUtilityManager.RegistDbUtility(tableNames[i], fobj); } } } }
webform:
using YZR.Config; public static class AppStart { ////// 应用程序入口 By YZR /// public static void Init() { //............//TODO:在这里需要将所有Entity注册进DbUtilityManager中 if (type != null) { Type tableType = typeof(TableNames); string[] tableNames = tableType.GetEnumNames(); for (int i = 0; i < tableNames.Length; i++) { object fobj = Activator.CreateInstance(type, new object[] { tableNames[i] }); DbUtilityManager.RegistDbUtility(tableNames[i], fobj); } } } }
那么接下来,业务逻辑层中只需要一句代码即可获取相应的实例对象:
如果需要操作另一个实例,也只是需要一句代码:
Note:RUtlilty能够兼容数据库.