1.添加框架
Model,DAL,IDAL,DALFactory,BLL,IBLL,Common,UI 层
2,在Model层中添加EF框架。保存,让实体映射到Model层。
3.搭设框架IDAL层的IUserInfoDal,公共的正删改查分页方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CZBW.BookShop.Model;
namespace CZBW.BookShop.IDAL
{
public interface IUserInfoDal
{
//查询数据库,传入参数是Lambda表达式
IQueryable<UserInfo> LoadEntities(System.Linq.Expressions.Expression<Func<UserInfo,bool>>whereLambda );
IQueryable<UserInfo> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount,System.Linq.Expressions.Expression<Func<UserInfo,s>> orderByLambda,System.Linq.Expressions.Expression<Func<UserInfo,bool>> whereLambda,bool isAsc);
bool DeleteEntity(UserInfo entity);
bool UpdateEntity(UserInfo entity);
UserInfo AddEntity(UserInfo entity);
}
}
将IUserInfoDal的内容替换为泛型,剪切到IBaseDal中,作为通用接口,同事IUserInfoDal继承IBaseDal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CZBW.BookShop.Model;
namespace CZBW.BookShop.IDAL
{
public interface IBaseDal<T> where T:class,new()
{
//查询数据库,传入参数是Lambda表达式
IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda);
IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, s>> orderByLambda, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, bool isAsc);
bool DeleteEntity(T entity);
bool UpdateEntity(T entity);
T AddEntity(T entity);
}
}
DAL层添加UserInfoDal实现IUserInfoDal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NCUT.BookShop.IDAL;
using NCUT.BookShop.Model;
using System.Data;
namespace NCUT.BookShop.DAL
{
public class UserInfoDal:IUserInfoDal
{
NCUTBookShopDBEntities db = new NCUTBookShopDBEntities();
public IQueryable<Model.UserInfo> LoadEntities(System.Linq.Expressions.Expression<Func<Model.UserInfo, bool>> whereLambda)
{
return db.UserInfo.Where(whereLambda);
}
public IQueryable<Model.UserInfo> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<Model.UserInfo, s>> orderByLambda, System.Linq.Expressions.Expression<Func<Model.UserInfo, bool>> whereLambda, bool isAsc)
{
var temp = db.UserInfo.Where<UserInfo>(whereLambda);//过滤
totalCount = temp.Count();
if (isAsc)//升序
{
temp =
temp.OrderBy<UserInfo, s>(orderByLambda)
.Skip<UserInfo>((pageIndex - 1)*pageSize)
.Take<UserInfo>(pageSize);
}
else
{
temp =
temp.OrderByDescending<UserInfo, s>(orderByLambda)
.Skip<UserInfo>((pageIndex - 1) * pageSize)
.Take<UserInfo>(pageSize);
}
return temp;
}
public bool DeleteEntity(Model.UserInfo entity)
{
db.Entry<UserInfo>(entity).State = System.Data.Entity.EntityState.Deleted;
return db.SaveChanges() > 0;
}
public bool UpdateEntity(Model.UserInfo entity)
{
db.Entry<UserInfo>(entity).State = System.Data.Entity.EntityState.Modified;
return db.SaveChanges() > 0;
}
public Model.UserInfo AddEntity(Model.UserInfo entity)
{
db.UserInfo.Add(entity);
db.SaveChanges();
return entity;
}
}
}
将UserInfoDal抽象为公共基类BaseDal,剪切
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NCUT.BookShop.Model;
namespace NCUT.BookShop.DAL
{
public class BaseDal<T>where T:class ,new()
{
NCUTBookShopDBEntities db = new NCUTBookShopDBEntities();
public IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda)
{
return db.Set<T>().Where<T>(whereLambda);
}
public IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, s>> orderByLambda, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, bool isAsc)
{
var temp = db.Set<T>().Where<T>(whereLambda);//过滤
totalCount = temp.Count();
if (isAsc)//升序
{
temp =
temp.OrderBy<T, s>(orderByLambda)
.Skip<T>((pageIndex - 1) * pageSize)
.Take<T>(pageSize);
}
else
{
temp =
temp.OrderByDescending<T, s>(orderByLambda)
.Skip<T>((pageIndex - 1) * pageSize)
.Take<T>(pageSize);
}
return temp;
}
public bool DeleteEntity(T entity)
{
db.Entry<T>(entity).State = System.Data.Entity.EntityState.Deleted;
return db.SaveChanges() > 0;
}
public bool UpdateEntity(T entity)
{
db.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified;
return db.SaveChanges() > 0;
}
public T AddEntity(T entity)
{
db.Set<T>().Add(entity);
db.SaveChanges();
return entity;
}
}
}
UserInfoDal继承BaseDal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NCUT.BookShop.IDAL;
using NCUT.BookShop.Model;
using System.Data;
namespace NCUT.BookShop.DAL
{
public class UserInfoDal:BaseDal<UserInfo>,IUserInfoDal
{
}
}