首先我們來寫個類進行獲取當前線程內唯一的DbContext

using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { /// <summary> /// 當前線程內的數據上下文 /// </summary> public class DbContextFactory { /// <summary> /// 獲取當前線程內的數據上下文,如果當前線程內沒有上下文,那么創建一個上下文, /// </summary> /// <returns>當前線程內的數據上下文</returns> public static DbContext GetCurrentDbContext() { DbContext currentContext = CallContext.GetData("CurrentDbContext") as DbContext; if (currentContext == null) { currentContext = new AuthorDesignContext(); CallContext.SetData("CurrentDbContext", currentContext); } return currentContext; } } }
CallContext 這個類是用來獲取當前線程內唯一的數據,可以避免一次性創建出多個數據庫上下文。
接下來是對基礎的倉儲進行編寫(DAL):BaseRepository類
對數據的增刪改查操作

using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class BaseRepository<T> where T : class,new() { public DbContext db = DbContextFactory.GetCurrentDbContext(); /// <summary> /// 添加一條記錄 /// </summary> /// <param name="entity"></param> /// <returns></returns> public T AddEntity(T entity) { db.Entry<T>(entity).State = EntityState.Added; db.SaveChanges(); return entity; } /// <summary> /// 修改一條記錄 /// </summary> /// <param name="entity"></param> /// <param name="property">需要修改的字段名稱</param> /// <returns></returns> public bool EditEntity(T entity, string[] property) { DbEntityEntry<T> entry = db.Entry<T>(entity); entry.State = EntityState.Unchanged; foreach (var item in property) { entry.Property(item).IsModified = true; } return db.SaveChanges() > 0; //return true; } /// <summary> /// 刪除一條記錄 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool DeleteEntity(T entity) { DbEntityEntry<T> entry = db.Entry<T>(entity); entry.State = EntityState.Deleted; return db.SaveChanges() > 0; // return true; } /// <summary> /// 查詢列表 /// </summary> /// <returns></returns> public IQueryable<T> LoadEntities() { return db.Set<T>(); } /// <summary> /// 查詢 /// </summary> /// <param name="whereLamda">查詢條件</param> /// <returns></returns> public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLamda) { return db.Set<T>().Where<T>(whereLamda); } /// <summary> /// 對查詢結果進行升序排序 /// </summary> /// <typeparam name="S">排序字段類型</typeparam> /// <param name="queryable">查詢結果</param> /// <param name="orderLamda">排序表達式</param> /// <returns>根據排序條件排序好之后的排序結果</returns> public IOrderedQueryable<T> Order<S>(IQueryable<T> queryable, Expression<Func<T, S>> orderLamda) { return queryable.OrderBy(orderLamda); } /// <summary> /// 對排序結果再次進行升序排序 /// </summary> /// <typeparam name="S">排序字段類型</typeparam> /// <param name="queryable">根據排序條件排序好之后的排序結果</param> /// <param name="orderLamda">排序表達式</param> /// <returns>根據排序條件排序好之后的排序結果</returns> public IOrderedQueryable<T> ThenOrder<S>(IOrderedQueryable<T> queryable, Expression<Func<T, S>> orderLamda) { return queryable.ThenBy(orderLamda); } /// <summary> /// 對查詢結果進行降序排序 /// </summary> /// <typeparam name="S">排序字段類型</typeparam> /// <param name="queryable">查詢結果</param> /// <param name="orderLamda">排序表達式</param> /// <returns>根據排序條件排序好之后的排序結果</returns> public IOrderedQueryable<T> OrderDesc<S>(IQueryable<T> queryable, Expression<Func<T, S>> orderLamda) { return queryable.OrderByDescending(orderLamda); } /// <summary> /// 對排序結果再次進行降序排序 /// </summary> /// <typeparam name="S">排序字段類型</typeparam> /// <param name="queryable">根據排序條件排序好之后的排序結果</param> /// <param name="orderLamda">排序表達式</param> /// <returns>根據排序條件排序好之后的排序結果</returns> public IOrderedQueryable<T> ThenOrderDesc<S>(IOrderedQueryable<T> queryable, Expression<Func<T, S>> orderLamda) { return queryable.ThenByDescending(orderLamda); } /// <summary> /// 對排序結果進行分頁操作 /// </summary> /// <param name="queryable">根據排序條件排序好之后的排序結果</param> /// <param name="nowNum">跳過序列中指定數量的元素</param> /// <param name="pageSize">從序列的開頭返回指定數量的連續元素</param> /// <returns>指定長度的列表</returns> public IQueryable<T> LoadPageEnties(IOrderedQueryable<T> queryable, int nowNum, int pageSize) { return queryable.Skip<T>(nowNum + 1).Take<T>(pageSize); } /// <summary> /// 分頁查詢 /// </summary> /// <typeparam name="S">排序類型</typeparam> /// <param name="whereLamda">查詢條件</param> /// <param name="orderLamda">排序條件</param> /// <param name="isDesc">是否倒序</param> /// <param name="pageIndex">第幾頁</param> /// <param name="pageSize">頁長</param> /// <param name="rowCount"></param> /// <returns></returns> public IQueryable<T> LoadEntities<S>(Expression<Func<T, bool>> whereLamda, Expression<Func<T, S>> orderLamda, bool isDesc, int pageIndex, int pageSize, out int rowCount) { var temp = db.Set<T>().Where<T>(whereLamda); rowCount = temp.Count(); if (isDesc) temp = temp.OrderByDescending<T, S>(orderLamda).Skip<T>(pageSize * (pageIndex - 1) + 1).Take<T>(pageSize); else temp = temp.OrderBy<T, S>(orderLamda).Skip<T>(pageSize * (pageIndex - 1) + 1).Take<T>(pageSize); return temp; } } }
然后管理員類AdminRepository的DAL編寫,繼承BaseRepository類

using AuthorDesign.IDAL; using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class AdminRepository : BaseRepository<Admin>{ } }
我要做的是面向接口的(雖然沒有BLL這層)那么接下來就對IDAL着層來進行編寫,
首先也是IBaseRepository接口

using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { public interface IBaseRepository<T> where T : class,new() { /// <summary> /// 添加一條記錄 /// </summary> /// <param name="entity"></param> /// <returns></returns> T AddEntity(T entity); /// <summary> /// 修改一條記錄 /// </summary> /// <param name="entity"></param> /// <param name="property">需要修改的字段名稱</param> /// <returns></returns> bool EditEntity(T entity, string[] property); /// <summary> /// 刪除一條記錄 /// </summary> /// <param name="entity"></param> /// <returns></returns> bool DeleteEntity(T entity); /// <summary> /// 查詢 /// </summary> IQueryable<T> LoadEntities(); /// <summary> /// 查詢 /// </summary> /// <param name="whereLamda">查詢條件</param> /// <returns></returns> IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLamda); /// <summary> /// 對查詢結果進行升序排序 /// </summary> /// <typeparam name="S">排序字段類型</typeparam> /// <param name="queryable">查詢結果</param> /// <param name="orderLamda">排序表達式</param> /// <returns>根據排序條件排序好之后的排序結果</returns> IOrderedQueryable<T> Order<S>(IQueryable<T> queryable, Expression<Func<T, S>> orderLamda); /// <summary> /// 對排序結果再次進行升序排序 /// </summary> /// <typeparam name="S">排序字段類型</typeparam> /// <param name="queryable">根據排序條件排序好之后的排序結果</param> /// <param name="orderLamda">排序表達式</param> /// <returns>根據排序條件排序好之后的排序結果</returns> IOrderedQueryable<T> ThenOrder<S>(IOrderedQueryable<T> queryable, Expression<Func<T, S>> orderLamda); /// <summary> /// 對查詢結果進行降序排序 /// </summary> /// <typeparam name="S">排序字段類型</typeparam> /// <param name="queryable">查詢結果</param> /// <param name="orderLamda">排序表達式</param> /// <returns>根據排序條件排序好之后的排序結果</returns> IOrderedQueryable<T> OrderDesc<S>(IQueryable<T> queryable, Expression<Func<T, S>> orderLamda); /// <summary> /// 對排序結果再次進行降序排序 /// </summary> /// <typeparam name="S">排序字段類型</typeparam> /// <param name="queryable">根據排序條件排序好之后的排序結果</param> /// <param name="orderLamda">排序表達式</param> /// <returns>根據排序條件排序好之后的排序結果</returns> IOrderedQueryable<T> ThenOrderDesc<S>(IOrderedQueryable<T> queryable, Expression<Func<T, S>> orderLamda); /// <summary> /// 對排序結果進行分頁操作 /// </summary> /// <param name="queryable">根據排序條件排序好之后的排序結果</param> /// <param name="nowNum">跳過序列中指定數量的元素</param> /// <param name="pageSize">從序列的開頭返回指定數量的連續元素</param> /// <returns>指定長度的列表</returns> IQueryable<T> LoadPageEnties(IOrderedQueryable<T> queryable, int nowNum, int pageSize); /// <summary> /// 分頁查詢 /// </summary> /// <typeparam name="S">排序類型</typeparam> /// <param name="whereLamda">查詢條件</param> /// <param name="orderLamda">排序條件</param> /// <param name="isDesc">是否倒序</param> /// <param name="pageIndex">第幾頁</param> /// <param name="pageSize">頁長</param> /// <param name="rowCount"></param> /// <returns></returns> IQueryable<T> LoadEntities<S>(Expression<Func<T, bool>> whereLamda, Expression<Func<T, S>> orderLamda, bool isDesc, int pageIndex, int pageSize, out int rowCount); } }
然后管理員接口IAdminRepository 繼承IBaseRepository

using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { public interface IAdminRepository:IBaseRepository<Admin> { } }
接下來我們更改下原來的 管理員類AdminRepository的DAL編寫,引用了IAdminRepository 的接口

using AuthorDesign.IDAL; using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class AdminRepository : BaseRepository<Admin>, IAdminRepository { } }
然后對其他Model也進行相同的編寫。下面附上代碼:
首先是IDAL,接口這里。

using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { public interface IActionToPageRepository : IBaseRepository<ActionToPage> { } } using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { public interface IAdminLoginLogRepository : IBaseRepository<AdminLoginLog> { } } using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { public interface IAdminOperationRepository : IBaseRepository<AdminOperation> { } } using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { public interface IAdminToPageRepository : IBaseRepository<AdminToPage> { } } using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { public interface IAuthoryRepository : IBaseRepository<Authory> { } } using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { public interface IAuthoryToPageRepository : IBaseRepository<AuthoryToPage> { } } using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { public interface IPageActionRepository : IBaseRepository<PageAction> { } } using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { public interface IPageMenuRepository : IBaseRepository<PageMenu> { } }
其次是DAL。

using AuthorDesign.IDAL; using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class ActionToPageRepository : BaseRepository<ActionToPage> ,IActionToPageRepository{ } } using AuthorDesign.IDAL; using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class AdminLoginLogRepository : BaseRepository<AdminLoginLog>, IAdminLoginLogRepository { } } using AuthorDesign.IDAL; using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class AdminOperationRepository : BaseRepository<AdminOperation>, IAdminOperationRepository { } } using AuthorDesign.IDAL; using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class AdminToPageRepository : BaseRepository<AdminToPage>, IAdminToPageRepository { } } using AuthorDesign.IDAL; using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class AuthoryRepository : BaseRepository<Authory>, IAuthoryRepository { } } using AuthorDesign.IDAL; using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class AuthoryToPageRepository : BaseRepository<AuthoryToPage>, IAuthoryToPageRepository { } } using AuthorDesign.IDAL; using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class PageActionRepository : BaseRepository<PageAction>, IPageActionRepository { } } using AuthorDesign.IDAL; using AuthorDesign.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { public class PageMenuRepository : BaseRepository<PageMenu>,IPageMenuRepository{ } }
然后就是寫個專門用來對接WEB層與 DAL與IDAL的類。
RepositoryEnter 倉儲入口,與web層的交互都交由這個類,附上代碼

