ABP DDD

发布时间 2023-03-22 21:13:59作者: superman-is-chao

1.创建一个MVC 项目

2.WEB 层引用 

 Volo.Abp.AspNetCore

3.添加 WebApplicationWebModule 添加 

ConfigureServices
OnApplicationInitialization  然后把 Startup 面的
ConfigureServices
OnApplicationInitialization  
复制到 WebApplicationWebModule  如下:
 1     [DependsOn(typeof(AbpAspNetCoreMvcModule))]
 2     public class WebApplicationWebModule:AbpModule
 3     {
 4         //注册服务
 5         public override void ConfigureServices(ServiceConfigurationContext context)
 6         {
 7             var services=context.Services;
 8             services.AddControllersWithViews();
 9         }
10         //注册中间件
11         public override void OnApplicationInitialization(ApplicationInitializationContext context)
12         {
13             var app = context.GetApplicationBuilder();
14             var env = context.GetEnvironment();
15             if (env.IsDevelopment())
16             {
17                 app.UseDeveloperExceptionPage();
18             }
19             else
20             {
21                 app.UseExceptionHandler("/Home/Error");
22             }
23             app.UseStaticFiles();
24 
25             app.UseRouting();
26 
27             app.UseAuthorization();
28 
29             app.UseEndpoints(endpoints =>
30             {
31                 endpoints.MapControllerRoute(
32                     name: "default",
33                     pattern: "{controller=Home}/{action=Index}/{id?}");
34             });
35         }
36     }

4.修改 Program类

 1 using DDDAbpDemo.Web;
 2 
 3 var builder = WebApplication.CreateBuilder(args);
 4 builder.Services.AddApplication<DDDAbpDemoWebModule>();
 5 
 6 var app = builder.Build();
 7 
 8 app.InitializeApplication();
 9 app.MapControllerRoute(
10                 name: "default",
11                 pattern: "{controller=Home}/{action=Index}/{id?}");
12 
13 app.Run();

5.构造函数依赖注入

 

using Microsoft.AspNetCore.Builder;

using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Modularity;

namespace DDDAbpDemo.Web
{
    [DependsOn(
        typeof(AbpAspNetCoreMvcModule)
        )]
    public class DDDAbpDemoWebModule:AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var services = context.Services;
            //构造函数依赖注入
            services.AddSingleton<IUserService, UserService>();
            services.AddControllersWithViews();

        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();
            // Configure the HTTP request pipeline.
            if (!env.IsDevelopment())
            {
                app.UseExceptionHandler(errorHandlingPath: "/Home/Error");
            }
            app.UseStaticFiles();
            app.UseRouting();
            app.UseAuthorization();
        }
    }
}

 

4.添加 WebApplication1.EntityFrameworkCore 类库项目

    引用Volo.Abp.AspNetCore,Volo.Abp.EntityFrameworkCore,Volo.Abp.EntityFrameworkCore.Sqlite(使用那个数据库就引用那个数据)

    添加 WebApplication1DbContext类

 1 using Volo.Abp.EntityFrameworkCore;
 2 using Volo.Abp.Modularity;
 3 
 4 namespace DDDAbpDemo.EntityFrameworkCore
 5 {
 6     [DependsOn(typeof(AbpEntityFrameworkCoreModule))]
 7     public class DDDAbpDemoEntityFrameworkCoreModule:AbpModule
 8     {
 9 
10     }
11 }

5.添加上下文类  DDDAbpDemoDbContext 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using Microsoft.EntityFrameworkCore;
 8 
 9 using Volo.Abp.EntityFrameworkCore;
10 
11 namespace DDDAbpDemo.EntityFrameworkCore
12 {
13     //泛型类 基于那个 上下文可以是多个
14     public class DDDAbpDemoDbContext : AbpDbContext<DDDAbpDemoDbContext>   //泛型类 基于那个 上下文可以是多个
15     {
16         public DDDAbpDemoDbContext(DbContextOptions<DDDAbpDemoDbContext> options) : base(options)
17         {
18         }
19         protected override void OnModelCreating(ModelBuilder modelBuilder)
20         {
21             base.OnModelCreating(modelBuilder);
22         }
23     }
24 }

 

6.DDDAbpDemoEntityFrameworkCoreModule  添加上下文依赖注入

 1 using Microsoft.Extensions.DependencyInjection;
 2 
 3 using Volo.Abp.EntityFrameworkCore;
 4 using Volo.Abp.Modularity;
 5 
 6 namespace DDDAbpDemo.EntityFrameworkCore
 7 {
 8     [DependsOn(typeof(AbpEntityFrameworkCoreModule))]
 9     public class DDDAbpDemoEntityFrameworkCoreModule:AbpModule
10     {
11         public override void ConfigureServices(ServiceConfigurationContext context)
12         {
13             var service = context.Services;
14             //添加依赖注入上下文类
15             service.AddAbpDbContext<DDDAbpDemoDbContext>();
16             //上下文所使用的数据库
17             Configure<AbpDbContextOptions>(opt => { opt.UseSqlServer(); });
18         }
19     }
20 }

7.添加 DDDAbpDemo.Domian

Nuget :Volo.Abp.AspNetCore,Volo.Abp.Ddd.Domain

 

8.添加 DDDAbpDemoDomianModule 类

 1 using Volo.Abp.Domain;
 2 using Volo.Abp.Modularity;
 3 
 4 namespace DDDAbpDemo.Domian
 5 {
 6     [DependsOn(typeof(AbpDddDomainModule))]
 7     public class DDDAbpDemoDomianModule:AbpModule
 8     {
 9 
10     }
11 }

 

9.DDDAbpDemo.Domian 下添加对象类 Users

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using Volo.Abp.Domain.Entities;
 8 
 9 namespace DDDAbpDemo.Domian.UserInfo
10 {
11     public class Users:Entity<int>
12     {
13         public string? UserNo { get; init; }
14 
15         public string? UserName { get; set; }
16 
17         public int? RoleId { get; set; }
18 
19         public string? Password { get; set; }
20     }
21 }

10.修改上下文类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using DDDAbpDemo.Domian.UserInfo;
 8 
 9 using Microsoft.EntityFrameworkCore;
10 
11 using Volo.Abp.Data;
12 using Volo.Abp.EntityFrameworkCore;
13 
14 namespace DDDAbpDemo.EntityFrameworkCore
15 {
16     [ConnectionStringName("Default")]
17     //泛型类 基于那个 上下文可以是多个
18     public class DDDAbpDemoDbContext : AbpDbContext<DDDAbpDemoDbContext>
19     {
20         public DbSet<Users>? users { get; set; }
21         public DDDAbpDemoDbContext(DbContextOptions<DDDAbpDemoDbContext> options) : base(options)
22         {
23         }
24         protected override void OnModelCreating(ModelBuilder modelBuilder)
25         {
26             base.OnModelCreating(modelBuilder);
27         }
28     }
29 }

11. DDDAbpDemo.Web 添加设计器依赖包

Microsoft.EntityFrameworkCore

Microsoft.EntityFrameworkCore.Tools

Microsoft.EntityFrameworkCore.Design

在程序包管理控制输入:add-migration i1