您的位置:首页 > 编程语言 > ASP

ASP.NET MVC系列 框架搭建(一)之仓储层的搭建

2015-05-19 00:10 357 查看
大神勿喷,小神默默学。

会了就是不值一提的东西,不会就是绝对的高大上。

最后上传源码。希望能给读者带来一些新的认识及知识。

还没上过头条。。各位大神,请点支持一下小弟。

陆续更新。更新到你会为止!!

我不是话唠,我只把重点点出来,细枝末节的不懂的可以留言探讨。这个系列的最后,我会再统一的把大家的问题,列在一篇新的Blog。工作需要规划,写博客也是如此。

仓储层:待优化

基接口:约束

子接口:实现基接口。进一步约束子仓储中特殊的方法。

*基仓储:具体实现,子类继承接口的方法。

    这步最难,最重要!

    ①db不能直接点出具体的Model,只能db.CreateObjectSet<T>().AddObject(entity);将实体“附加”到上下文。

    ②where和order方法需要传lamada。各自类型问题。where<T,bool> order<T,S>的使用。

    ③Select时候IEumarable<T>转换IQueryable<T>需要AsQueryable()方法。

子仓储:只写除了基类和接口的方法之外外特殊的需求方法。比如多表。

以UserInforRespository为例:

IBaseRespository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

//添加System.Data.Entity引用
namespace LKBS.CommunicationPlatform.IDAL
{
public interface IBaseRespository<T> where T : class ,new()
{
//接口不需要上下文db
// int rowcount = 0;
//public 对接口方法无效
bool AddEntity(T entity);
bool DeleteEntity(T entity);
bool UpdateEntity(T entity);
T SelectOneEntity(Func<T, bool> wherelamda);
IQueryable<T> SelectAllEntityList(Func<T, bool> wherelamda);

IQueryable<T> SelectPageEntityList<S>(Func<T, S> orderLamda, Func<T, bool> whereLamda, int pageIndex, int pageSize, bool isAsc, out int rowcount);
}
}


BaseRespository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Entity;
using LKBS.CommunicationPlatform.Model;
using LKBS.CommunicationPlatform.IDAL;
using System.Data;

namespace LKBS.CommunicationPlatform.DAL
{
public class BaseRespository<T> where T : class,new()
{
ModelContainer db = new ModelContainer();

/// <summary>
/// 增加
/// </summary>
/// <param name="entity">增加实体,实体没赋的属性为NULL</param>
/// <returns></returns>
public bool AddEntity(T entity)
{
db.CreateObjectSet<T>().AddObject(entity);
//db.ObjectStateManager.ChangeObjectState(entity, EntityState.Added);
db.SaveChanges();
return true;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="entity">方便附加到上下文一个实体,传入实体,用到只有ID</param>
/// <returns></returns>
public bool DeleteEntity(T entity)
{
//var temp=  db.UserInfor.Where(userinfor=>userinfor.ID>=id).FirstOrDefault<UserInfor>();
//db.UserInfor.Attach(userinfor);
//db.ObjectStateManager.ChangeObjectState(userinfor, EntityState.Deleted);
db.CreateObjectSet<T>().AddObject(entity);
db.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted);
db.SaveChanges();
return true;
}
/// <summary>
/// 改
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public bool UpdateEntity(T entity)
{
//db.UserInfor.Attach(userinfor);
//db.ObjectStateManager.ChangeObjectState(userinfor, EntityState.Modified);
db.CreateObjectSet<T>().AddObject(entity);
db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
db.SaveChanges();
return true;
}
/// <summary>
/// 单个实体
/// </summary>
/// <param name="wherelamda"></param>
/// <returns></returns>
public T SelectOneEntity(Func<T, bool> wherelamda)
{
var rowTemp = db.CreateObjectSet<T>().Where<T>(wherelamda).FirstOrDefault<T>();
return rowTemp;
}
/// <summary>
/// 查询 整张表或者多个实体
/// </summary>
/// <returns></returns>
public IQueryable<T> SelectAllEntityList(Func<T, bool> wherelamda)
{
var rowsTemp = db.CreateObjectSet<T>().Where<T>(wherelamda);
return rowsTemp.AsQueryable<T>();
}
int rowcount = 0;
//泛型方法 <S>就是约束参数的类型,传什么参数,“自动识别”<S>. 定义需要<S>,调用该方法可以省略 :因为传的参数就已确定<S>的类型
public IQueryable<T> SelectPageEntityList<S>(Func<T, S> orderLamda, Func<T, bool> whereLamda, int pageIndex, int pageSize, bool isAsc, out int rowcount)
{//非页数pageCount,是总行数
//IEnumerable<>
var temp = db.CreateObjectSet<T>().Where<T>(whereLamda);
rowcount = temp.Count<T>();
if (isAsc)
{
//IQueryable
IEnumerable<T> entityPageList =
temp
.OrderBy<T, S>(orderLamda)
.Skip<T>((pageIndex - 1) * pageSize).
Take<T>(pageSize);
//pageCount=db.UserInfor.
return entityPageList.AsQueryable();
}
else
{
IEnumerable<T> entityPageList =
//db.UserInfor
//.Where<UserInfor>(whereLamda)//u => u.ID > 0
temp
.OrderByDescending<T, S>(orderLamda)//u => u.ID
.Skip<T>((pageIndex - 1) * pageSize).
Take<T>(pageSize);
//pageCount=db.UserInfor.
return entityPageList.AsQueryable();
}

}
}
}


IUserInforRespository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LKBS.CommunicationPlatform.Model;

namespace LKBS.CommunicationPlatform.IDAL
{
public interface IUserInforRespository:IBaseRespository<UserInfor>
{
}
}


UserInforRespository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LKBS.CommunicationPlatform.Model;
using System.Data;
using LKBS.CommunicationPlatform.IDAL;

namespace LKBS.CommunicationPlatform.DAL
{
public class UserInforRespository:BaseRespository<UserInfor>,IUserInforRespository
{
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: