EDP .Net開發框架--業務模型

風輕·雲淡發表於2024-05-19

平臺下載地址: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中包含的欄位都可以作為查詢物件的屬性(查詢條件)

業務模型使用

詳見示例專案StudentBLL.cs

  • 業務模型屬性驗證
    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()。

相關文章