在本節我想與大家與分享一下,我所將要做的權限系統的架構和數據庫的表的設計。請各位大神們對我項目中設計的不足之處進行指導,讓我得以更好的寫完它,留給需要它的人。
我的項目架構如下圖所示:
如上圖所示,在數據訪問層,我采用了抽象工廠的方式,來對數據訪問層和業務邏輯層解耦,當然如果你想更高大上一些,可以用第三方的框架,比如SPRing.Net ,Autofac來實現。解耦的好處在于可以方便的切換數據庫,當數據庫變更時,只需換一下對應的數據訪問DAL就行,本系列中,我所采用的是SQLServer。寫到這我想在如下這個大數據時代,MongoDB其實也是不錯的。后面有機會,可以開一個MongoDB的專題和大家一起來使用一下MongoDB學習一下它。對于抽象工廠來實現業務邏輯層與數據訪問層的解耦實現代碼如下,主要用到了反射,面向接口編程。
配置:
<appSettings> <add key="DAL" value="MCFramework.SQLDAL"/> <!--頁容量--> <add key="PageSize" value="20"/></appSettings>View Code
抽象工廠:
namespace MCFramework.RepositoryFactory{ public class RepositoryFactory { public RepositoryFactory() { } private static readonly string AssemblyPath =ConfigurationSettings.AppSettings["DAL"]; #region CreateObject //不使用緩存 private static object CreateObjectNoCache(string AssemblyPath, string classNamespace) { try { object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace); return objType; } catch(System.Exception ex) { string str=ex.StackTrace;// 記錄錯誤日志 return null; } } //使用緩存 //private static object CreateObject(string AssemblyPath, string classNamespace) //{ // object objType = DataCache.GetCache(classNamespace); // if (objType == null) // { // try // { // objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace); // DataCache.SetCache(classNamespace, objType);// 寫入緩存 // } // catch//(System.Exception ex) // { // //string str=ex.Message;// 記錄錯誤日志 // } // } // return objType; //} #endregion /// <summary> /// 用戶倉儲 /// </summary> public static ISystem_EmployeeRepository System_EmployeeRepository { get { return (ISystem_EmployeeRepository)CreateObjectNoCache(AssemblyPath, AssemblyPath + ".System_EmployeeRepository"); } } /// <summary> ///菜單倉儲 /// </summary> public static ISystem_MenuRepository System_MenuRepository { get { return (ISystem_MenuRepository)CreateObjectNoCache(AssemblyPath, AssemblyPath + ".System_MenuRepository"); } } /// <summary> ///角色倉儲 /// </summary> public static ISystem_RoleRepository System_RoleRepository { get { return (ISystem_RoleRepository)CreateObjectNoCache(AssemblyPath, AssemblyPath + ".System_RoleRepository"); } } /// <summary> ///按鈕倉儲 /// </summary> public static ISystem_ButtonRepository System_ButtonRepository { get { return (ISystem_ButtonRepository)CreateObjectNoCache(AssemblyPath, AssemblyPath + ".System_ButtonRepository"); } } }}View Code
所有的訪問數據庫的操作都用接口來約束:
namespace MCFramework.IDAL{ public interface IBaseRepository<T> where T:class { int Add(T model); int UpdateDel(string ids, bool isDel); int Del(string ids); int Update(T model); DataSet GetListByProc(string procName, System.Data.SqlClient.SqlParameter[] paras); DataSet GetModel(string Id); DataSet GetList(string strWhere); }}View Code
namespace MCFramework.IDAL{ public interface ISystem_ButtonRepository:IBaseRepository<System_ButtonModel> { bool IsExit(string ButtonCode); }}View Code
接口的實現:
namespace MCFramework.SQLDAL{ /// <summary> /// Author: MaChun /// Description: DALTier -- the DAL class of System_Button. /// Datetime:2015/6/8 13:00:35 /// </summary> public class BaseSystem_ButtonRepository: IBaseRepository<System_ButtonModel> { //創建企業庫連接 public SqlDataaccess db = SqlDataAccess.CreateDataAccess(); #region 新增一條記錄 Add(System_ButtonModel model) /// <summary> /// 新增一條記錄 /// </summary> public int Add(System_ButtonModel model) { int result = 0; try { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into System_Button("); strSql.Append("SBT_Guid,SBT_ButtonCode,SBT_ButtonName,SBT_IconUrl,SBT_IconCSS,SBT_CreateBy,SBT_CreatedDate)"); strSql.Append(" values ("); strSql.Append("@SBT_Guid,@SBT_ButtonCode,@SBT_ButtonName,@SBT_IconUrl,@SBT_IconCss,@SBT_CreateBy,@SBT_CreatedDate)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@SBT_Guid", SqlDbType.VarChar,36), new SqlParameter("@SBT_ButtonCode", SqlDbType.VarChar,200), new SqlParameter("@SBT_ButtonName", SqlDbType.VarChar,100), new SqlParameter("@SBT_IconUrl", SqlDbType.VarChar,100), new SqlParameter("@SBT_IconCss", SqlDbType.VarChar,100), new SqlParameter("@SBT_CreateBy", SqlDbType.VarChar,100), new SqlParameter("@SBT_CreatedDate", SqlDbType.DateTime,8)}; parameters[0].Value = model.SBTGuid; parameters[1].Value = model.SBTButtonCode; parameters[2].Value = model.SBTButtonName; parameters[3].Value = model.SBTIconUrl; parameters[4].Value = model.SBTIconCss; parameters[5].Value = model.SBTCreateBy; parameters[6].Value = model.SBTCreatedDate; result = db.ExecuteNonQuery(strSql.ToString(), parameters); } catch (Exception ex) { throw ex; } return result; } #endregion #region 邏輯刪除 UpdateDel(
新聞熱點
疑難解答