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 LoadEntities(System.Linq.Expressions.Expression>whereLambda );
IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount,System.Linq.Expressions.Expression> orderByLambda,System.Linq.Expressions.Expression> 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 where T:class,new()
{
//查询数据库,传入参数是Lambda表达式
IQueryable LoadEntities(System.Linq.Expressions.Expression> whereLambda);
IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression> orderByLambda, System.Linq.Expressions.Expression> whereLambda, bool isAsc);
bool DeleteEntity(T entity);
bool UpdateEntity(T entity);
T AddEntity(T entity);
}
}
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 LoadEntities(System.Linq.Expressions.Expression> whereLambda)
{
return db.UserInfo.Where(whereLambda);
}
public IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression> orderByLambda, System.Linq.Expressions.Expression> whereLambda, bool isAsc)
{
var temp = db.UserInfo.Where(whereLambda);//过滤
totalCount = temp.Count();
if (isAsc)//升序
{
temp =
temp.OrderBy(orderByLambda)
.Skip((pageIndex - 1)*pageSize)
.Take(pageSize);
}
else
{
temp =
temp.OrderByDescending(orderByLambda)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize);
}
return temp;
}
public bool DeleteEntity(Model.UserInfo entity)
{
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
return db.SaveChanges() > 0;
}
public bool UpdateEntity(Model.UserInfo entity)
{
db.Entry(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;
}
}
}
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 BaseDalwhere T:class ,new()
{
NCUTBookShopDBEntities db = new NCUTBookShopDBEntities();
public IQueryable LoadEntities(System.Linq.Expressions.Expression> whereLambda)
{
return db.Set().Where(whereLambda);
}
public IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression> orderByLambda, System.Linq.Expressions.Expression> whereLambda, bool isAsc)
{
var temp = db.Set().Where(whereLambda);//过滤
totalCount = temp.Count();
if (isAsc)//升序
{
temp =
temp.OrderBy(orderByLambda)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize);
}
else
{
temp =
temp.OrderByDescending(orderByLambda)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize);
}
return temp;
}
public bool DeleteEntity(T entity)
{
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
return db.SaveChanges() > 0;
}
public bool UpdateEntity(T entity)
{
db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
return db.SaveChanges() > 0;
}
public T AddEntity(T entity)
{
db.Set().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,IUserInfoDal
{
}
}