using AuthorDesign.IDAL; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.DAL { /// <summary> /// 倉儲入口 /// </summary> public class RepositoryEnter:IRepositoryEnter { /// <summary> /// 統一SaveChange方法 /// </summary> /// <returns></returns> public int SaveChange() { return DbContextFactory.GetCurrentDbContext().SaveChanges(); } /// <summary> /// 獲取頁面與頁面動作聯系倉儲 /// </summary> public IDAL.IActionToPageRepository GetActionToPageRepository { get { return new ActionToPageRepository(); } } /// <summary> /// 獲取管理員登錄日志倉儲 /// </summary> public IDAL.IAdminLoginLogRepository GetAdminLoginLogRepository { get { return new AdminLoginLogRepository(); } } /// <summary> /// 獲取管理員操作倉儲 /// </summary> public IDAL.IAdminOperationRepository GetAdminOperationRepository { get { return new AdminOperationRepository(); } } /// <summary> /// 獲取管理員倉儲 /// </summary> public IDAL.IAdminRepository GetAdminRepository { get { return new AdminRepository(); } } /// <summary> /// 獲取管理員與頁面倉儲 /// </summary> public IDAL.IAdminToPageRepository GetAdminToPageRepository { get { return new AdminToPageRepository(); } } /// <summary> /// 獲取角色倉儲 /// </summary> public IDAL.IAuthoryRepository GetAuthoryRepository { get { return new AuthoryRepository(); } } /// <summary> /// 獲取角色與頁面倉儲 /// </summary> public IDAL.IAuthoryToPageRepository GetAuthoryToPageRepository { get { return new AuthoryToPageRepository(); } } /// <summary> /// 獲取頁面動作倉儲 /// </summary> public IDAL.IPageActionRepository GetPageActionRepository { get { return new PageActionRepository(); } } /// <summary> /// 獲取頁面倉儲 /// </summary> public IDAL.IPageMenuRepository GetPageMenuRepository { get { return new PageMenuRepository(); } } } }
IRepositoryEnter接口

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuthorDesign.IDAL { /// <summary> /// 倉儲入口 /// </summary> public interface IRepositoryEnter { /// <summary> /// 統一SaveChange方法 /// </summary> /// <returns></returns> int SaveChange(); /// <summary> /// 獲取頁面與頁面動作聯系倉儲 /// </summary> IDAL.IActionToPageRepository GetActionToPageRepository { get; } /// <summary> /// 獲取管理員登錄日志倉儲 /// </summary> IDAL.IAdminLoginLogRepository GetAdminLoginLogRepository { get; } /// <summary> /// 獲取管理員操作倉儲 /// </summary> IDAL.IAdminOperationRepository GetAdminOperationRepository { get; } /// <summary> /// 獲取管理員倉儲 /// </summary> IDAL.IAdminRepository GetAdminRepository { get; } /// <summary> /// 獲取管理員與頁面倉儲 /// </summary> IDAL.IAdminToPageRepository GetAdminToPageRepository { get; } /// <summary> /// 獲取角色倉儲 /// </summary> IDAL.IAuthoryRepository GetAuthoryRepository { get; } /// <summary> /// 獲取角色與頁面倉儲 /// </summary> IDAL.IAuthoryToPageRepository GetAuthoryToPageRepository { get; } /// <summary> /// 獲取頁面動作倉儲 /// </summary> IDAL.IPageActionRepository GetPageActionRepository { get; } /// <summary> /// 獲取頁面倉儲 /// </summary> IDAL.IPageMenuRepository GetPageMenuRepository { get; } } }
對DAL於IDAL的一些編寫就到這里,我感覺自己講的很亂,好像沒有什么主次之分。如果各位又不懂的或者感覺那里錯了的,還請告訴我。