zoukankan      html  css  js  c++  java
  • 知识全聚集 .Net Core 技术突破 丨ABP vNext 开始

    介绍

    很久没有更新博客了,之前想更新但是发现博客园崩了,外加工作上的调换也比较忙,最近有了点时间我来继续更新下这个系列的文章。

    今年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

    项目开发过程中可能随时根据想法进行变化,加油!!!

  • 相关阅读:
    数组和类集排序总结
    toString()方法
    冒泡排序java
    Scanner类输入字符串和整形数字
    身份证分组
    jsonp的实质
    在伪数组上部署iterator方法
    获取属性的方法
    合并以及对于引用克隆
    深度复制对象已解决循环引用
  • 原文地址:https://www.cnblogs.com/wl-blog/p/15033568.html
Copyright © 2011-2022 走看看