EDP .Net开发框架--业务模型

EDP .Net开发框架--业务模型 开发框架 业务模型 .Net EDP Net --

平台下载地址:https://gitee.com/alwaysinsist/edp


业务模型概述

业务模型管理中所涉及的业务模型,业务模型的属性,业务模型的视图都是可以通过权限设置来实现数据的行(视图),列(属性)权限管控。业务模型是整个EDP平台的核心基础,数据的查询、新增、修改、删除、行列权限都是通过业务模型来实现的。

业务模型管理

按分类管理系统内各个业务模型,对应系统中各个业务对象。

业务模型分类

管理业务模型分类。
image

业务模型

  • 新增业务模型
    image
    image

  • 编辑业务模型
    image
    image
    image

  • 业务模型代码生成
    image
    image

    表设计文档模板,可以包含多个表结构设计
    image

业务模型开发

定义业务模型

创建业务模型类,继承基类ModelPermissionBase。定义好的业务模型可以在上述的业务模型管理功能中进行管理,实现数据行、列权限的管控。

点击查看代码
using System;

namespace School.Model
{    
    using EDP.Common.Model;
    using EDP.Common.Help;

    using EDP.Database;
    using EDP.Framework;

    using EDP.Authentication;    

    /// <summary>
    /// 学生业务对象
    /// </summary>
    [ModelEnitity(TableName = "t_school_student", KeyName = "ID")]
    public class StudentModel: ModelPermissionBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public StudentModel() {
            this.SetDefaultQuerySQL(@"select * 
	from (select a.ID
			,a.Name
			,a.Number
			,a.GenderId
			,a.Age
			,a.Contact
			,a.ContactMobile
			,a.Hobby
			,a.CampusId
			,a.ClassId
			,a.CreateUserId
			,a.CreateUserName
			,a.CreateDepartmentId
			,a.CreateOrganizationId
			,a.CreateTime
			,a.ModifyUserId
			,a.ModifyUserName
			,a.ModifyTime
			,a.Remarks
			,b.Name CampusName
			,c.Name ClassName
			,d.Name SchoolName
			,c.FullCode ClassFullCode
		from t_school_student a
		left join t_sys_organization_unit b on a.CampusId = b.ID
		left join t_sys_organization_unit c on a.ClassId = c.ID
		left join t_sys_organization_unit d on c.FullCode like d.FullCode + '%' and d.Levels = c.Levels - 3) tab");
        }

