CRM框架小知识以及增删查改逻辑代码

CRM:客户关系管理系统

总体框架 MVC4 + EF5 + Autofac 替代工厂层连接各个层之间的关系

基本框架

01实体层 02仓储层 03业务层 04公共层 05UI 用MVC框架
------------------------------------------
01实体层 EF

01实体层 中 为了方便模型注解

用T4模板分别生成 EF实体类的部分类 和 贴标签的类

EF实体类的部分类

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using crm.model.ModelView;
[MetadataType(typeof(sysFunctionView))]
public partial class sysFunction
{

}

贴标签的类

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public partial class sysMenusView
{
public int mID { get; set; }
public int mParentID { get; set; }
[DisplayName("菜单名称"), Required(ErrorMessage = "菜单名称非空")]
public string mName { get; set; }
[DisplayName("地址url"), Required(ErrorMessage = "地址url非空")]
public string mUrl { get; set; }
[DisplayName("区域"), Required(ErrorMessage = "区域非空")]
public string mArea { get; set; }
[DisplayName("控制器"), Required(ErrorMessage = "控制器非空")]
public string mController { get; set; }
[DisplayName("方法"), Required(ErrorMessage = "方法非空")]
public string mAction { get; set; }
[DisplayName("排序编号"), Required(ErrorMessage = "排序编号非空")]
public int mSortid { get; set; }
[DisplayName("状态")]
public int mStatus { get; set; }
[DisplayName("菜单图标"), Required(ErrorMessage = "菜单图标非空")]
public string mPicname { get; set; }
public int mLevel { get; set; }
public string mExp1 { get; set; }
public Nullable<int> mExp2 { get; set; }
public int mCreatorID { get; set; }
public System.DateTime mCreateTime { get; set; }
public Nullable<int> mUpdateID { get; set; }
public System.DateTime mUpdateTime { get; set; }
}

注意点: 引用T4 模板的时候 首先 修改 此处

const string inputFile = @"..crm.modelJKCRM.edmx";

对于T4模板的使用 更好的理解 里面生成空间命名,类名,字段,属性,

知道怎么去修改T4 模板按照自己的需要 生成类


-----------------------------

02仓储层 两个类库 DAL 与 接口 IDAL

DAL类库中:

泛型 BaseDAL<TEntity> 类 和 BaseDbContext 类

还有一系列通过T4模板生成的'TEntity'DAL 类 这些类

继承BaseDAL<TEntity> 并实现接口 I'TEntity'DAL

public partial class 'TEntity'DAL:BaseDAL<TEntity>,I'TEntity'DAL


BaseDbContext 类 继承 DbContext 自定义EF容器

using System.Data.Entity;

public class BaseDbContext:DbContext
{
public BaseDbContext()
: base("name=JKCRMEntities")
{

}
}

首先有 泛型 BaseDAL要实现接口IDAL --》

public class BaseDAL<TEntity>:IBaseDAL<TEntity> where TEntity :class

BaseDAL 中编写 增,删,改,查 逻辑代码

定义一个DbSet<T> 私有变量在构造函数中初始化

DbSet<TEntity> _dbset;

public BaseDAL()
{
_dbset = db.Set<TEntity>();
}

--------增,删,改,查 逻辑代码

#region 3.0 查询相关方法

#region 3.0.1 带条件查询方法
/// <summary>
/// 带条件查询方法
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public IQueryable<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where)
{
return _dbset.Where(where);
}
#endregion

#region 3.0.2 连表查询

public IQueryable<TEntity> QueryJoin(Expression<Func<TEntity, bool>> where, string[] tableNames)
{
//1.0 参数合法性检查
if (tableNames == null || tableNames.Any() == false)
{
throw new Exception("连表方法至少要有一个表名称");
}

//2.0 定义DbQuery<Tentity>
DbQuery<TEntity> query = _dbset;

//3.0 遍历
foreach (var item in tableNames)
{
query = query.Include(item);
}

//4.0 带上条件查询
return query.Where(where);
}

#endregion

#region 3.0.3 分页查询

public IQueryable<TEntity> QueryByPage<TKey>(int pageindex
, int pagesize
, out int totalcount
, Expression<Func<TEntity, bool>> where
, Expression<Func<TEntity, TKey>> order)
{
//1.0 计算出跳过的总行数
int skipCount = (pageindex - 1) * pagesize;

//2.0 计算出总行数
totalcount = _dbset.Count(where);

//3.0 返回
return _dbset.Where(where).OrderByDescending(order).Skip(skipCount).Take(pagesize);
}

#endregion

#region 3.0.4 统一的执行sql语句的方法

