學(xué)習(xí)之路:項目整體框架簡單的搭建
最近剛學(xué)了些關(guān)于asp.net mvc方面的知識,于是了要拿個小項目來練練手,提高下自己的code能力跟思維能力.在此之前做東西都很簡單,直接用動軟那一套生成代碼,生成一個簡單的三層架構(gòu)作為項目整體的框架,數(shù)據(jù)庫訪問層用的是ado.net.這么做了感覺挺麻煩,如果要項目要換數(shù)據(jù)庫,要給數(shù)據(jù)庫增加表或者給表增加某個字段, 或者不使用ado.net用個orm框架來訪問數(shù)據(jù)庫等等,這樣整體項目該動起來就提別的麻煩,為了解決這一些問題我們需要重新思考怎么搭建。
關(guān)于數(shù)據(jù)庫訪問層
數(shù)據(jù)庫訪問驅(qū)動層--大家都知道EF,NH跟Ado.net或者你自己實現(xiàn)的,這些都是為我們訪問數(shù)據(jù)庫或?qū)?shù)據(jù)庫操作建立了橋梁,當(dāng)然數(shù)據(jù)庫也可能是不同的數(shù)據(jù)庫,這些都是根據(jù)項目需求來定的,至于選擇哪個則要視情況而定了.這里我就用了EF--model-first.我是直接在edmx里面設(shè)計模型,然后生成實體跟數(shù)據(jù)庫,具體如下,做了個簡單的權(quán)限管理(還沒完全實現(xiàn))..
數(shù)據(jù)庫訪問實現(xiàn)層
這個層很簡單就是傳統(tǒng)意義上的BLL層,很久之前了我是一個實體寫5個crud的基本操作,現(xiàn)在發(fā)現(xiàn)好2,真是覺的學(xué)的越多代碼越少,,,,這里直接定義一個基類,然后讓別的實體類繼承即可,,,真心發(fā)現(xiàn)以前好2--哈哈
- public class BaseRepository<T>:IDAL.IBaseRepository<T> where T:class
- {
- private DbContext container = EFContentFactory.GetCurrentContext();
- #region 增加
- public T AddEntity(T entity)
- {
- container.Set<T>().Add(entity);
- return entity;
- }
- #endregion
- #region 刪除
- public bool DeleteEntity(T entity)
- {
- container.Set<T>().Attach(entity);
- container.Entry(entity).State = EntityState.Deleted;
- return true;
- }
- #endregion
- #region 修改
- public bool UpdateEntity(T entity)
- {
- container.Set<T>().Attach(entity);
- container.Entry(entity).State = EntityState.Modified;
- return true;
- }
- #endregion
- #region 查詢
- public IQueryable<T> GetEntities(Func<T, bool> lambdaWhere)
- {
- IQueryable<T> entities = container.Set<T>().Where(lambdaWhere).AsQueryable();
- return entities;
- }
- #endregion
- #region 分頁
- public IQueryable<T> GetEntitiesByPageIndex<TS>(int pageIndex, int pageSize, out int totalCount, Func<T, bool> lambdaWhere, Func<T, TS> orderByRole, bool descending)
- {
- var temp = container.Set<T>().Where(lambdaWhere).AsQueryable();
- totalCount = temp.Count();
- if (descending)
- {
- temp = temp.OrderByDescending(orderByRole)
- .Skip(pageSize * (pageIndex - 1))
- .Take(pageSize).AsQueryable();
- }
- else
- {
- temp = temp.OrderBy(orderByRole)
- .Skip(pageSize * (pageIndex - 1))
- .Take(pageSize).AsQueryable();
- }
- return temp;
- }
- #endregion
- }
到這一步我以為自己的數(shù)據(jù)庫訪問層寫完了,然后可以去寫業(yè)務(wù)邏輯層的東西了,實則不然,想想看,如果你要換數(shù)據(jù)庫,或者換成ef或者ado.net 如果按老一套,則整個項目的每一個層都需要去替換,大大的增加了工作量,這里我們可以做個手腳,把數(shù)據(jù)訪問層再給它抽象出一層來,這就需要用到接口了.
- IDAL.IBaseRepository<T>
大體想想看我們的bll層如果沒有接口我們直接這么寫 dal.xxrepository=new xxrepository();老一套的寫法,則跟我前面說的一樣,可維護性替換性大大降低..我們現(xiàn)在可以這么寫
IDAL.xxrepository=new xxrepository().這樣我們替換DAL層時候 BLL層根部不需要關(guān)心你到底是怎么實現(xiàn)的.這一點非常的重要.接口就相當(dāng)于一個契約,約束了你必須實現(xiàn)哪些功能,我們?nèi)绻黾庸δ芸芍苯釉诮涌谥性鎏?接口需要為部分接口,如我給出的上面代碼一樣,基類需要一個接口,子類也需要.這樣我們就抽象出一個數(shù)據(jù)庫接口層.
抽象工廠與簡單工廠
我們還可以對業(yè)務(wù)層跟數(shù)據(jù)庫訪問層再讀的抽象出來,這里我們就需要用到工廠--其實很簡單,從工廠類里面取出來的dal層的類并返回IDAL的接口
- public static class ShopDaoFactory
- {
- public static IUserInfoRepository UserInfoRepository
- {
- get{return new UserInfoRepository();}
- }
- public static IRoleRepository RoleRepository
- {
- get{return new RoleRepository();}
- }
- }
那么業(yè)務(wù)層拿到接口時也不需要關(guān)心到底怎么實現(xiàn)的,這樣又是一層的抽象,當(dāng)然你也可以用抽象工廠,利用反射跟配置外加緩存來實現(xiàn),不過一般情況下簡單工廠足夠了,這里就相當(dāng)于一個數(shù)據(jù)庫訪問層的入口了.
業(yè)務(wù)邏輯層的基類與子類
當(dāng)我們實體模型多了的時候我們?nèi)绻麤]有基類,則要寫一堆重復(fù)性的東西,我們現(xiàn)在就要把這些重復(fù)的性的東西放到基類里面給我們實現(xiàn),如同Dal層,我們定義了一個基類,但是在BLL層我們會遇到一個問題,IDAL.IBaseRepository<T>怎么獲取從工廠獲得接口了......思考一下.....我們的子類可以知道自己所需要的接口------我們可以做個手腳,讓父類為抽象類,定義一個抽象方法,然后讓子類重寫改方法,并且在構(gòu)造函數(shù)里面調(diào)用,因為我們必須用到這個接口,所以必須在構(gòu)造函數(shù)里面
- public abstract class BaseService<T> :IBLL.IBaseService<T> where T:class, new ()
- {
- public BaseService()
- {
- GetInstance();
- }
- protected IDAL.IDbSession _DbSession = DbSeesionFactory.GetSession();
- protected IDAL.IBaseRepository<T> CurrentRepository { get; set; }
- public abstract void GetInstance();
- public IQueryable<T> GetEntities(Func<T, bool> lambdaWhere)
- {
- //_DbSession.SavaChanges();
- return CurrentRepository.GetEntities(lambdaWhere);
- }
- public bool DeleteEntity(T entity)
- {
- CurrentRepository.DeleteEntity(entity);
- return _DbSession.SaveChanges() > 0;
- }
- public bool UpdateEntity(T entity)
- {
- CurrentRepository.UpdateEntity(entity);
- return _DbSession.SaveChanges() > 0;
- }
- public T AddEntity(T entity)
- {
- var en = CurrentRepository.AddEntity(entity);
- _DbSession.SaveChanges();
- return en;
- }
- public IQueryable<T> GetEntitiesByPageIndex<TS>(int pageIndex, int pageSize, out int totalCount, Func<T, bool> lambdaWhere, Func<T, TS> orderByRole, bool descending)
- {
- return CurrentRepository.GetEntitiesByPageIndex(pageIndex, pageSize, out totalCount, lambdaWhere, orderByRole,
- descending);
- }
- }
- }
其他的業(yè)務(wù)層也需要接口抽象出一層出來來作為約束,這樣ui層也不需要關(guān)心你業(yè)務(wù)層怎么實現(xiàn)...
另外一種實現(xiàn)數(shù)據(jù)庫入口的方試DBSession
我們先看一個類,dbsession里面有屬性,為接口,對應(yīng)的該接口所對應(yīng)的實現(xiàn)類,兩個方法SaveChanges(),與exesql(EF 用的5.0+),里面返回的是當(dāng)前EF線程類上下文的savechange()與執(zhí)行sql語句的放回值,怎么才能確保當(dāng)前進程內(nèi)EF上下文只有一個了, 我們看另外一個類.
- public partial class DbSession:IDAL.IDbSession
- {
- #region 代碼生成器生成
- //public IDAL.IRoleRepository RoleRepository
- //{
- // get { return new RoleRepository();}
- //}
- //public IDAL.IUserInfoRepository UserInfoRepository
- //{
- // get { return new UserInfoRepository();}
- //}
- #endregion
- public int SaveChanges()
- {
- return EFContentFactory.GetCurrentContext().SaveChanges();
- }
- public int ExcuteSql(string strSql, System.Data.Objects.ObjectParameter[] parameters)
- {
- return EFContentFactory.GetCurrentContext().Database.ExecuteSqlCommand(strSql, parameters);
- }
- }
- public class EFContentFactory
- {
- public static DbContext GetCurrentContext()
- {
- DbContext obj = CallContext.GetData("DbContext") as DbContext;
- if (obj==null)
- {
- obj = new Model.DataContainer();
- CallContext.SetData("DbContext",obj);
- }
- return obj;
- }
- }
CallContext 是類似于方法調(diào)用的線程本地存儲區(qū)的專用集合對象,并提供對每個邏輯執(zhí)行線程都唯一的數(shù)據(jù)槽。數(shù)據(jù)槽不在其他邏輯線程上的調(diào)用上下文之間共享,這是從msdn上截取的一段話,它有幾個方法,這里面我們用到setdata跟 getdata,來確保上下文線程內(nèi)唯一,同樣的我們讓他接口化,與工廠內(nèi)實現(xiàn)下--
- public class DbSeesionFactory
- {
- /// <summary>
- /// 保證線程內(nèi)dbsession唯一
- /// </summary>
- /// <returns></returns>
- public static IDAL.IDbSession GetSession()
- {
- IDAL.IDbSession _dbSession = CallContext.GetData("DbSession") as IDbSession;
- if (_dbSession == null)
- {
- _dbSession = new DbSession();
- CallContext.SetData("DbSession", _dbSession);
- }
- return _dbSession;
- }
- }
業(yè)務(wù)層的子類重寫方法時這么來實現(xiàn),同樣基類加個: protected IDAL.IDbSession _DbSession = DbSeesionFactory.GetSession();
- public partial class ActionInfoService:BaseService<ActionInfo>,IBLL.IActionInfoService
- {
- public override void GetInstance()
- {
- CurrentRepository = _DbSession.ActionInfoRepository;
- }
- }
- public partial class R_UserInfo_ActionInfoService:BaseService<R_UserInfo_ActionInfo>,IBLL.IR_UserInfo_ActionInfoService
- {
- public override void GetInstance()
- {
- CurrentRepository = _DbSession.R_UserInfo_ActionInfoRepository;
- }
- }
- public partial class RoleService:BaseService<Role>,IBLL.IRoleService
- {
- public override void GetInstance()
- {
- CurrentRepository = _DbSession.RoleRepository;
- }
- }
為什么要這么做了?當(dāng)我們用EF的時候比如一個方法里面要操作多個表,就不斷的需要用到上下文,這樣可以幫我們剩不少事***直接來個_dbsession.savechange().可以達到批量刪除修改等等操作.具體看我,今天做了個批量刪除的
- public int DeleteUsers(List<int> list)
- {
- foreach (var i in list)
- {
- _DbSession.UserInfoRepository.DeleteEntity(new UserInfo() {ID = i});
- }
- return _DbSession.SaveChanges();
- }
好困,把這幾天學(xué)習(xí)的東西總結(jié)了下還是收獲不少,雖然對里面有些東西不是非常的理解,慢慢看看就領(lǐng)悟了,分享給大學(xué)一同學(xué)習(xí)~
原文鏈接:http://www.cnblogs.com/wings/archive/2012/12/03/2798943.html