从零开始,搭建博客系统MVC5+EF6搭建框架(1),EF Code frist、实现泛型数据仓储以及业务逻辑

时间:2022-08-27 15:17:17

前言

     从上篇30岁找份程序员的工作(伪程序员的独白)文章开始,我说过我要用我自学的技术,来搭建一个博客系统,也希望大家给点意见,另外我很感谢博客园的各位朋友们,对我那篇算是自我阶段总结文章的评论,在里面能看出有很多种声音,有支持的我的朋友给我加油打气,有分享自己工作经历的朋友,有提出忠肯意见的朋友,有对记事本写代码吐槽的朋友,也有希望让我换个行业的,觉得我可能不适合这个行业朋友,不管怎样,我都接受,都是大家同行的一些忠告,谢谢大家。

首先我要在这里感谢很多博客园里面的大牛,写了很多系列,很多学习资料,让我受益很多,有机会找个时间,我会把我浏览器中收藏的资源都整理出来,分享给大家,其实我会的这些都是自己在博客园看到的文章、在某宝买的视频、QQ群里看群主的分享公开课学的,希望大家多提宝贵意见。

一、框架搭建

从零开始,搭建博客系统MVC5+EF6搭建框架(1),EF Code frist、实现泛型数据仓储以及业务逻辑

首先创建几个文件夹:

01Data用来放链接数据的EF以及创建表的类;

02Core存放数据仓储一些跟数据库链接实现数据操作的部分:

03Services 用于存放业务逻辑处理;

04Common用于放公共应用的工具之类;

05UI mvc页面展示的就放在这里 以及web相关的核心代码

其中除了Wchl.CRM.WebUI创建的是MVC5应用程序,其他的都是创建类库

二、创建数据库

1.创建一个空的EF code frist环境,输入的名字为WMBlogDB

从零开始,搭建博客系统MVC5+EF6搭建框架(1),EF Code frist、实现泛型数据仓储以及业务逻辑

2、选择空的Code Frist模型

从零开始,搭建博客系统MVC5+EF6搭建框架(1),EF Code frist、实现泛型数据仓储以及业务逻辑

3、创建一个models文件存放所有表的类,这里先创建一个用户信息表的类sysUserInfo

sysUserInfo类:

 namespace Wchl.WMBlog.Model.Models
{
public class sysUserInfo
{
/// <summary>
/// 用户ID
/// </summary>
public int uID { get; set; }
/// <summary>
/// 登录账号
/// </summary>
public string uLoginName { get; set; }
/// <summary>
/// 登录密码
/// </summary>
public string uLoginPWD { get; set; }
/// <summary>
/// 真实姓名
/// </summary>
public string uRealName { get; set; }
/// <summary>
/// 状态
/// </summary>
public int uStatus { get; set; }
/// <summary>
/// 备注
/// </summary>
public string uRemark { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public System.DateTime uCreateTime { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public System.DateTime uUpdateTime { get; set; }
}
}

4、创建一个maps文件夹,主要是用来放对表字段进行约束的类sysUserInfoMap

sysUserInfoMap类:

 namespace Wchl.WMBlog.Model.Maps
{
public class sysUserInfoMap:EntityTypeConfiguration<sysUserInfo>
{
public sysUserInfoMap()
{
this.HasKey(u => u.uID);
this.Property(u => u.uLoginName).HasMaxLength(60);
this.Property(u => u.uLoginPWD).HasMaxLength(60);
this.Property(u => u.uRealName).HasMaxLength(60);
}
}
}

关于EntityTypeConfiguration类的用法,大家可以去看看博客园里面一些介绍文章,HasKey设置主键,HasMaxLength字段最大长度。

5、在控制台中创建数据库脚本 Enable-Migrations

从零开始,搭建博客系统MVC5+EF6搭建框架(1),EF Code frist、实现泛型数据仓储以及业务逻辑

6、修改Configuration类配置

从零开始,搭建博客系统MVC5+EF6搭建框架(1),EF Code frist、实现泛型数据仓储以及业务逻辑

7、在WMBlogDB类中重写OnModelCreating方法

从零开始,搭建博客系统MVC5+EF6搭建框架(1),EF Code frist、实现泛型数据仓储以及业务逻辑

重写OnModelCreating方法:

         protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//移除表名为复数
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//自动添加实现EntityTypeConfiguration的类
modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
base.OnModelCreating(modelBuilder);
}

二、仓储层建设

1、在Wchl.WMBlog.IRepository中创建一个类,做为操作数据的父接口IBaseRepository,这里使用泛型来创建

IBaseRepository接口

 namespace Wchl.WMBlog.IRepository.Base
{
public interface IBaseRepository<TEntity> where TEntity:class
{
#region 查询
/// <summary>
/// 单表查询
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> predicate); /// <summary>
/// 多表关联查询
/// </summary>
/// <param name="predicate"></param>
/// <param name="tableNames"></param>
/// <returns></returns>
List<TEntity> QueryJoin(Expression<Func<TEntity, bool>> predicate, string[] tableNames);
/// <summary>
/// 升序查询还是降序查询
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="predicate"></param>
/// <param name="keySelector"></param>
/// <param name="IsQueryOrderBy"></param>
/// <returns></returns>
List<TEntity> QueryOrderBy<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> keySelector, bool IsQueryOrderBy); /// <summary>
/// 升序分页查询还是降序分页
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="pageIndex">第几页</param>
/// <param name="pagesize">一页多少条</param>
/// <param name="rowcount">返回共多少条</param>
/// <param name="predicate">查询条件</param>
/// <param name="keySelector">排序字段</param>
/// <param name="IsQueryOrderBy">true为升序 false为降序</param>
/// <returns></returns>
List<TEntity> QueryByPage<TKey>(int pageIndex, int pagesize, out int rowcount, Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> keySelector, bool IsQueryOrderBy);
#endregion #region 编辑
/// <summary>
/// 通过传入的model加需要修改的字段来更改数据
/// </summary>
/// <param name="model"></param>
/// <param name="propertys"></param>
void Edit(TEntity model, string[] propertys); /// <summary>
/// 直接查询之后再修改
/// </summary>
/// <param name="model"></param>
void Edit(TEntity model);
#endregion #region 删除
void Delete(TEntity model, bool isadded);
#endregion #region 新增
void Add(TEntity model);
#endregion #region 统一提交
int SaverChanges();
#endregion #region 调用存储过程返回一个指定的TResult
List<TResult> RunProc<TResult>(string sql, params object[] pamrs);
#endregion
}
}

2、然后实现IBaseRepository接口,在Wchl.WMBlog.Repository程序集中创建BaseRepository类来实现对数据操作的查询、增加、删除、编辑等。

