目录
MVC5+EF6 简易版CMS(非接口) 第二章:建数据模型
MVC5+EF6 简易版CMS(非接口) 第三章:数据存储和业务处理
MVC5+EF6 简易版CMS(非接口) 第四章:使用业务层方法,以及关联表解决方案
先来了解下各项的引用关系
FytCms.DALMSSQL=》Domain.Entity、EntityFramework
BusinessLogic.Server=》FytCms.DALMSSQL
FytCms.Web=》FytCms.Helper、Domain.Entity,BusinessLogic.Server,FytCms.Common
ok 简单吧,哇咔咔
1、在Core.Repository文件夹下,建FytCms.DALMSSQL类库
这个类库主要的意思就是操作sql server的,当然你可以建FytCms.DALMySql类库,在类库增加方法
主要使用EF操作数据
/// <summary>
/// 实现接口基类
/// <remarks>创建:2015.05.04 FUYU
/// </remarks>
/// </summary>
/// <typeparam name="T">类型</typeparam>
public class BaseRepository<T> where T : class ,new()
{
/// <summary>
/// 获得数据上下文
/// </summary>
protected FytSysDbContext FytContext = new FytSysDbContext(); #region 1、添加
/// <summary>
/// 添加
/// </summary>
/// <param name="entity">数据实体</param>
/// <param name="isSave">是否保存</param>
/// <returns>添加后的数据实体</returns>
public T Add(T entity, bool isSave = true)
{
FytContext.Set<T>().Add(entity);
if (isSave) FytContext.SaveChanges();
return entity;
} /// <summary>
/// 同时增加多条数据到一张表(事务处理)
/// </summary>
/// <param name="entitys"></param>
/// <returns></returns>
public bool AddEntity(List<T> entitys)
{
foreach (var entity in entitys)
{
FytContext.Entry<T>(entity).State = System.Data.Entity.EntityState.Added;
}
return FytContext.SaveChanges() > ;
}
#endregion #region 2、修改
/// <summary>
/// 更新
/// </summary>
/// <param name="entity">数据实体</param>
/// <param name="isSave">是否保存</param>
/// <returns>是否成功</returns>
public bool Update(T entity, bool isSave = true)
{
FytContext.Set<T>().Attach(entity);
FytContext.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified;
return !isSave || FytContext.SaveChanges() > ;
} /// <summary>
/// 同时更新多条数据(事务处理)
/// </summary>
/// <param name="entitys">数据实体</param>
/// <returns>是否成功</returns>
public bool Update(List<T> entitys)
{
entitys.ForEach(entity =>
{
FytContext.Set<T>().Attach(entity);
FytContext.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified;//将所有属性标记为修改状态
});
return FytContext.SaveChanges() > ;
} /// <summary>
/// 修改一条数据,会修改指定列的值
/// </summary>
/// <returns>是否成功</returns>
public bool Update(T entity, params string[] proNames)
{
DbEntityEntry<T> dbee = FytContext.Entry<T>(entity);
if (dbee.State == System.Data.Entity.EntityState.Detached)
{
FytContext.Set<T>().Attach(entity);
}
dbee.State = System.Data.Entity.EntityState.Unchanged;//先将所有属性状态标记为未修改
proNames.ToList().ForEach(c => dbee.Property(c).IsModified = true);//将要修改的属性状态标记为修改
return FytContext.SaveChanges() > ;
}
#endregion #region 3、删除
/// <summary>
/// 删除
/// </summary>
/// <param name="entity">数据实体</param>
/// <param name="isSave">是否保存</param>
/// <returns>是否成功</returns>
public bool Delete(T entity, bool isSave = true)
{
FytContext.Set<T>().Attach(entity);
FytContext.Entry<T>(entity).State = System.Data.Entity.EntityState.Deleted;
return !isSave || FytContext.SaveChanges() > ;
} /// <summary>
/// 根据条件删除
/// </summary>
/// <param name="delWhere">删除表达式</param>
/// <param name="isSave">是否保存</param>
/// <returns>是否成功</returns>
public bool DeleteBy(Expression<Func<T, bool>> delWhere, bool isSave = true)
{
//3.1查询要删除的数据
List<T> listDeleting = FytContext.Set<T>().Where(delWhere).ToList();
//3.2将要删除的数据 用删除方法添加到 EF 容器中
listDeleting.ForEach(u =>
{
FytContext.Set<T>().Attach(u);//先附加到 EF容器
FytContext.Set<T>().Remove(u);//标识为 删除 状态
});
return !isSave || FytContext.SaveChanges() > ;
} /// <summary>
/// 批量物理删除数据,也可以用作单个物理删除--此方法适用于id为int类型的表--性能会比先查询后删除快
/// </summary>
/// <param name="ids">ID集合1,2,3</param>
/// <returns>是否成功</returns>
public bool DeletePhysics(string ids)
{
var tableName = typeof(T).Name;//获取表名
var sql = string.Format("delete from {0} where id in({1})", tableName, ids);
return FytContext.Database.ExecuteSqlCommand(sql) > ;
}
#endregion #region 4、查询
/// <summary>
/// 是否存在
/// </summary>
/// <param name="anyLambda">查询表达式</param>
/// <returns>布尔值</returns>
public bool Exist(Expression<Func<T, bool>> anyLambda)
{
return FytContext.Set<T>().Any(anyLambda);
} /// <summary>
/// 查询记录数
/// </summary>
/// <param name="predicate">条件表达式</param>
/// <returns>记录数</returns>
public int Count(Expression<Func<T, bool>> predicate)
{
return FytContext.Set<T>().Count(predicate);
} /// <summary>
/// 查询数据根据ID主键(优先)
/// </summary>
/// <param name="id">主键</param>
/// <returns>实体</returns>
public T GetModel(int id)
{
return FytContext.Set<T>().Find(id);
} /// <summary>
/// 查询数据
/// </summary>
/// <param name="whereLambda">查询表达式</param>
/// <returns>实体</returns>
public T GetModel(Expression<Func<T, bool>> whereLambda)
{
var entity =FytContext.Set<T>().FirstOrDefault<T>(whereLambda);
return entity;
} /// <summary>
/// 查找数据列表
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="whereLamdba">查询表达式</param>
/// <param name="orderLambda">排序表达式</param>
/// <param name="isAsc">是否升序</param>
/// <returns></returns>
public IQueryable<T> GetList<TKey>(Expression<Func<T, bool>> whereLamdba, Expression<Func<T, TKey>> orderLambda, bool isAsc)
{
return isAsc ? FytContext.Set<T>().Where(whereLamdba).OrderBy(orderLambda) :
FytContext.Set<T>().Where(whereLamdba).OrderByDescending(orderLambda);
}
#endregion #region 5、分页 /// <summary>
/// 查找分页数据列表
/// </summary>
/// <typeparam name="TKey">排序</typeparam>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">每页记录数</param>
/// <param name="rows">总行数</param>
/// <param name="totalPage">分页总数</param>
/// <param name="whereLambda">查询表达式</param>
/// <param name="isAsc">是否升序</param>
/// <param name="orderBy">排序表达式</param>
/// <returns></returns>
public IQueryable<T> GetPageList<TKey>(int pageIndex, int pageSize, out int rows, out int totalPage,
Expression<Func<T, bool>> whereLambda, bool isAsc, Expression<Func<T, TKey>> orderBy)
{
var temp = FytContext.Set<T>().Where<T>(whereLambda);
rows = temp.Count();
totalPage = rows % pageSize == ? rows / pageSize : rows / pageSize + ;
temp = isAsc ? temp.OrderBy<T, TKey>(orderBy) : temp.OrderByDescending<T, TKey>(orderBy);
return temp.Skip<T>(pageSize * (pageIndex - )).Take<T>(pageSize); }
#endregion
}
数据操作做好,下一步就是业务调用数据操作,
2、建立业务类
在BusinessLogic.Server文件夹下增加BusinessLogic.Server类库,
在该类库引用FytMsys.DALMSSQL
新建类BaseServer 具体代码如下
/// <summary>
/// BLL层抽象类
/// <remarks>创建:2015.05.04 FUYU
/// </remarks>
/// </summary>
/// <typeparam name="T">类型</typeparam>
public class BaseServer<T> where T : class,new()
{
/// <summary>
/// 实例化数据层方法
/// </summary>
protected BaseRepository<T> Repository=new BaseRepository<T>(); #region 1、添加
/// <summary>
/// 添加
/// </summary>
/// <param name="entity">数据实体</param>
/// <param name="isSave">是否保存</param>
/// <returns>添加后的数据实体</returns>
public T Add(T entity, bool isSave = true)
{
return Repository.Add(entity,isSave);
} /// <summary>
/// 同时增加多条数据到一张表(事务处理)
/// </summary>
/// <param name="entitys"></param>
/// <returns></returns>
public bool AddEntity(List<T> entitys)
{
return Repository.AddEntity(entitys);
}
#endregion #region 2、修改
/// <summary>
/// 更新
/// </summary>
/// <param name="entity">数据实体</param>
/// <param name="isSave">是否保存</param>
/// <returns>是否成功</returns>
public bool Update(T entity, bool isSave = true)
{
return Repository.Update(entity,isSave);
} /// <summary>
/// 同时更新多条数据(事务处理)
/// </summary>
/// <param name="entitys">数据实体</param>
/// <returns>是否成功</returns>
public bool Update(List<T> entitys)
{
return Repository.Update(entitys);
} /// <summary>
/// 修改一条数据,会修改指定列的值
/// </summary>
/// <returns>是否成功</returns>
public bool Update(T entity, params string[] proNames)
{
return Repository.Update(entity,proNames);
}
#endregion #region 3、删除
/// <summary>
/// 删除
/// </summary>
/// <param name="entity">数据实体</param>
/// <param name="isSave">是否保存</param>
/// <returns>是否成功</returns>
public bool Delete(T entity, bool isSave = true)
{
return Repository.Delete(entity,isSave);
} /// <summary>
/// 根据条件删除
/// </summary>
/// <param name="delWhere">删除表达式</param>
/// <param name="isSave">是否保存</param>
/// <returns>是否成功</returns>
public bool DeleteBy(Expression<Func<T, bool>> delWhere, bool isSave = true)
{
return Repository.DeleteBy(delWhere,isSave);
} /// <summary>
/// 批量物理删除数据,也可以用作单个物理删除--此方法适用于id为int类型的表--性能会比先查询后删除快
/// </summary>
/// <param name="ids">ID集合1,2,3</param>
/// <returns>是否成功</returns>
public bool DeletePhysics(string ids)
{
return Repository.DeletePhysics(ids);
}
#endregion #region 4、查询
/// <summary>
/// 是否存在
/// </summary>
/// <param name="anyLambda">查询表达式</param>
/// <returns>布尔值</returns>
public bool Exist(Expression<Func<T, bool>> anyLambda)
{
return Repository.Exist(anyLambda);
} /// <summary>
/// 查询记录数
/// </summary>
/// <param name="predicate">条件表达式</param>
/// <returns>记录数</returns>
public int Count(Expression<Func<T, bool>> predicate)
{
return Repository.Count(predicate);
} /// <summary>
/// 查询数据根据ID主键(优先)
/// </summary>
/// <param name="id">主键</param>
/// <returns>实体</returns>
public T GetModel(int id)
{
return Repository.GetModel(id);
} /// <summary>
/// 查询数据
/// </summary>
/// <param name="whereLambda">查询表达式</param>
/// <returns>实体</returns>
public T GetModel(Expression<Func<T, bool>> whereLambda)
{
return Repository.GetModel(whereLambda);
} /// <summary>
/// 查找数据列表
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="whereLamdba">查询表达式</param>
/// <param name="orderLambda">排序表达式</param>
/// <param name="isAsc">是否升序</param>
/// <returns></returns>
public IQueryable<T> GetList<TKey>(Expression<Func<T, bool>> whereLamdba, Expression<Func<T, TKey>> orderLambda, bool isAsc)
{
return Repository.GetList(whereLamdba,orderLambda,isAsc);
}
#endregion #region 5、分页 /// <summary>
/// 查找分页数据列表
/// </summary>
/// <typeparam name="TKey">排序</typeparam>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">每页记录数</param>
/// <param name="rows">总行数</param>
/// <param name="totalPage">分页总数</param>
/// <param name="whereLambda">查询表达式</param>
/// <param name="isAsc">是否升序</param>
/// <param name="orderBy">排序表达式</param>
/// <returns></returns>
public IQueryable<T> GetPageList<TKey>(int pageIndex, int pageSize, out int rows, out int totalPage,
Expression<Func<T, bool>> whereLambda, bool isAsc, Expression<Func<T, TKey>> orderBy)
{
return Repository.GetPageList(pageIndex,pageSize,out rows,out totalPage,whereLambda,isAsc,orderBy); }
#endregion }
可以在次扩展业务方法,也可以新建一个自定义类库引用BusinessLogic.Server 在自定义类库新建自定义类扩展方法
要一步要做的就是WebUi层、