數(shù)據(jù)存儲層在項(xiàng)目Ninesky.DataLibrary中實(shí)現(xiàn),整個(gè)項(xiàng)目只有一個(gè)類Repository。
Repository中實(shí)現(xiàn)增刪改查詢等方法供業(yè)務(wù)邏輯層調(diào)用,主要功能如下圖:
具體步驟
一、添加實(shí)體框架的引用
1、打開解決方案,選擇項(xiàng)目Ninesky.DataLibrary,在引用上右鍵,選擇管理NuGet程序包。
在NuGet包管理器中的瀏覽標(biāo)簽中點(diǎn)擊EntityFramework,點(diǎn)擊右側(cè)欄的安裝按鈕。
在搜索框輸入EntityFramework.zh-Hans,安裝假體中文資源包。
二、實(shí)現(xiàn)數(shù)據(jù)倉儲類
打開解決方案,選擇項(xiàng)目Ninesky.DataLibrary,將Class1.cs重命名為Repository.cs,在文檔頭添加using System.Data.Entity;將類名重命名為public class Repository。改為public class RepositoryT> where T :class
1、DbContext屬性
在Repository類中添加如下代碼 public DbContext DbContext { get; set; }
2、構(gòu)造函數(shù)
為類中添加夠高函數(shù),可以直接傳遞DbContex。
public Repository()
{ }
public Repository(DbContext dbContext)
{
DbContext = dbContext;
}
3、查找實(shí)體方法Find
Find有一個(gè)重載。兩個(gè)方法分別可以根據(jù)ID和根據(jù)lamdba表達(dá)式查找實(shí)體。
/// summary>
/// 查找實(shí)體
/// /summary>
/// param name="ID">實(shí)體主鍵值/param>
/// returns>/returns>
public T Find(int ID)
{
return DbContext.SetT>().Find(ID);
}
/// summary>
/// 查找實(shí)體
/// /summary>
/// param name="where">查詢Lambda表達(dá)式/param>
/// returns>/returns>
public T Find(ExpressionFuncT,bool>> where)
{
return DbContext.SetT>().SingleOrDefault(where);
}
4、查找實(shí)體列表方法FindList
根據(jù)需要FindList進(jìn)行多次重載
/// summary>
/// 查找實(shí)體列表
/// /summary>
/// returns>/returns>
public IQueryableT> FindList()
{
return DbContext.SetT>();
}
/// summary>
/// 查找實(shí)體列表
/// /summary>
/// typeparam name="TKey">排序建類型/typeparam>
/// param name="order">排序表達(dá)式/param>
/// param name="asc">是否正序/param>
/// returns>/returns>
public IQueryableT> FindListTKey>(ExpressionFuncT, TKey>> order, bool asc)
{
return asc ? DbContext.SetT>().OrderBy(order) : DbContext.SetT>().OrderByDescending(order);
}
/// summary>
/// 查找實(shí)體列表
/// /summary>
/// typeparam name="TKey">排序鍵類型/typeparam>
/// param name="order">排序鍵/param>
/// param name="asc">是否正序/param>
/// param name="number">獲取的記錄數(shù)量/param>
/// returns>/returns>
public IQueryableT> FindListTKey>(ExpressionFuncT, TKey>> order, bool asc,int number)
{
return asc ? DbContext.SetT>().OrderBy(order).Take(number) : DbContext.SetT>().OrderByDescending(order).Take(number);
}
/// summary>
/// 查找實(shí)體列表
/// /summary>
/// param name="where">查詢Lambda表達(dá)式/param>
/// returns>/returns>
public IQueryableT> FindList(ExpressionFuncT, bool>> where)
{
return DbContext.SetT>().Where(where);
}
/// summary>
/// 查找實(shí)體列表
/// /summary>
/// param name="where">查詢Lambda表達(dá)式/param>
/// param name="number">獲取的記錄數(shù)量/param>
/// returns>/returns>
public IQueryableT> FindList(ExpressionFuncT, bool>> where, int number)
{
return DbContext.SetT>().Where(where).Take(number);
}
/// summary>
/// 查找實(shí)體列表
/// /summary>
/// typeparam name="TKey">排序鍵類型/typeparam>
/// param name="where">查詢Lambda表達(dá)式/param>
/// param name="order">排序鍵/param>
/// param name="asc">是否正序/param>
/// returns>/returns>
public IQueryableT> FindListTKey>(ExpressionFuncT, bool>> where, ExpressionFuncT, TKey>> order, bool asc)
{
return asc ? DbContext.SetT>().Where(where).OrderBy(order) : DbContext.SetT>().Where(where).OrderByDescending(order);
}
/// summary>
/// 查找實(shí)體列表
/// /summary>
/// typeparam name="TKey">排序鍵類型/typeparam>
/// param name="where">查詢Lambda表達(dá)式/param>
/// param name="order">排序鍵/param>
/// param name="asc">是否正序/param>
/// param name="number">獲取的記錄數(shù)量/param>
/// returns>/returns>
public IQueryableT> FindListTKey>(ExpressionFuncT, bool>> where, ExpressionFuncT, TKey>> order, bool asc, int number)
{
return asc ? DbContext.SetT>().Where(where).OrderBy(order).Take(number) : DbContext.SetT>().Where(where).OrderByDescending(order).Take(number);
}
5、查找實(shí)體分頁列表方法FindPageList
根據(jù)需要FindPageList進(jìn)行多次重載
/// summary>
/// 查找分頁列表
/// /summary>
/// param name="pageSize">每頁記錄數(shù)。必須大于1/param>
/// param name="pageIndex">頁碼。首頁從1開始,頁碼必須大于1/param>
/// param name="totalNumber">總記錄數(shù)/param>
/// returns>/returns>
public IQueryableT> FindPageList(int pageSize, int pageIndex, out int totalNumber)
{
if (pageIndex 1) pageIndex = 1;
if (pageSize 1) pageSize = 10;
IQueryableT> _list = DbContext.SetT>();
totalNumber = _list.Count();
return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize);
}
/// summary>
/// 查找分頁列表
/// /summary>
/// param name="pageSize">每頁記錄數(shù)。必須大于1/param>
/// param name="pageIndex">頁碼。首頁從1開始,頁碼必須大于1/param>
/// param name="totalNumber">總記錄數(shù)/param>
/// param name="order">排序鍵/param>
/// param name="asc">是否正序/param>
/// returns>/returns>
public IQueryableT> FindPageListTKey>(int pageSize, int pageIndex, out int totalNumber, ExpressionFuncT, TKey>> order, bool asc)
{
if (pageIndex 1) pageIndex = 1;
if (pageSize 1) pageSize = 10;
IQueryableT> _list = DbContext.SetT>();
_list = asc ? _list.OrderBy(order) : _list.OrderByDescending(order);
totalNumber = _list.Count();
return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize);
}
/// summary>
/// 查找分頁列表
/// /summary>
/// param name="pageSize">每頁記錄數(shù)。必須大于1/param>
/// param name="pageIndex">頁碼。首頁從1開始,頁碼必須大于1/param>
/// param name="totalNumber">總記錄數(shù)/param>
/// param name="where">查詢表達(dá)式/param>
public IQueryableT> FindPageList(int pageSize, int pageIndex, out int totalNumber, ExpressionFuncT, bool>> where)
{
if (pageIndex 1) pageIndex = 1;
if (pageSize 1) pageSize = 10;
IQueryableT> _list = DbContext.SetT>().Where(where);
totalNumber = _list.Count();
return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize);
}
/// summary>
/// 查找分頁列表
/// /summary>
/// param name="pageSize">每頁記錄數(shù)。必須大于1/param>
/// param name="pageIndex">頁碼。首頁從1開始,頁碼必須大于1/param>
/// param name="totalNumber">總記錄數(shù)/param>
/// param name="where">查詢表達(dá)式/param>
/// param name="order">排序鍵/param>
/// param name="asc">是否正序/param>
public IQueryableT> FindPageListTKey>(int pageSize, int pageIndex, out int totalNumber, ExpressionFuncT, bool>> where, ExpressionFuncT, TKey>> order, bool asc)
{
if (pageIndex 1) pageIndex = 1;
if (pageSize 1) pageSize = 10;
IQueryableT> _list = DbContext.SetT>().Where(where);
_list = asc ? _list.OrderBy(order) : _list.OrderByDescending(order);
totalNumber = _list.Count();
return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize);
}
6、添加實(shí)體方法Add
Add 方法有一個(gè)重載,重載方法可以選擇是否立即保存
/// summary>
/// 添加實(shí)體
/// /summary>
/// param name="entity">實(shí)體/param>
/// returns>受影響的對象的數(shù)目/returns>
public int Add(T entity)
{
return Add(entity, true);
}
/// summary>
/// 添加實(shí)體
/// /summary>
/// param name="entity">實(shí)體/param>
/// param name="isSave">是否立即保存/param>
/// returns>在“isSave”為True時(shí)返回受影響的對象的數(shù)目,為False時(shí)直接返回0/returns>
public int Add(T entity,bool isSave)
{
DbContext.SetT>().Add(entity);
return isSave ? DbContext.SaveChanges() : 0;
}
7、更新實(shí)體方法Update
Updae 方法有一個(gè)重載,重載方法可以選擇是否立即保存
#region Update
/// summary>
/// 更新實(shí)體【立即保存】
/// /summary>
/// param name="entity">實(shí)體/param>
/// returns>在“isSave”為True時(shí)返回受影響的對象的數(shù)目,為False時(shí)直接返回0/returns>
public int Update(T entity)
{
return Update(entity, true);
}
/// summary>
/// 更新實(shí)體
/// /summary>
/// param name="entity">實(shí)體/param>
/// param name="isSave">是否立即保存/param>
/// returns>在“isSave”為True時(shí)返回受影響的對象的數(shù)目,為False時(shí)直接返回0/returns>
public int Update(T entity, bool isSave)
{
DbContext.SetT>().Attach(entity);
DbContext.EntryT>(entity).State = EntityState.Modified;
return isSave ? DbContext.SaveChanges() : 0;
}
8、刪除實(shí)體方法Delete
Delete 方法有兩個(gè)重載,一個(gè)是可以選擇否立即保存,另一個(gè)是批量刪除
/// summary>
/// 刪除實(shí)體【立即保存】
/// /summary>
/// param name="entity">實(shí)體/param>
/// returns>受影響的對象的數(shù)目/returns>
public int Delete(T entity)
{
return Delete(entity, true);
}
/// summary>
/// 刪除實(shí)體
/// /summary>
/// param name="entity">實(shí)體/param>
/// param name="isSave">是否立即保存/param>
/// returns>在“isSave”為True時(shí)返回受影響的對象的數(shù)目,為False時(shí)直接返回0/returns>
public int Delete(T entity,bool isSave)
{
DbContext.SetT>().Remove(entity);
return isSave ? DbContext.SaveChanges() : 0;
}
/// summary>
/// 批量刪除實(shí)體
/// /summary>
/// param name="entities">實(shí)體集合/param>
/// returns>受影響的對象的數(shù)目/returns>
public int Delete(IEnumerableT> entities)
{
DbContext.SetT>().RemoveRange(entities);
return DbContext.SaveChanges();
}
9、統(tǒng)計(jì)記錄數(shù)方法Count
Count 方法有一個(gè)重載,可以根據(jù)表達(dá)式進(jìn)行統(tǒng)計(jì)
/// summary>
/// 記錄數(shù)
/// /summary>
/// returns>/returns>
public int Count()
{
return DbContext.SetT>().Count();
}
/// summary>
/// 記錄數(shù)
/// /summary>
/// param name="predicate">表達(dá)式/param>
/// returns>/returns>
public int Count(ExpressionFuncT, bool>> predicate)
{
return DbContext.SetT>().Count(predicate);
}
10、是否存在
/// summary>
/// 記錄是否存在
/// /summary>
/// param name="predicate">表達(dá)式/param>
/// returns>/returns>
public bool IsContains(ExpressionFuncT, bool>> predicate)
{
return Count(predicate) > 0;
}
11、保存到數(shù)據(jù)庫
/// summary>
/// 保存數(shù)據(jù)【在Add、Upate、Delete未立即保存的情況下使用】
/// /summary>
/// returns>受影響的記錄數(shù)/returns>
public int Save()
{
return DbContext.SaveChanges();
}
您可能感興趣的文章:- IIS7/IIS7.5/IIS8網(wǎng)站目錄執(zhí)行權(quán)限設(shè)置方法(與IIS6不同)
- Win2008 R2中IIS7.5配置完網(wǎng)站權(quán)限不足問題的解決方法
- IIS PHP環(huán)境Temp文件夾的權(quán)限問題引起的網(wǎng)站故障
- win2003 IIS虛擬主機(jī)網(wǎng)站防木馬、權(quán)限設(shè)置、安全配置整理
- Apache Wind2003 配置網(wǎng)站目錄權(quán)限小結(jié)
- ASP.NET MVC5網(wǎng)站開發(fā)之登錄、驗(yàn)證和注銷管理員篇1(六)
- vs2010制作簡單的asp.net網(wǎng)站
- 如何對ASP.NET網(wǎng)站實(shí)現(xiàn)靜態(tài)化
- ASP.NET MVC5網(wǎng)站開發(fā)管理列表、回復(fù)及刪除(十三)
- MVC網(wǎng)站開發(fā)之權(quán)限管理篇