BaseRepository类

 namespace Wchl.WMBlog.Repository.Base
{
public class BaseRepository<TEntity>: IBaseRepository<TEntity> where TEntity:class
{
WMBlogDB db = new WMBlogDB(); DbSet<TEntity> _dbSet; public BaseRepository()
{
_dbSet = db.Set<TEntity>();
} #region 查询
/// <summary>
/// 单表查询
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> predicate)
{
return _dbSet.Where(predicate).ToList();
} /// <summary>
/// 多表关联查询
/// </summary>
/// <param name="predicate"></param>
/// <param name="tableNames"></param>
/// <returns></returns>
public List<TEntity> QueryJoin(Expression<Func<TEntity, bool>> predicate, string[] tableNames)
{
if (tableNames == null && tableNames.Any() == false)
{
throw new Exception("缺少连表名称");
} DbQuery<TEntity> query = _dbSet; foreach (var table in tableNames)
{
query = query.Include(table);
} return query.Where(predicate).ToList();
} /// <summary>
/// 升序查询还是降序查询
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="predicate"></param>
/// <param name="keySelector"></param>
/// <param name="IsQueryOrderBy"></param>
/// <returns></returns>
public List<TEntity> QueryOrderBy<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> keySelector,bool IsQueryOrderBy)
{
if (IsQueryOrderBy)
{
return _dbSet.Where(predicate).OrderBy(keySelector).ToList();
}
return _dbSet.Where(predicate).OrderByDescending(keySelector).ToList(); } /// <summary>
/// 升序分页查询还是降序分页
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="pageIndex">第几页</param>
/// <param name="pagesize">一页多少条</param>
/// <param name="rowcount">返回共多少条</param>
/// <param name="predicate">查询条件</param>
/// <param name="keySelector">排序字段</param>
/// <param name="IsQueryOrderBy">true为升序 false为降序</param>
/// <returns></returns>
public List<TEntity> QueryByPage<TKey>(int pageIndex,int pagesize,out int rowcount,Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> keySelector, bool IsQueryOrderBy)
{
rowcount = _dbSet.Count(predicate);
if (IsQueryOrderBy)
{
return _dbSet.Where(predicate).OrderBy(keySelector).Skip((pageIndex - 1) * pagesize).Take(pagesize).ToList();
}
else
{
return _dbSet.Where(predicate).OrderByDescending(keySelector).Skip((pageIndex - 1) * pagesize).Take(pagesize).ToList();
}
}
#endregion #region 编辑
/// <summary>
/// 通过传入的model加需要修改的字段来更改数据
/// </summary>
/// <param name="model"></param>
/// <param name="propertys"></param>
public void Edit(TEntity model, string[] propertys)
{
if (model == null)
{
throw new Exception("实体不能为空");
} if (propertys.Any() == false)
{
throw new Exception("要修改的属性至少要有一个");
} //将model追击到EF容器
DbEntityEntry entry = db.Entry(model); entry.State = EntityState.Unchanged; foreach (var item in propertys)
{
entry.Property(item).IsModified = true;
} //关闭EF对于实体的合法性验证参数
db.Configuration.ValidateOnSaveEnabled = false;
} /// <summary>
/// 直接查询之后再修改
/// </summary>
/// <param name="model"></param>
public void Edit(TEntity model)
{
db.Entry(model).State = EntityState.Modified;
}
#endregion #region 删除
public void Delete(TEntity model, bool isadded)
{
if (!isadded) {
_dbSet.Attach(model);
}
_dbSet.Remove(model);
}
#endregion #region 新增
public void Add(TEntity model)
{
_dbSet.Add(model);
}
#endregion #region 统一提交
public int SaverChanges()
{
return db.SaveChanges();
}
#endregion #region 调用存储过程返回一个指定的TResult
public List<TResult> RunProc<TResult>(string sql, params object[] pamrs)
{
return db.Database.SqlQuery<TResult>(sql, pamrs).ToList();
}
#endregion
}
}

三、业务逻辑层父接口和父类创建

1、在Wchl.WMBlog.IServices程序集中创建IBaseServices接口

IBaseServices接口:

 namespace Wchl.WMBlog.IServices.Base
{
public interface IBaseServices<TEntity> where TEntity:class
{
#region 查询
/// <summary>
/// 单表查询
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> predicate); /// <summary>
/// 多表关联查询
/// </summary>
/// <param name="predicate"></param>
/// <param name="tableNames"></param>
/// <returns></returns>
List<TEntity> QueryJoin(Expression<Func<TEntity, bool>> predicate, string[] tableNames);
/// <summary>
/// 升序查询还是降序查询
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="predicate"></param>
/// <param name="keySelector"></param>
/// <param name="IsQueryOrderBy"></param>
/// <returns></returns>
List<TEntity> QueryOrderBy<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> keySelector, bool IsQueryOrderBy); /// <summary>
/// 升序分页查询还是降序分页
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="pageIndex">第几页</param>
/// <param name="pagesize">一页多少条</param>
/// <param name="rowcount">返回共多少条</param>
/// <param name="predicate">查询条件</param>
/// <param name="keySelector">排序字段</param>
/// <param name="IsQueryOrderBy">true为升序 false为降序</param>
/// <returns></returns>
List<TEntity> QueryByPage<TKey>(int pageIndex, int pagesize, out int rowcount, Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> keySelector, bool IsQueryOrderBy);
#endregion #region 编辑
/// <summary>
/// 通过传入的model加需要修改的字段来更改数据
/// </summary>
/// <param name="model"></param>
/// <param name="propertys"></param>
void Edit(TEntity model, string[] propertys); /// <summary>
/// 直接查询之后再修改
/// </summary>
/// <param name="model"></param>
void Edit(TEntity model);
#endregion #region 删除
void Delete(TEntity model, bool isadded);
#endregion #region 新增
void Add(TEntity model);
#endregion #region 统一提交
int SaverChanges();
#endregion #region 调用存储过程返回一个指定的TResult
List<TResult> RunProc<TResult>(string sql, params object[] pamrs);
#endregion
}
}