/// <summary>
/// 利用EF直接执行sql语句或者存储过程
/// 注意如果执行的是存储过程,则不需要exec 只需要传入 存储过程的名称 参数1,参数2......
/// 格式: Usp_GetList 1,2
/// </summary>
/// <typeparam name="TElement"></typeparam>
/// <param name="sql"></param>
/// <param name="ps"></param>
/// <returns></returns>
public List<TElement> RunSql<TElement>(string sql, params object[] ps)
{
return db.Database.SqlQuery<TElement>(sql, ps).ToList();
}

#endregion

#endregion

#region 4.0 新增

public void Add(TEntity model)
{
//1.0 参数合法性验证
if (model == null)
{
throw new Exception("实体非空");
}

//2.0 追加到EF容器中并且将状态修改成added
_dbset.Add(model);
}

#endregion

#region 5.0 按需编辑(new 具体的实体,手工修改其状态为unchanged再修改属性的ismodified为true)

public void Edit(TEntity model, string[] properts)
{
//1.0 参数合法性验证
if (model == null)
{
throw new Exception("实体非空");
}
if (properts == null || properts.Any() == false)
{
throw new Exception("要修改的属性至少要有一个");
}

//2.0 将model追加到EF容器中
var entry = db.Entry(model);
entry.State = System.Data.EntityState.Unchanged;

//3.0 遍历
foreach (var item in properts)
{
entry.Property(item).IsModified = true;
}

//4.0 关闭EF实体属性合法性验证
db.Configuration.ValidateOnSaveEnabled = false;
}

#endregion

#region 6.0 物理删除

public void Delete(TEntity model, bool isAddedContext)
{
//1.0 参数合法性验证
if (model == null)
{
throw new Exception("实体非空");
}

//2.0 如果未追加则追加
if (isAddedContext == false)
{
_dbset.Attach(model);
}

//3.0 将状态修改为deleted
_dbset.Remove(model);
}

#endregion

#region 7.0 统一保存
public int SaveChanges()
{
return db.SaveChanges();
}
#endregion

------

IDAL类库中:接口IBaseDAL 和通过T4模板生成的一些了接口 I'Tentity'DAL

这一系列接口 继承 接口IBaseDAL

public partial interface I'Tentity'DAL:IBaseDAL<Tentity>
---------------------------------------------------------------
整体DAL 成 之间关系

1,通过T4模板生成的 'TEntity'DAL 继承泛型类BaseDAL<TEntity>

并实现通过T4模板生成的 I'TEntity'DAL 接口

2,通过T4模板生成的 I'TEntity'DAL 接口 继承IBaseDAL<TEntity> 接口

3,BaseDAL<TEntity> 实现 IBaseDAL<TEntity> 接口

'TEntity'DAL:BaseDAL<TEntity>,I'TEntity'DAL

I'Tentity'DAL:IBaseDAL<Tentity>

BaseDAL<TEntity>:IBaseDAL<TEntity> where TEntity :class
--------------------------------------------------------------

03业务层

两个类库 BLL 和 IBLL

类库BLL 中 包含 BaseBLL<TEntity> 泛型类 和通过T4模板生成的一些类'TEntity'BLL

关系:1, BaseBLL<TEntity> 泛型类 继承接口IBaseBLL<TEntity>

public class BaseBLL<TEntity> : IBaseBLL<TEntity> where TEntity : class

2, 通过T4模板生成的一些类 继承 BaseBLL<TEntity> 泛型类

并且实现通过T4模板生成的一些接口 I'TEntity'BLL

public partial class 'TEntity'BLL:BaseBLL<TEntity>,I'TEntity'BLL

在 BaseBLL<TEntity> 中 :

public class BaseBLL<TEntity> : IBaseBLL<TEntity> where TEntity : class
{
protected IBaseDAL<TEntity> basedal;
#region 3.0 查询相关方法

#region 3.0.1 带条件查询方法
/// <summary>
/// 带条件查询方法
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public IQueryable<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where)
{
return basedal.QueryWhere(where);
}
#endregion

等 .....


在通过T4模板生成的一些类'TEntity'BLL 构造函数中 直接T4模板生成

public partial class sysKeyValueBLL:BaseBLL<sysKeyValue>,IsysKeyValueBLL
{
IsysKeyValueDAL dal;
public sysKeyValueBLL(IsysKeyValueDAL dal)
{
this.dal = dal;
base.basedal = dal;
}

在IBLL 类库中

包含 接口IBaseBLL 和通过T4模板生成的一些了接口 I'TEntity'BLL

public partial interface IsysKeyValueBLL:IBaseBLL<sysKeyValue>

人的本事不是与生俱来的,不是你掌握了多少,而是当你面对一个未知问题的时候,你能用多少时间来掌握!
原文地址:https://www.cnblogs.com/dianshen520/p/4349136.html