在本系列教程中,我们以一个大型CMS系统的完整开发流程为例,和大家一起探讨net开发的经验和教训。在本程序中,我们采用了流行的三层/N层框架+仓储模式的架构模式。项目分层示意图:
![](https://i-blog.csdnimg.cn/blog_migrate/40fbb62dcf4f0863107772334ca4d214.png)
各层的主要用途:
- 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