最近剛學(xué)了些關(guān)于asp.net mvc方面的知識(shí),于是了要拿個(gè)小項(xiàng)目來(lái)練練手,提高下自己的code能力跟思維能力.在此之前做東西都很簡(jiǎn)單,直接用動(dòng)軟那一套生成代碼,生成一個(gè)簡(jiǎn)單的三層架構(gòu)作為項(xiàng)目整體的框架,數(shù)據(jù)庫(kù)訪問(wèn)層用的是ado.net.這么做了感覺(jué)挺麻煩,如果要項(xiàng)目要換數(shù)據(jù)庫(kù),要給數(shù)據(jù)庫(kù)增加表或者給表增加某個(gè)字段,或者不使用ado.net用個(gè)orm框架來(lái)訪問(wèn)數(shù)據(jù)庫(kù)等等,這樣整體項(xiàng)目該動(dòng)起來(lái)就提別的麻煩,為了解決這一些問(wèn)題我們需要重新思考怎么搭建.
關(guān)于數(shù)據(jù)庫(kù)訪問(wèn)層
數(shù)據(jù)庫(kù)訪問(wèn)驅(qū)動(dòng)層--大家都知道EF,NH跟Ado.net或者你自己實(shí)現(xiàn)的,這些都是為我們?cè)L問(wèn)數(shù)據(jù)庫(kù)或?qū)?shù)據(jù)庫(kù)操作建立了橋梁,當(dāng)然數(shù)據(jù)庫(kù)也可能是不同的數(shù)據(jù)庫(kù),這些都是根據(jù)項(xiàng)目需求來(lái)定的,至于選擇哪個(gè)則要視情況而定了.這里我就用了EF--model-first.我是直接在edmx里面設(shè)計(jì)模型,然后生成實(shí)體跟數(shù)據(jù)庫(kù),具體如下,做了個(gè)簡(jiǎn)單的權(quán)限管理(還沒(méi)完全實(shí)現(xiàn))..

