在本系列教程中,我們以一個大型CMS系統的完整開發流程為例,和大家一起探討net開發的經驗和教訓。在本程序中,我們采用了流行的三層/N層框架+倉儲模式的架構模式。項目分層示意圖:


各層的主要用途:
- EasyFast.Web ——UI展示層,系統的操作界面。
- EasyFast.BLL ——業務邏輯層,用於處理程序中的業務邏輯。
- EasyFast.Model ——用於在各層之間傳遞數據。
- EasyFast.Utility ——公共類庫
- EasyFast.Repository ——數據操作(數據倉儲層)
- EasyFast.DBContext ——ORM工具層
基本框架代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; namespace EasyFast.Repository.Interface { public interface IRepository<T> where T : class { T GetById(int id); T Find(string where, string orderColumn, List<SqlParameter> parameters); int Add(T model); int Delete(int id); int Update(T model); int GetPageCount(string tableName, string where, List<SqlParameter> parameters); DataTable GetPageList(string tableName, string fieldNames, string where, string orderColumn, int startRecordIndex, int endRecordIndex, List<SqlParameter> parameters); DataTable GetDataTable(string tableName, string fieldNames, string where, string orderColumn, List<SqlParameter> parameters); } }
——目錄結構:EasyFast.Repository.Interface.IRepository
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using EasyFast.Repository.Interface; using System.Data; using System.Data.SqlClient; namespace EasyFast.Repository { public class Repository<T> : IRepository<T> where T : class { public T GetById(int id) { T model = default(T); return model; } public T Find(string where, string orderColumn, List<SqlParameter> parameters) { T model = default(T); return model; } public int Add(T model) { int _result = 0; return _result; } public int Delete(int id) { int _result = 0; return _result; } public int Update(T model) { int _result = 0; return _result; } public int GetPageCount(string tableName, string where, List<SqlParameter> parameters) { int _result = 0; return _result; } public DataTable GetPageList(string tableName, string fieldNames, string where, string orderColumn, int startRecordIndex, int endRecordIndex, List<SqlParameter> parameters) { DataTable dt = new DataTable(); return dt; } public DataTable GetDataTable(string tableName, string fieldNames, string where, string orderColumn, List<SqlParameter> parameters) { DataTable dt = new DataTable(); return dt; } } }
——目錄結構:EasyFast.Repository.Repository
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using EasyFast.Repository; using EasyFast.Repository.Interface; namespace EasyFast.BLL { public class BaseBLL<T> where T : class { IRepository<T> repository = new Repository<T>(); protected readonly Object lockHelper = new object(); #region 獲取model public T GetById(int id) { return repository.GetById(id); } public T Find(string where) { return repository.Find(where,"", null); } public T Find(string where, List<SqlParameter> parameters) { return repository.Find(where,"", parameters); } public T Find(string where, string orderColumn, List<SqlParameter> parameters) { return repository.Find(where, orderColumn, parameters); } #endregion #region 新增一條記錄 public int Add(T model) { return repository.Add(model); } #endregion #region 刪除一條記錄 public int Delete(int id) { return repository.Delete(id); } #endregion #region 更新一條記錄 public int Update(T model) { return repository.Update(model); } #endregion #region 獲取指定條件的記錄集 public virtual DataTable GetDataTable(string tableName, string fieldNames, string where, string orderColumn, List<SqlParameter> parameters) { return repository.GetDataTable(tableName, fieldNames, where, orderColumn, parameters); } public virtual DataTable GetDataTable(string fieldNames, string where, string orderColumn, List<SqlParameter> parameters) { return repository.GetDataTable("", fieldNames, where, orderColumn, parameters); } public virtual DataTable GetDataTable(string fieldNames, string where, List<SqlParameter> parameters) { return repository.GetDataTable("", fieldNames, where, "", parameters); } public virtual DataTable GetDataTable(string where, List<SqlParameter> parameters) { return repository.GetDataTable("", "*", where, "", parameters); } public virtual DataTable GetDataTable(string where) { return repository.GetDataTable("", "*", where, "", null); } public virtual DataTable GetDataTable() { return repository.GetDataTable("", "*", "", "", null); } #endregion #region 獲取指定條件的記錄數 public virtual int GetPageCount(string tableName, string where, List<SqlParameter> parameters) { return repository.GetPageCount(tableName, where, parameters); } public virtual int GetPageCount(string where, List<SqlParameter> parameters) { return repository.GetPageCount("", where, parameters); } public virtual int GetPageCount(string where) { return repository.GetPageCount("", where, null); } public virtual int GetPageCount() { return repository.GetPageCount("", "", null); } #endregion #region 分頁獲取指定條件的記錄 public virtual DataTable GetPageList(string tableName, string fieldNames, string where, string orderColumn, int startRecordIndex, int endRecordIndex, List<SqlParameter> parameters) { return repository.GetPageList(tableName, fieldNames, where, orderColumn, startRecordIndex, endRecordIndex, parameters); } public virtual DataTable GetPageList(string tableName, string fieldNames, string where, int startRecordIndex, int endRecordIndex, List<SqlParameter> parameters) { return repository.GetPageList(tableName, fieldNames, where, "", startRecordIndex, endRecordIndex, parameters); } public virtual DataTable GetPageList(string fieldNames, string where, int startRecordIndex, int endRecordIndex, List<SqlParameter> parameters) { return repository.GetPageList("", fieldNames, where, "", startRecordIndex, endRecordIndex, parameters); } public virtual DataTable GetPageList(string where, int startRecordIndex, int endRecordIndex, List<SqlParameter> parameters) { return repository.GetPageList("", "*", where, "", startRecordIndex, endRecordIndex, parameters); } public virtual DataTable GetPageList(string where, int startRecordIndex, int endRecordIndex) { return repository.GetPageList("", "*", where, "", startRecordIndex, endRecordIndex, null); } public virtual DataTable GetPageList(int startRecordIndex, int endRecordIndex) { return repository.GetPageList("", "*", "", "", startRecordIndex, endRecordIndex, null); } #endregion } }
——目錄結構:EasyFast.BLL.BaseBLL
示例代碼下載: EasyFastCMS-2014.05.28.zip