自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

學(xué)習(xí)之路:項目整體框架簡單的搭建

開發(fā) 項目管理
最近剛學(xué)了些關(guān)于asp.net mvc方面的知識,于是了要拿個小項目來練練手,提高下自己的code能力跟思維能力.在此之前做東西都很簡單,直接用動軟那一套生成代碼,生成一個簡單的三層架構(gòu)作為項目整體的框架,數(shù)據(jù)庫訪問層用的是ado.net。

最近剛學(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--哈哈

  1. public class BaseRepository<T>:IDAL.IBaseRepository<T> where T:class 
  2.    {  
  3.        private DbContext container = EFContentFactory.GetCurrentContext();  
  4.         #region 增加  
  5.         public T AddEntity(T entity)  
  6.         {  
  7.    
  8.             container.Set<T>().Add(entity);  
  9.                
  10.             return entity;  
  11.         }  
  12.    
  13.         #endregion  
  14.         #region 刪除  
  15.         public bool DeleteEntity(T entity)  
  16.         {  
  17.             container.Set<T>().Attach(entity);  
  18.             container.Entry(entity).State = EntityState.Deleted;  
  19.             return true;  
  20.    
  21.         }  
  22.         #endregion  
  23.         #region 修改  
  24.         public bool UpdateEntity(T entity)  
  25.         {  
  26.             container.Set<T>().Attach(entity);  
  27.             container.Entry(entity).State = EntityState.Modified;  
  28.             return true;  
  29.         }  
  30.         #endregion  
  31.         #region 查詢  
  32.         public IQueryable<T> GetEntities(Func<T, bool> lambdaWhere)  
  33.         {  
  34.             IQueryable<T> entities = container.Set<T>().Where(lambdaWhere).AsQueryable();  
  35.             return entities;  
  36.         }  
  37.         #endregion  
  38.         #region 分頁  
  39.         public IQueryable<T> GetEntitiesByPageIndex<TS>(int pageIndex, int pageSize, out int totalCount, Func<T, bool> lambdaWhere, Func<T, TS> orderByRole, bool descending)  
  40.         {  
  41.             var temp = container.Set<T>().Where(lambdaWhere).AsQueryable();  
  42.             totalCount = temp.Count();  
  43.             if (descending)  
  44.             {  
  45.                 temp = temp.OrderByDescending(orderByRole)  
  46.                     .Skip(pageSize * (pageIndex - 1))  
  47.                     .Take(pageSize).AsQueryable();  
  48.             }  
  49.             else 
  50.             {  
  51.                 temp = temp.OrderBy(orderByRole)  
  52.                     .Skip(pageSize * (pageIndex - 1))  
  53.                     .Take(pageSize).AsQueryable();  
  54.             }  
  55.             return temp;  
  56.    
  57.         }  
  58.         #endregion  
  59.     } 

到這一步我以為自己的數(shù)據(jù)庫訪問層寫完了,然后可以去寫業(yè)務(wù)邏輯層的東西了,實則不然,想想看,如果你要換數(shù)據(jù)庫,或者換成ef或者ado.net 如果按老一套,則整個項目的每一個層都需要去替換,大大的增加了工作量,這里我們可以做個手腳,把數(shù)據(jù)訪問層再給它抽象出一層來,這就需要用到接口了.

  1. 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的接口

  1. public static class ShopDaoFactory  
  2.     {  
  3.         public  static  IUserInfoRepository UserInfoRepository  
  4.         {  
  5.             get{return new UserInfoRepository();}  
  6.         }  
  7.         public  static  IRoleRepository RoleRepository  
  8.         {  
  9.             get{return new RoleRepository();}  
  10.         }  
  11.     } 

那么業(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ù)里面

  1. public  abstract class BaseService<T> :IBLL.IBaseService<T> where T:classnew ()  
  2.     {  
  3.        public BaseService()  
  4.        {  
  5.            GetInstance();  
  6.        }  
  7.    
  8.        protected IDAL.IDbSession _DbSession = DbSeesionFactory.GetSession();  
  9.        protected IDAL.IBaseRepository<T> CurrentRepository { getset; }  
  10.        public abstract void GetInstance();  
  11.    
  12.           
  13.        public IQueryable<T> GetEntities(Func<T, bool> lambdaWhere)  
  14.        {  
  15.            //_DbSession.SavaChanges();  
  16.            return CurrentRepository.GetEntities(lambdaWhere);  
  17.        }  
  18.    
  19.        public bool DeleteEntity(T entity)  
  20.        {  
  21.            CurrentRepository.DeleteEntity(entity);  
  22.            return _DbSession.SaveChanges() > 0;  
  23.        }  
  24.    
  25.        public bool UpdateEntity(T entity)  
  26.        {  
  27.             CurrentRepository.UpdateEntity(entity);  
  28.            return _DbSession.SaveChanges() > 0;  
  29.        }  
  30.    
  31.        public T AddEntity(T entity)  
  32.        {  
  33.            var en = CurrentRepository.AddEntity(entity);  
  34.            _DbSession.SaveChanges();  
  35.            return en;  
  36.        }  
  37.    
  38.        public IQueryable<T> GetEntitiesByPageIndex<TS>(int pageIndex, int pageSize, out int totalCount, Func<T, bool> lambdaWhere, Func<T, TS> orderByRole, bool descending)  
  39.        {  
  40.            return CurrentRepository.GetEntitiesByPageIndex(pageIndex, pageSize, out totalCount, lambdaWhere, orderByRole,  
  41.                                                            descending);  
  42.        }  
  43.     }  

其他的業(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上下文只有一個了, 我們看另外一個類.

  1. public  partial class DbSession:IDAL.IDbSession  
  2.    {  
  3.        #region 代碼生成器生成  
  4.        //public IDAL.IRoleRepository RoleRepository  
  5.        //{  
  6.        //    get { return new RoleRepository();}  
  7.        //}  
  8.    
  9.        //public IDAL.IUserInfoRepository UserInfoRepository  
  10.        //{  
  11.        //    get { return  new UserInfoRepository();}  
  12.        //}  
  13.        #endregion  
  14.    
  15.        public int SaveChanges()  
  16.        {  
  17.            return EFContentFactory.GetCurrentContext().SaveChanges();  
  18.        }  
  19.    
  20.        public int ExcuteSql(string strSql, System.Data.Objects.ObjectParameter[] parameters)  
  21.        {  
  22.            return EFContentFactory.GetCurrentContext().Database.ExecuteSqlCommand(strSql, parameters);  
  23.        }  
  24.    } 

 

  1. public class EFContentFactory  
  2.     {  
  3.         public  static DbContext GetCurrentContext()  
  4.         {  
  5.             DbContext obj = CallContext.GetData("DbContext"as DbContext;  
  6.             if (obj==null)  
  7.             {  
  8.                 obj = new Model.DataContainer();  
  9.                 CallContext.SetData("DbContext",obj);  
  10.             }  
  11.             return obj;  
  12.         }  
  13.     } 

CallContext 是類似于方法調(diào)用的線程本地存儲區(qū)的專用集合對象,并提供對每個邏輯執(zhí)行線程都唯一的數(shù)據(jù)槽。數(shù)據(jù)槽不在其他邏輯線程上的調(diào)用上下文之間共享,這是從msdn上截取的一段話,它有幾個方法,這里面我們用到setdata跟 getdata,來確保上下文線程內(nèi)唯一,同樣的我們讓他接口化,與工廠內(nèi)實現(xiàn)下--

  1. public class DbSeesionFactory  
  2.    {  
  3.       /// <summary>  
  4.       /// 保證線程內(nèi)dbsession唯一  
  5.       /// </summary>  
  6.       /// <returns></returns>  
  7.       public  static  IDAL.IDbSession GetSession()  
  8.       {  
  9.           IDAL.IDbSession _dbSession = CallContext.GetData("DbSession"as IDbSession;  
  10.           if (_dbSession == null)  
  11.           {  
  12.               _dbSession = new DbSession();  
  13.               CallContext.SetData("DbSession", _dbSession);  
  14.           }  
  15.    
  16.           return _dbSession;  
  17.       }  
  18.            
  19.    } 

業(yè)務(wù)層的子類重寫方法時這么來實現(xiàn),同樣基類加個: protected IDAL.IDbSession _DbSession = DbSeesionFactory.GetSession();

  1. public partial class ActionInfoService:BaseService<ActionInfo>,IBLL.IActionInfoService     
  2.     {  
  3.         public override void GetInstance()  
  4.         {  
  5.             CurrentRepository = _DbSession.ActionInfoRepository;  
  6.         }   
  7.     }  
  8.        
  9.     public partial class R_UserInfo_ActionInfoService:BaseService<R_UserInfo_ActionInfo>,IBLL.IR_UserInfo_ActionInfoService    
  10.     {  
  11.         public override void GetInstance()  
  12.         {  
  13.             CurrentRepository = _DbSession.R_UserInfo_ActionInfoRepository;  
  14.         }   
  15.     }  
  16.        
  17.     public partial class RoleService:BaseService<Role>,IBLL.IRoleService   
  18.     {  
  19.         public override void GetInstance()  
  20.         {  
  21.             CurrentRepository = _DbSession.RoleRepository;  
  22.         }   
  23.     } 

為什么要這么做了?當(dāng)我們用EF的時候比如一個方法里面要操作多個表,就不斷的需要用到上下文,這樣可以幫我們剩不少事***直接來個_dbsession.savechange().可以達到批量刪除修改等等操作.具體看我,今天做了個批量刪除的

  1. public int DeleteUsers(List<int> list)  
  2. {  
  3. foreach (var i in list)  
  4. {  
  5. _DbSession.UserInfoRepository.DeleteEntity(new UserInfo() {ID = i});  
  6. }  
  7. return _DbSession.SaveChanges();  

好困,把這幾天學(xué)習(xí)的東西總結(jié)了下還是收獲不少,雖然對里面有些東西不是非常的理解,慢慢看看就領(lǐng)悟了,分享給大學(xué)一同學(xué)習(xí)~

原文鏈接:http://www.cnblogs.com/wings/archive/2012/12/03/2798943.html

責(zé)任編輯:林師授 來源: 博客園
相關(guān)推薦

2011-07-08 14:33:02

Cocos2d iphone

2024-01-22 16:24:10

框架小程序開發(fā)

2016-11-02 14:18:45

搭建論壇Flask框架

2016-03-15 16:24:47

集群調(diào)度框架演進

2025-01-21 08:00:00

自適應(yīng)框架框架開發(fā)

2017-12-12 14:26:16

數(shù)據(jù)庫PostgreSQL邏輯優(yōu)化

2014-07-28 14:04:26

2012-09-06 10:07:26

jQuery

2021-05-24 16:01:35

人工智能AI機器學(xué)習(xí)

2009-06-01 14:32:10

jpa技術(shù)Java框架

2010-05-25 17:35:18

IT架構(gòu)

2014-01-21 14:15:24

2013-06-20 10:28:39

MVVM框架avalon架構(gòu)

2019-10-09 11:26:01

JavaXMLSQL

2024-11-21 08:09:51

2009-07-14 14:04:36

WebWork框架

2015-07-20 13:56:59

SDN

2020-01-19 11:10:44

機器學(xué)習(xí)人工智能數(shù)據(jù)科學(xué)

2016-12-13 10:07:50

JAVA框架搭建

2009-09-27 18:06:00

CCNACCNPcisco
點贊
收藏

51CTO技術(shù)棧公眾號