2、在Wchl.WMBlog.Services程序集创建BaseServices类

BaseServices类

 namespace Wchl.WMBlog.Services.Base
{
public class BaseServices<TEntity>: IBaseServices<TEntity> where TEntity:class
{
public IBaseRepository<TEntity> baseDal = new BaseRepository<TEntity>(); #region 查询
/// <summary>
/// 单表查询
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> predicate)
{
return baseDal.QueryWhere(predicate);
} /// <summary>
/// 多表关联查询
/// </summary>
/// <param name="predicate"></param>
/// <param name="tableNames"></param>
/// <returns></returns>
public List<TEntity> QueryJoin(Expression<Func<TEntity, bool>> predicate, string[] tableNames)
{
return baseDal.QueryJoin(predicate, tableNames); } /// <summary>
/// 升序查询还是降序查询
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="predicate"></param>
/// <param name="keySelector"></param>
/// <param name="IsQueryOrderBy"></param>
/// <returns></returns>
public List<TEntity> QueryOrderBy<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> keySelector, bool IsQueryOrderBy)
{
return baseDal.QueryOrderBy(predicate, keySelector, IsQueryOrderBy);
} /// <summary>
/// 升序分页查询还是降序分页
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <param name="pageIndex">第几页</param>
/// <param name="pagesize">一页多少条</param>
/// <param name="rowcount">返回共多少条</param>
/// <param name="predicate">查询条件</param>
/// <param name="keySelector">排序字段</param>
/// <param name="IsQueryOrderBy">true为升序 false为降序</param>
/// <returns></returns>
public List<TEntity> QueryByPage<TKey>(int pageIndex, int pagesize, out int rowcount, Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> keySelector, bool IsQueryOrderBy)
{ return baseDal.QueryByPage(pageIndex, pagesize,out rowcount, predicate, keySelector, IsQueryOrderBy); }
#endregion #region 编辑
/// <summary>
/// 通过传入的model加需要修改的字段来更改数据
/// </summary>
/// <param name="model"></param>
/// <param name="propertys"></param>
public void Edit(TEntity model, string[] propertys)
{
baseDal.Edit(model, propertys);
} /// <summary>
/// 直接查询之后再修改
/// </summary>
/// <param name="model"></param>
public void Edit(TEntity model)
{
baseDal.Edit(model);
}
#endregion #region 删除
public void Delete(TEntity model, bool isadded)
{
baseDal.Delete(model, isadded);
}
#endregion #region 新增
public void Add(TEntity model)
{
baseDal.Add(model);
}
#endregion #region 统一提交
public int SaverChanges()
{
return baseDal.SaverChanges();
}
#endregion #region 调用存储过程返回一个指定的TResult
public List<TResult> RunProc<TResult>(string sql, params object[] pamrs)
{
return baseDal.RunProc<TResult>(sql, pamrs);
}
#endregion
}
}

到目前为止数据库、仓储层、业务逻辑层的父类和父接口都实现了,下一篇博文就在UI层怎么调用,测试看,成功写成功没。

谢谢大家的支持,多提宝贵意见。