介紹

很久沒有更新部落格了,之前想更新但是發現部落格園崩了,外加工作上的調換也比較忙,最近有了點時間我來繼續更新下這個系列的文章。

今年3月份我帶著我們研發組同事,將公司產品從老Abp重構到Abp vNext目前已經上線,我非常確認Abp vNext完全可以應用到生產環境,並且他足以支撐超複雜業務的系統開發。

很多人提到Abp就想到DDD,這很好,但是Abp並不是要求你一定要DDD,很多人在社群在群裡說你不DDD你用Abp還不如不用,Abp你用來開發系統太重了,我其實很像問一下說出這些話的人,你深入用過Abp嘛?你用它開發過複雜的業務系統嘛?你能保證你現在使用的系統能夠持久更新嘛?你說Abp很重我就想知道他重的點在哪裡?(此處歡迎大佬找我探討)

這次連載我將由淺入深的來給大家把 Abp vNext 過一遍,該專案作為老張的哲學Blog.Core部落格系統姊妹篇,用比較簡單的業務來演示你該如何開始使用Abp vNext,DDD我也會涉及到,如果時間允許IdentityServer和模組化我也會講,反正就是過一遍讓你不要太觸怕這個東西,話不多說咱們還是直接上專案比較實在。

開始

Abp官網:https://abp.io/

直接建立專案,專案名稱:Blog.Core.AbpvNext 我完全是為了看著方便這麼叫,注意我選擇的Ui是Angular,採用方案是將伺服器端和授權分離為兩個應用程式,如下圖所示。

專案下載下來後修改DbMigrator、Host、IdentityServer的appsettings.json資料庫連線字串。

然後啟動DbMigrator生成資料庫,生成出來的資料庫如下圖所示,資料庫表包含IdentityServer、使用者、角色、組織、審計、Entity、安全、Feature、許可權、日誌、後臺任務、設定等這幾類。

專案介紹

我之前寫過一個部落格站點,如圖所示我就根據這個部落格站點來簡單的分析下模型,模型如下圖所示,後面我們會根據業務在進行修改。

建立模型

根據上面的模型把Entity建立一下,目錄結構看下圖

  public class SiteUser : FullAuditedAggregateRoot<Guid>
{
/// <summary>
/// 使用者名稱
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 使用者
/// </summary>
public string Name { get; set; }
/// <summary>
/// 密碼
/// </summary>
public string PassWord { get; set; }
/// <summary>
/// 頭像
/// </summary>
public string HeadPortrait { get; set; }
/// <summary>
/// 郵箱
/// </summary>
public string Email { get; set; }
/// <summary>
/// 介紹
/// </summary>
public string Introduce { get; set; }
}
    public class Question:FullAuditedAggregateRoot<Guid>
{
/// <summary>
/// 標題
/// </summary>
public string Title { get; set; }
/// <summary>
/// 內容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 類別
/// </summary>
public string Tag { get; set; }
/// <summary>
/// 訪問量
/// </summary>
public int Traffic { get; set; } /// <summary>
/// 問答評論
/// </summary>
public virtual ICollection<QuestionComment> QuestionComments { get; set; }
}
    public class QuestionComment:FullAuditedAggregateRoot<Guid>
{
/// <summary>
/// 內容
/// </summary>
public string Content { get; set; } /// <summary>
/// 是否採納
/// </summary>
public bool IsAdoption { get; set; } /// <summary>
/// 問答資訊
/// </summary>
public virtual Question Question { get; set; }
}
    /// <summary>
/// 文章
/// </summary>
public class Article: FullAuditedAggregateRoot<Guid>
{
/// <summary>
/// 標題
/// </summary>
public string Title { get; set; }
/// <summary>
/// 封面
/// </summary>
public string Cover { get; set; }
/// <summary>
/// 內容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 類別
/// </summary>
public string Tag { get; set; }
/// <summary>
/// 訪問量
/// </summary>
public int Traffic { get; set; }
/// <summary>
/// 文章評論
/// </summary>
public virtual ICollection<ArticleComment> ArticleComments { get; set; } = new List<ArticleComment>(); }
    public class ArticleComment:FullAuditedAggregateRoot<Guid>
{
/// <summary>
/// 內容
/// </summary>
public string Content { get; set; } /// <summary>
/// 問答Id
/// </summary>
public Guid QuestionId { get; set; }
}

