在本节我想与大家与分享一下,我所将要做的权限系统的架构和数据库的表的设计。请各位大神们对我项目中设计的不足之处进行指导,让我得以更好的写完它,留给需要它的人。
我的项目架构如下图所示:
如上图所示,在数据访问层,我采用了抽象工厂的方式,来对数据访问层和业务逻辑层解耦,当然如果你想更高大上一些,可以用第三方的框架,比如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(
新闻热点
疑难解答