復(fù)制代碼 代碼如下:
public class BaseRepositoryT>:IDAL.IBaseRepositoryT> where T:class
{
private DbContext container = EFContentFactory.GetCurrentContext();
#region 增加
public T AddEntity(T entity)
{
container.SetT>().Add(entity);
return entity;
}
#endregion
#region 刪除
public bool DeleteEntity(T entity)
{
container.SetT>().Attach(entity);
container.Entry(entity).State = EntityState.Deleted;
return true;
}
#endregion
#region 修改
public bool UpdateEntity(T entity)
{
container.SetT>().Attach(entity);
container.Entry(entity).State = EntityState.Modified;
return true;
}
#endregion
#region 查詢(xún)
public IQueryableT> GetEntities(FuncT, bool> lambdaWhere)
{
IQueryableT> entities = container.SetT>().Where(lambdaWhere).AsQueryable();
return entities;
}
#endregion
#region 分頁(yè)
public IQueryableT> GetEntitiesByPageIndexTS>(int pageIndex, int pageSize, out int totalCount, FuncT, bool> lambdaWhere, FuncT, TS> orderByRole, bool descending)
{
var temp = container.SetT>().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ù)庫(kù)訪問(wèn)層寫(xiě)完了,然后可以去寫(xiě)業(yè)務(wù)邏輯層的東西了,實(shí)則不然,想想看,如果你要換數(shù)據(jù)庫(kù),或者換成ef或者ado.net 如果按老一套,則整個(gè)項(xiàng)目的每一個(gè)層都需要去替換,大大的增加了工作量,這里我們可以做個(gè)手腳,把數(shù)據(jù)訪問(wèn)層再給它抽象出一層來(lái),這就需要用到接口了.
IDAL.IBaseRepositoryT>大體想想看我們的bll層如果沒(méi)有接口我們直接這么寫(xiě) dal.xxrepository=new xxrepository();老一套的寫(xiě)法,則跟我前面說(shuō)的一樣,可維護(hù)性替換性大大降低..我們現(xiàn)在可以這么寫(xiě)
IDAL.xxrepository=new xxrepository().這樣我們替換DAL層時(shí)候 BLL層根部不需要關(guān)心你到底是怎么實(shí)現(xiàn)的.這一點(diǎn)非常的重要.接口就相當(dāng)于一個(gè)契約,約束了你必須實(shí)現(xiàn)哪些功能,我們?nèi)绻黾庸δ芸芍苯釉诮涌谥性鎏?接口需要為部分接口,如我給出的上面代碼一樣,基類(lèi)需要一個(gè)接口,子類(lèi)也需要.這樣我們就抽象出一個(gè)數(shù)據(jù)庫(kù)接口層.
抽象工廠與簡(jiǎn)單工廠
我們還可以對(duì)業(yè)務(wù)層跟數(shù)據(jù)庫(kù)訪問(wèn)層再讀的抽象出來(lái),這里我們就需要用到工廠--其實(shí)很簡(jiǎn)單,從工廠類(lèi)里面取出來(lái)的dal層的類(lèi)并返回IDAL的接口
復(fù)制代碼 代碼如下:
public static class ShopDaoFactory
{
public static IUserInfoRepository UserInfoRepository
{
get{return new UserInfoRepository();}
}
public static IRoleRepository RoleRepository
{
get{return new RoleRepository();}
}
}
那么業(yè)務(wù)層拿到接口時(shí)也不需要關(guān)心到底怎么實(shí)現(xiàn)的,這樣又是一層的抽象,當(dāng)然你也可以用抽象工廠,利用反射跟配置外加緩存來(lái)實(shí)現(xiàn),不過(guò)一般情況下簡(jiǎn)單工廠足夠了,這里就相當(dāng)于一個(gè)數(shù)據(jù)庫(kù)訪問(wèn)層的入口了.
業(yè)務(wù)邏輯層的基類(lèi)與子類(lèi)
當(dāng)我們實(shí)體模型多了的時(shí)候我們?nèi)绻麤](méi)有基類(lèi),則要寫(xiě)一堆重復(fù)性的東西,我們現(xiàn)在就要把這些重復(fù)的性的東西放到基類(lèi)里面給我們實(shí)現(xiàn),如同Dal層,我們定義了一個(gè)基類(lèi),但是在BLL層我們會(huì)遇到一個(gè)問(wèn)題,IDAL.IBaseRepositoryT>怎么獲取從工廠獲得接口了......思考一下.....我們的子類(lèi)可以知道自己所需要的接口------我們可以做個(gè)手腳,讓父類(lèi)為抽象類(lèi),定義一個(gè)抽象方法,然后讓子類(lèi)重寫(xiě)改方法,并且在構(gòu)造函數(shù)里面調(diào)用,因?yàn)槲覀儽仨氂玫竭@個(gè)接口,所以必須在構(gòu)造函數(shù)里面
復(fù)制代碼 代碼如下:
public abstract class BaseServiceT> :IBLL.IBaseServiceT> where T:class, new ()
{
public BaseService()
{
GetInstance();
}
protected IDAL.IDbSession _DbSession = DbSeesionFactory.GetSession();
protected IDAL.IBaseRepositoryT> CurrentRepository { get; set; }
public abstract void GetInstance();
public IQueryableT> GetEntities(FuncT, 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 IQueryableT> GetEntitiesByPageIndexTS>(int pageIndex, int pageSize, out int totalCount, FuncT, bool> lambdaWhere, FuncT, TS> orderByRole, bool descending)
{
return CurrentRepository.GetEntitiesByPageIndex(pageIndex, pageSize, out totalCount, lambdaWhere, orderByRole,
descending);
}
}
}
其他的業(yè)務(wù)層也需要接口抽象出一層出來(lái)來(lái)作為約束,這樣ui層也不需要關(guān)心你業(yè)務(wù)層怎么實(shí)現(xiàn)...
另外一種實(shí)現(xiàn)數(shù)據(jù)庫(kù)入口的方試DBSession
我們先看一個(gè)類(lèi),dbsession里面有屬性,為接口,對(duì)應(yīng)的該接口所對(duì)應(yīng)的實(shí)現(xiàn)類(lèi),兩個(gè)方法SaveChanges(),與exesql(EF用的5.0+),里面返回的是當(dāng)前EF線程類(lèi)上下文的savechange()與執(zhí)行sql語(yǔ)句的放回值,怎么才能確保當(dāng)前進(jìn)程內(nèi)EF上下文只有一個(gè)了,我們看另外一個(gè)類(lèi).
復(fù)制代碼 代碼如下:
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 是類(lèi)似于方法調(diào)用的線程本地存儲(chǔ)區(qū)的專(zhuān)用集合對(duì)象,并提供對(duì)每個(gè)邏輯執(zhí)行線程都唯一的數(shù)據(jù)槽。數(shù)據(jù)槽不在其他邏輯線程上的調(diào)用上下文之間共享,這是從msdn上截取的一段話,它有幾個(gè)方法,這里面我們用到setdata跟getdata,來(lái)確保上下文線程內(nèi)唯一,同樣的我們讓他接口化,與工廠內(nèi)實(shí)現(xiàn)下--
復(fù)制代碼 代碼如下:
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ù)層的子類(lèi)重寫(xiě)方法時(shí)這么來(lái)實(shí)現(xiàn),同樣基類(lèi)加個(gè): protected IDAL.IDbSession _DbSession = DbSeesionFactory.GetSession();
復(fù)制代碼 代碼如下:
public partial class ActionInfoService:BaseServiceActionInfo>,IBLL.IActionInfoService
{
public override void GetInstance()
{
CurrentRepository = _DbSession.ActionInfoRepository;
}
}
public partial class R_UserInfo_ActionInfoService:BaseServiceR_UserInfo_ActionInfo>,IBLL.IR_UserInfo_ActionInfoService
{
public override void GetInstance()
{
CurrentRepository = _DbSession.R_UserInfo_ActionInfoRepository;
}
}
public partial class RoleService:BaseServiceRole>,IBLL.IRoleService
{
public override void GetInstance()
{
CurrentRepository = _DbSession.RoleRepository;
}
}
為什么要這么做了?當(dāng)我們用EF的時(shí)候比如一個(gè)方法里面要操作多個(gè)表,就不斷的需要用到上下文,這樣可以幫我們剩不少事最后直接來(lái)個(gè)_dbsession.savechange().可以達(dá)到批量刪除修改等等操作.具體看我,今天做了個(gè)批量刪除的
復(fù)制代碼 代碼如下:
public int DeleteUsers(Listint> list)
{
foreach (var i in list)
{
_DbSession.UserInfoRepository.DeleteEntity(new UserInfo() {ID = i});
}
return _DbSession.SaveChanges();
}
好困,把這幾天學(xué)習(xí)的東西總結(jié)了下還是收獲不少,雖然對(duì)里面有些東西不是非常的理解,慢慢看看就領(lǐng)悟了,分享給大學(xué)一同學(xué)習(xí)~
您可能感興趣的文章:- 在IIS上部署ASP.NET Core項(xiàng)目的圖文方法
- asp.net core項(xiàng)目mvc權(quán)限控制:分配權(quán)限
- 淺談ASP.NET Core 中間件詳解及項(xiàng)目實(shí)戰(zhàn)
- 使用.NET命令行編譯器編譯項(xiàng)目(如ASP.NET、C#等)
- 創(chuàng)建一個(gè)完整的ASP.NET Web API項(xiàng)目
- 如何為asp.net網(wǎng)站項(xiàng)目添加子項(xiàng)目
- asp.net 刪除項(xiàng)目文件/文件夾IIS重啟,Session丟失問(wèn)題
- Asp.Net MVC3.0如何項(xiàng)目部署到Win7 64位系統(tǒng)
- ASP.NET Core新建項(xiàng)目教程(3)