Domain

引入Volo.Abp.Ddd.Domain

创建模块:DomainModule
[DependsOn(typeof(AbpDddDomainModule))]

创建领域模型,聚合根需要继承AggregateRoot(BasicAggregateRoot),非聚合根直接继承Entity

Application.Contracts

引入Volo.Abp.Ddd.Application.Contracts

创建模块:ApplicationContractModule
创建对应Dto模型
创建IService,继承IApplicationService,定义提供的方法

Application

依赖Volo.Abp.Ddd.Application
依赖Application.Contracts
依赖Domain
依赖Volo.Abp.AutoMapper

创建Service,继承ApplicationService,继承并实现IService

创建模块:ApplicationModule
[DependsOn]DomainModule
[DependsOn]ApplicationContractModule
[DependsOn]AbpAutoMapperModule

public override void ConfigureServices(ServiceConfigurationContext context)
        {
            //注入AutoMapper
            context.Services.AddAutoMapperObjectMapper<***ApplicationModule>();

            Configure<AbpAutoMapperOptions>(options=> {
                options.AddMaps<***ApplicationModule>();
            });
        }

创建对应的Profile类,需要继承AutoMapper.Profile

Api

依赖Volo.Abp.AspNetCore.Mvc
依赖Volo.Abp.Autofac
依赖Application
依赖Swashbuckle.AspNetCore(使用swagger)

Program中UseAutofac()

创建模块:ApiModule,配置应用程序

    [DependsOn(typeof(AbpAspNetCoreMvcModule))]
    [DependsOn(typeof(AbpAutofacModule))]
    [DependsOn(typeof(***ApplicationModule))]
    public class ApiModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            //自动controler
            Configure<AbpAspNetCoreMvcOptions>(options =>
            {
                options.ConventionalControllers.Create(typeof(***Service).Assembly);
            });

            //配置Swagger文档
            ConfigureSwaggerServices(context.Services);
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();

            app.UseRouting();
            app.UseConfiguredEndpoints();

            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "Api");
            });
        }

        /// <summary>
        /// 配置Swagger文档
        /// </summary>
        /// <param name="services"></param>
        private void ConfigureSwaggerServices(IServiceCollection services)
        {
            services.AddSwaggerGen(
                options =>
                {
                    options.SwaggerDoc("v1", new OpenApiInfo { Title = "Api", Version = "v0.1" });
                    options.DocInclusionPredicate((docName, description) => true);
                    options.CustomSchemaIds(type => type.FullName);
                }
                );
        }
    }

Startup中初始化abp应用程序

 public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplication<ApiModule>();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.InitializeApplication();
        }
    }

EFCore

依赖Volo.Abp.EntityFrameworkCore.SqlServer
依赖Domain

创建模块:EntityFremeworkCoreModule
[DependsOn(typeof(AbpEntityFrameworkCoreSqlServerModule))]

public override void ConfigureServices(ServiceConfigurationContext context)
{
    context.Services.AddAbpDbContext<ImsDbContext>(options=> {
        //添加默认仓储
        options.AddDefaultRepositories();
    });

    Configure<AbpDbContextOptions>(options=> {
        options.UseSqlServer();
    });
}

创建DbContext,需要继承 AbpDbContext,添加对应的 DbSet,重写 OnModelCreating。

此时已经可以在Application中的Service中 注入相应的 IRepository<>了(ApiModule需要[DependsOn(typeof(EntityFremeworkCoreModule))])

UnitOfWork

在Service的构造函数中,直接注入IUnitOfWorkManager。

using (var u =_uow.Begin())
{
    ...
    ...
    await u.CompleteAsync();
}

Q.E.D.