abp(net core)+easyui+efcore倉儲系統——領域層建立實體(三)

DotNet菜園發表於2019-06-05

abp(net core)+easyui+efcore倉儲系統目錄

abp(net core)+easyui+efcore倉儲系統——ABP總體介紹(一)

abp(net core)+easyui+efcore倉儲系統——解決方案介紹(二)

 

      在上二篇文章中我們簡單介紹了一下ABP.TPLMS系統的概況,已經對ABP的體系結構以及專案結構有了一個初步的瞭解。在這一篇文章中我們主要和領域層打交道,主要是建立實體與進行遷移。接下來我們開始建立Module實體。

一、建立Module實體

      實體是DDD(領域驅動設計)的核心概念之一。Eirc Evans是這樣描述的實體的:“它根本上不是通過屬性定義的,而是通過一系列連續性和標識定義的”。因此,實體都有Id屬性並且都儲存到資料庫中。一個實體一般會對映到資料庫的一張表。現在我們來完成以下任務:在領域層建立一個Entitys資料夾,並在這個資料夾中建立Module實體類。

1. 在Visual Studio 2017的“解決方案資源管理器”中,右鍵單擊“ABP.TPLMS.Core”專案。 選擇“新增” > “新建資料夾”。如下圖。

2.將資料夾命名為“Entitys”。

3. 右鍵單擊“Entitys”資料夾,然後選擇“新增” > “類”。 將類命名為 Module,然後選擇“新增”。如下圖。

 

4.ABP中所有的實體類都繼承自Entity,而Entity實現了IEntity介面;而IEntity介面是一個泛型介面,通過泛型指定主鍵Id型別,預設的Entity的主鍵型別是int型別。如下圖。

 
5.建立Module類,肯定需要儲存建立時間,可以通過實現審計模組中的IHasCreationTime來實現這種通用功能。如下圖。

 

6.  abp中實體是派生於Entity類,先看一下我們在Core層新建的Module類。程式碼如下:

using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using Abp.Timing;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text; 

namespace ABP.TPLMS.Entitys
{
    public class Module:Entity, IHasCreationTime
    {

        public const int MaxLength = 255;
        public Module()
        {

            this.DisplayName = string.Empty;
            this.Name = string.Empty;
            this.Url = string.Empty;
            this.HotKey = string.Empty;
            this.ParentId = 0;
            this.IconName = string.Empty;
            this.Status = 0;
            this.ParentName = string.Empty;
            this.RequiredPermissionName = string.Empty;
            this.RequiresAuthentication = false;
            this.SortNo = 0;         

            CreationTime = Clock.Now;
        }    

        [Required]
        [StringLength(MaxLength)]
        public string DisplayName { get; set; }
 

        [Required]
        [StringLength(MaxLength)]
        public string Name { get; set; } 

        [Required]
        [StringLength(MaxLength)]
        public string Url { get; set; }
 

        [StringLength(MaxLength)]
        public string HotKey { get; set; }
        public int ParentId { get; set; }
        public bool RequiresAuthentication { get; set; }
        public bool IsAutoExpand { get; set; } 

        [StringLength(MaxLength)]
        public string IconName { get; set; }
        public int Status { get; set; } 

        [Required]
        [StringLength(MaxLength)]
        public string ParentName { get; set; }
 

        [StringLength(MaxLength)]
        public string RequiredPermissionName { get; set; }
        public int SortNo { get; set; }           
        public DateTime CreationTime { get; set; }     
    }
}

      在上面的Module實體類中的一些屬性上我們定義了[Required]、[MaxLength]等特性用來進行輸入校驗的。

      上面的Module實體類,沒有新增Id屬性,為什麼呢?因為Module繼承自Entity類,Entity類已經定義Id,它是該Entity類的主鍵。因此,所有繼承Entity類的實體類的主鍵名都是Id

      Id(主鍵)的型別是可以更改的,預設是int(int32)。如果你想將Id定義為其他型別,可以在<>內寫,比如Guid,long也是可以的。

      Entity類重寫了等號運算子(==),可以輕鬆地檢查兩個實體是否相同了(實體的Id相同則認為它們相同)。它也定義了IsTransient方法來檢測它是否有Id。 

      IHasCreationTime介面使用一個通用的屬性來描述一個實體的“建立時間”。當實現了該介面的實體類插入到資料庫中時,ABP會自動地將當前的時間設定給CreationTime。

      7.定義好實體之後,我們就要去DbContext中定義實體對應的DbSet,以應用Code First 資料遷移。找到我們的基礎服務層,即以EntityFrameworkCore結尾的專案中,找到DbContext類,如下圖,新增以下程式碼。

 

 

using Microsoft.EntityFrameworkCore;
using Abp.Zero.EntityFrameworkCore;
using ABP.TPLMS.Authorization.Roles;
using ABP.TPLMS.Authorization.Users;
using ABP.TPLMS.MultiTenancy;
using ABP.TPLMS.Entitys;
 

namespace ABP.TPLMS.EntityFrameworkCore
{

    public class TPLMSDbContext : AbpZeroDbContext<Tenant, Role, User, TPLMSDbContext>
    {

        /* Define a DbSet for each entity of the application */      

        public TPLMSDbContext(DbContextOptions<TPLMSDbContext> options)
            : base(options)
        {
        }

        public DbSet<Module> Modules { get; set; }
    }

}

二、執行Code First資料遷移,

    1.從選單中選擇“工具->NuGet包管理器器—>程式包管理器控制檯”選單。如下圖。

 

    2. 在PMC中,預設專案選擇EntityframeworkCore對應的專案後。輸入以下命令:Add-Migration AddEntityModule,建立遷移。如下圖。

 

    3. 在上面的命令執行完畢之後,建立成功後,會在Migrations資料夾下建立時間_AddEntityModule格式的類檔案,這些程式碼是基於DbContext指定的模型。如下圖。

 

4.在程式包管理器控制檯,輸入Update-Database,回車執行遷移。執行成功後,檢視資料庫,Moudles表建立成功。如下圖。

 

 

 

 

相關文章