加入上下文並建立對映

在 EntityFrameworkCore層 AbpvNextDbContext 中 將Entity加入到上下文。


public class AbpvNextDbContext : AbpDbContext<AbpvNextDbContext>
{ public DbSet<Article> Articles { get; set; } public DbSet<ArticleComment> ArticleComments { get; set; } public DbSet<Question> Questions { get; set; } public DbSet<QuestionComment> QuestionComments { get; set; } public DbSet<SiteUser> SiteUsers { get; set; }
}

建立 EntityConfigurationGroup 資料夾,建立資料對映配置,如圖所示

public class SiteUserCfg : IEntityTypeConfiguration<SiteUser>
{
public void Configure(EntityTypeBuilder<SiteUser> builder)
{
builder.ToTable(AbpvNextConsts.DbTablePrefix + "SiteUser", AbpvNextConsts.DbSchema);
builder.ConfigureByConvention(); builder.Property(e => e.UserName).HasMaxLength(128);
builder.Property(e => e.Name).HasMaxLength(128);
builder.Property(e => e.PassWord).HasMaxLength(256);
builder.Property(e => e.Email).HasMaxLength(128);
builder.Property(e => e.HeadPortrait).HasMaxLength(512);
builder.Property(e => e.Introduce).HasMaxLength(1024); }
}
 public  class ArticleCfg : IEntityTypeConfiguration<Article>
{
public void Configure(EntityTypeBuilder<Article> builder)
{
builder.ToTable(AbpvNextConsts.DbTablePrefix + "Article", AbpvNextConsts.DbSchema);
builder.ConfigureByConvention(); builder.Property(e => e.Title).HasMaxLength(128);
builder.Property(e => e.Cover).HasMaxLength(1024);
// builder.Property(e => e.Content).HasMaxLength(128);
builder.Property(e => e.Tag).HasMaxLength(128); builder.HasMany(e => e.ArticleComments).WithOne()
.HasForeignKey(x => x.ArticleId).IsRequired(false);
}
}
  public class ArticleCommentCfg : IEntityTypeConfiguration<ArticleComment>
{
public void Configure(EntityTypeBuilder<ArticleComment> builder)
{
builder.ToTable(AbpvNextConsts.DbTablePrefix + "ArticleComment", AbpvNextConsts.DbSchema);
builder.ConfigureByConvention(); builder.Property(e => e.Content).HasMaxLength(1024); }
}
    public class QuestionCfg : IEntityTypeConfiguration<Question>
{
public void Configure(EntityTypeBuilder<Question> builder)
{
builder.ToTable(AbpvNextConsts.DbTablePrefix + "Question", AbpvNextConsts.DbSchema);
builder.ConfigureByConvention(); builder.Property(e => e.Title).HasMaxLength(128);
// builder.Property(e => e.Content).HasMaxLength(128);
builder.Property(e => e.Tag).HasMaxLength(128); builder.HasMany(e => e.QuestionComments).WithOne()
.HasForeignKey(x => x.QuestionId).IsRequired(false);
}
}
    public class QuestionCommentCfg : IEntityTypeConfiguration<QuestionComment>
{
public void Configure(EntityTypeBuilder<QuestionComment> builder)
{ builder.ToTable(AbpvNextConsts.DbTablePrefix + "QuestionComment", AbpvNextConsts.DbSchema);
builder.ConfigureByConvention(); builder.Property(e => e.Content).HasMaxLength(1024);
}
}

加入到配置上下文

    public static class AbpvNextDbContextModelCreatingExtensions
{
public static void ConfigureAbpvNext(this ModelBuilder builder)
{
Check.NotNull(builder, nameof(builder)); // 文章
builder.ApplyConfiguration(new ArticleCfg()); builder.ApplyConfiguration(new ArticleCommentCfg()); // 問答
builder.ApplyConfiguration(new QuestionCfg()); builder.ApplyConfiguration(new QuestionCommentCfg()); // 使用者
builder.ApplyConfiguration(new SiteUserCfg());
}
}

建立遷移選擇 EntityFrameworkCore.DbMigrations 執行 Add-Migration Init_App_Db .

結語

該專案存放倉庫在 https://github.com/BaseCoreVueProject/Blog.Core.AbpvNext

QQ群:867095512

專案開發過程中可能隨時根據想法進行變化,加油!!!