        #region **----实体属性---------------------------**
        /// <summary>
        /// 主键ID
        /// </summary>
        [ModelEnitity(ColumnName = "ID")]
        public string ID { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        [ModelEnitity(ColumnName = "Name", IsRequired = true, MaxLength = 50)]
        public string Name { get; set; }

        /// <summary>
        /// 编号
        /// <summary>
        [ModelEnitity(ColumnName = "Number")]
        public string Number { get; set; }

        /// <summary>
        /// 性别ID
        /// </summary>
        [ModelEnitity(ColumnName = "GenderId", IsRequired = true)]
        public int? GenderId { get; set; }

        /// <summary>
        /// 年龄
        /// </summary>
        [ModelEnitity(ColumnName = "Age", IsRequired = true)]
        public int? Age { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        [ModelEnitity(ColumnName = "Contact", MaxLength = 50)]
        public string Contact { get; set; }

        /// <summary>
        /// 联系人手机
        /// </summary>
        [ModelEnitity(ColumnName = "ContactMobile", Regex = @"^[1]([3-9])[0-9]{9}$")]
        public string ContactMobile { get; set; }

        /// <summary>
        /// 兴趣爱好
        /// </summary>
        [ModelEnitity(ColumnName = "Hobby", MaxLength = 500)]
        public string Hobby { get; set; }

        /// <summary>
        /// 校区ID
        /// </summary>
        [ModelEnitity(ColumnName = "CampusId")]
        public string CampusId { get; set; }

        /// <summary>
        /// 班级ID
        /// </summary>
        [ModelEnitity(ColumnName = "ClassId", IsRequired = true)]
        public string ClassId { get; set; }

        /// <summary>
        /// 创建人ID
        /// </summary>
        [ModelEnitity(ColumnName = "CreateUserId")]
        public string CreateUserId { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        [ModelEnitity(ColumnName = "CreateUserName")]
        public string CreateUserName { get; set; }

        /// <summary>
        /// 创建部门ID
        /// </summary>
        [ModelEnitity(ColumnName = "CreateDepartmentId")]
        public string CreateDepartmentId { get; set; }

        /// <summary>
        /// 创建机构ID
        /// </summary>
        [ModelEnitity(ColumnName = "CreateOrganizationId")]
        public string CreateOrganizationId { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [ModelEnitity(ColumnName = "CreateTime")]
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 修改人ID
        /// </summary>
        [ModelEnitity(ColumnName = "ModifyUserId")]
        public string ModifyUserId { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        [ModelEnitity(ColumnName = "ModifyUserName")]
        public string ModifyUserName { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        [ModelEnitity(ColumnName = "ModifyTime")]
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [ModelEnitity(ColumnName = "Remarks", MaxLength = 1000)]
        public string Remarks { get; set; }
        #endregion

        #region **----扩展属性---------------------------**
        /// <summary>
        /// 校区名称
        /// </summary>
        public string CampusName { get; set; }

        /// <summary>
        /// 学校名称
        /// </summary>
        public string SchoolName { get; set; }

        /// <summary>
        /// 班级名称
        /// </summary>
        public string ClassName { get; set; }

        /// <summary>
        /// 性别@readonly
        /// </summary>
        public GenderEnum Gender {
            get {
                GenderEnum ret = GenderEnum.None;
                if (this.GenderId.HasValue) {
                    try { ret = (GenderEnum)this.GenderId; }
                    catch { }
                }
                return ret;
            }
        }

        /// <summary>
        /// 性别显示@readonly
        /// </summary>
        public string GenderDisplay {
            get {
                return EnumHelp.GetEnumDescription(this.Gender);
            }
        }

        /// <summary>
        /// 班级全编码
        /// </summary>
        public string ClassFullCode { get; set; }

        /// <summary>
        /// 学校
        /// </summary>
        public Organization.OrganizationModel School { get; set; } = new Organization.OrganizationModel();
        #endregion

        #region **----方法-------------------------------**
        /// <summary>
        /// 创建当前实体对象
        /// </summary>
        /// <param name="db">数据库接口</param>
        public void Create(IDBInstance db = null) {
            if (string.IsNullOrWhiteSpace(this.ID))
                this.ID = Guid.NewGuid().ToString();

            var loginUser = AuthenticationHelp.GetLoginUser(db);
            if (loginUser != null && loginUser.IsLogin) {
                this.CreateUserId = loginUser.UserInfo.ID;
                this.CreateUserName = loginUser.UserInfo.Name;
                this.CreateDepartmentId = loginUser.UserInfo.DepartmentId;
                this.CreateOrganizationId = loginUser.UserInfo.OrganizationId;
            }

            this.CreateTime = DateTime.Now;
            this.ModifyTime = null;
            this.ModifyUserId = null;
            this.ModifyUserName = null;
        }

        /// <summary>
        /// 修改当前实体对象
        /// </summary>
        /// <param name="db">数据库接口</param>
        public void Modify(IDBInstance db = null) {
            this.ModifyTime = DateTime.Now;

            var loginUser = AuthenticationHelp.GetLoginUser(db);
            if (loginUser != null && loginUser.IsLogin) {
                this.ModifyUserId = loginUser.UserInfo.ID;
                this.ModifyUserName = loginUser.UserInfo.Name;
            }

            this.ModifyTime = DateTime.Now;
            this.CreateDepartmentId = null;
            this.CreateOrganizationId = null;
            this.CreateTime = null;
            this.CreateUserId = null;
            this.CreateUserName = null;
        }

        /// <summary>
        /// 修改当前实体对象
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <param name="db">数据库接口</param>
        public void Modify(string keyValue, IDBInstance db = null) {
            this.ID = keyValue;
            this.Modify(db);
        }
        #endregion
    }
}

image

[ModelEnitity(TableName = "t_school_student", KeyName = "ID")]
TableName:表名
KeyName:主键对应的属性名

[ModelEnitity(ColumnName = "Name", IsRequired = true, MaxLength = 50)]
ColumnName:字段名称
IsRequired:是否必须,用于验证属性值是否必须有值
MaxLength:最大长度,属性值的最大长度

[ModelEnitity(ColumnName = "ContactMobile", Regex = @"^[1]([3-9])[0-9]{9}$")]
Regex:正则表达式,用于验证属性值格式

点击查看代码
/// <summary>
/// 构造函数
/// </summary>
public StudentModel() {
            this.SetDefaultQuerySQL(@"select * 
	from (select a.ID
			,a.Name
			,a.Number
			,a.GenderId
			,a.Age
			,a.Contact
			,a.ContactMobile
			,a.Hobby
			,a.CampusId
			,a.ClassId
			,a.CreateUserId
			,a.CreateUserName
			,a.CreateDepartmentId
			,a.CreateOrganizationId
			,a.CreateTime
			,a.ModifyUserId
			,a.ModifyUserName
			,a.ModifyTime
			,a.Remarks
			,b.Name CampusName
			,c.Name ClassName
			,d.Name SchoolName
			,c.FullCode ClassFullCode
		from t_school_student a
		left join t_sys_organization_unit b on a.CampusId = b.ID
		left join t_sys_organization_unit c on a.ClassId = c.ID
		left join t_sys_organization_unit d on c.FullCode like d.FullCode + '%' and d.Levels = c.Levels - 3) tab");

            //引入命名空间 using EDP.Common;
            //default:注册表配置中数据库连接配置所对应的"Key"值
            //若没有设置连线字串,则取注册表配置中的默认数据库连接
            //this.SetConnectionString(Config.Database["default"]); 示例项目使用默认数据库连接,不需要设置
        }
在构造函数中通过SetDefaultQuerySQL()方法设置默认查询SQL,通过SetConnectionString()方法设置数据库连接字符串(若没设置则取默认数据库连接字串)。

public string CampusName { get; set; }
扩展属性是默认查询SQL中所查询的非业务模型映射的表中的字段。

点击查看代码
/// <summary>
/// 创建当前实体对象
/// </summary>
/// <param name="db">数据库接口</param>
public void Create(IDBInstance db = null) {
    if (string.IsNullOrWhiteSpace(this.ID))
        this.ID = Guid.NewGuid().ToString();

    var loginUser = AuthenticationHelp.GetLoginUser(db);
    if (loginUser != null && loginUser.IsLogin) {
        this.CreateUserId = loginUser.UserInfo.ID;
        this.CreateUserName = loginUser.UserInfo.Name;
        this.CreateDepartmentId = loginUser.UserInfo.DepartmentId;
        this.CreateOrganizationId = loginUser.UserInfo.OrganizationId;
    }

    this.CreateTime = DateTime.Now;
    this.ModifyTime = null;
    this.ModifyUserId = null;
    this.ModifyUserName = null;
}

/// <summary>
/// 修改当前实体对象
/// </summary>
/// <param name="db">数据库接口</param>
public void Modify(IDBInstance db = null) {
    this.ModifyTime = DateTime.Now;

    var loginUser = AuthenticationHelp.GetLoginUser(db);
    if (loginUser != null && loginUser.IsLogin) {
        this.ModifyUserId = loginUser.UserInfo.ID;
        this.ModifyUserName = loginUser.UserInfo.Name;
    }

    this.ModifyTime = DateTime.Now;
    this.CreateDepartmentId = null;
    this.CreateOrganizationId = null;
    this.CreateTime = null;
    this.CreateUserId = null;
    this.CreateUserName = null;
}

/// <summary>
/// 修改当前实体对象
/// </summary>
/// <param name="keyValue">主键值</param>
/// <param name="db">数据库接口</param>
public void Modify(string keyValue, IDBInstance db = null) {
    this.ID = keyValue;
    this.Modify(db);
}
在业务对象上提供Create()和Modify()扩展方法,用于在创建和修改业务时调用,给业务对象必要属性赋值。

定义查询对象

查询对象是用来实现业务对象的查询,通过属性来设置查询条件。需要继承QueryModelBase基类。

点击查看代码
using System;

namespace School.Model
{
    using EDP.Common;
    using EDP.Common.Model;

    /// <summary>
    /// 学生信息查询对象
    /// </summary>
    [QueryModel] //标记类的查询对象特性
    public class StudentQueryModel : QueryModelBase //继承QueryModelBase
    {
        /// <summary>
        /// 姓名
        /// </summary>
        [QueryModel(Operator = QueryOperator.like, ColumnName = "Name")]
        public string Name { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        [QueryModel(Operator = QueryOperator.eq, ColumnName = "GenderId")]
        public int? GenderId { get; set; }

        /// <summary>
        /// 校区
        /// </summary>
        [QueryModel(Operator = QueryOperator.eq, ColumnName = "CampusId")]
        public string CampusId { get; set; }

        /// <summary>
        /// 班级
        /// </summary>
        [QueryModel(Operator = QueryOperator.eq, ColumnName = "ClassId")]
        public string ClassId { get; set; }

        /// <summary>
        /// 班级全编码
        /// </summary>
        /// <example>ClassFullCode like '值%'</example>
        [QueryModel(Operator = QueryOperator.likeRight, ColumnName = "ClassFullCode")]
        public string ClassFullCode { get; set; }

        /// <summary>
        /// 编号姓名关键字
        /// Number和Name都进行like查询
        /// </summary>
        /// <example>Number like '%关键字%' or Name like '%值%'</example>
        [QueryModel(Operator = QueryOperator.like, ColumnNameList = "Number;Name")]
        public string NumberNameKeywrod { get; set; }

        /// <summary>
        /// 分页对象
        /// </summary>
        public PageModel Page { get; set; } = new PageModel();
    }
}

[QueryModel(Operator = QueryOperator.like, ColumnName = "Name")]
Operator:查询操作符(=、>、<、>=、like等)
ColumnName:对应业务对象的属性(实体属性,扩展属性),只要是默认查询SQL中包含的字段都可以作为查询对象的属性(查询条件)

业务模型使用

  • 业务模型属性验证
    var verify = item.Verify(keyValue);
    此处通过Verify()方法验证业务模型各属性值,通过属性验证标记(IsRequired,MaxLength, Regex)进行验证。

  • 业务模型新增

点击查看代码
item.Create(db);
item.ValidateSQL = string.Format("where not exists (select 1 from t_school_student where Number = '{0}')", item.Number);
result = item.InsertModel(db);
通过业务模型Create()方法创建当前实体对象。
通过ValidateSQL属性值确保不会有重复的Number值插入到数据库。
调用InsertModel()方法新增业务模型数据。
  • 业务模型修改
点击查看代码
item.Modify(keyValue, db);
item.Number = null; //编号不能修改
result = item.UpdateModel(db);
通过业务模型Modify()方法修改当前实体对象。
调用UpdateModel()方法修改业务模型数据。
  • 业务模型查询
点击查看代码
ResultModel<StudentModel> ret = new ResultModel<StudentModel>();
ret.Content = new StudentModel();
ret.KeyValue = keyValue;
ret.Content.ID = keyValue;
ret.Content.Query(db);
使用统一的返回对象 ResultModel<StudentModel>。
实例化ret.Content并设置操作的主键值ret.KeyValue = keyValue。
设置查询的主键值 ret.Content.ID = keyValue。
调用业务对象Query()方法 ret.Content.Query(db)。
执行Query()查询后,根据权限设置给ret.Content赋值。
ret.Content.AccessPropertyList中返回有权限的属性名称。
  • 业务模型列表查询
点击查看代码
ResultModel<List<StudentModel>> ret = new ResultModel<List<StudentModel>>();
ret.Content = new List<StudentModel>();
ret.Content.Query(queryModel.Page, queryModel, db);
ret.Page = queryModel.Page;
使用统一的返回对象 ResultModel<List<StudentModel>>。
实例化 ret.Content = new List<StudentModel>()。
调用业务对象Query()方法 ret.Content.Query(queryModel.Page, queryModel, db)。
queryModel为查询对象StudentQueryModel的实例。
执行Query()查询后,根据权限设置给ret.Content赋值(有权限的数据集合)。
ret.Content[0].AccessPropertyList中返回有权限的属性名称。
  • 业务模型删除
点击查看代码
StudentModel item = new StudentModel();
item.Id = keyValue;
item.DeleteModel();
实例化StudentModel。
设置主键值 item.Id = keyValue。
调用业务对象DeleteModel()方法 item.DeleteModel()。
评论