ABP 极简入门教程(一)

本示例不讲概念,只讲怎么用,概念性的内容没有比官方文档更清楚的了,我也正在学习,可能理解的地方有不对的欢迎一起交流,但需要您了解以下内容才能看明白

  • asp.net core
  • Entity Framework ,数据迁移
  • DDD领域驱动设计 (Entities、Repositories、Domain Services、Domain Events、Application Services、DTOs等)
  • Castle windsor (依赖注入容器)
  • AutoMapper(实现Dto类与实体类的双向自动转换)
  • Bootstrap 
  • jQuery

Abp下载,MVC项目 Multi Page Web Application 项目名Sample

项目结构

Sample.Core下新建Territory目录,新建Province模型类

using Abp.Domain.Entities;

namespace Sample.Territory
{
    public class Province : Entity<int>
    {
        public string Name { get; set; }
    }
}

Sample.EntityFrameworkCore类库下找到EntityFrameworkCore目录下的SampleDbContext修改如下

using Microsoft.EntityFrameworkCore;
using Abp.Zero.EntityFrameworkCore;
using Sample.Authorization.Roles;
using Sample.Authorization.Users;
using Sample.MultiTenancy;
using Sample.Territory;

namespace Sample.EntityFrameworkCore
{
    public class SampleDbContext : AbpZeroDbContext<Tenant, Role, User, SampleDbContext>
    {
        /* Define a DbSet for each entity of the application */

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

        //DBSet类表示一个实体的集合
        public DbSet<Province> Provinces { get; set; }
        
        //如果需要变更数据库表名需要使用
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Province>().ToTable("Province");
        }
    }
}

数据迁移,实体类转为数据表

PM> Add-Migration addProvince
Build started...
Build succeeded.
To undo this action, use Remove-Migration.
PM> update-database
Build started...
Build succeeded.
Applying migration '20200610003957_addProvince'.
Done.

 Sample.Application类库下新建Territory目录,添加ProvinceAppService类,因需求abp提供的方法已经足够使用,故未添加IProvinceAppService接口

using Abp.Application.Services;
using Abp.Domain.Repositories;
using Sample.Territory.Dto;

namespace Sample.Territory
{
    /// <summary>
    /// abp提供的常用增删改查方式
    /// </summary>
    public class ProvinceAppService:AsyncCrudAppService<Province,ProvinceDto>
    {
        public ProvinceAppService(IRepository<Province, int> repository) : base(repository)
        {
        }
    }
}

Territory目录下新建Dto目录, 并添加ProvinceDto实体类

using Abp.Application.Services.Dto;
using Abp.AutoMapper;

namespace Sample.Territory.Dto
{
    [AutoMapFrom(typeof(Province))]
    public class ProvinceDto :EntityDto<int>
    {
        public string Name { get; set; }
    }
}

同目录新建ProvinceProfile类,自动映射

using AutoMapper;

namespace Sample.Territory.Dto
{
    public class ProvinceProfile : Profile
    {
        public ProvinceProfile()
        {
            CreateMap<ProvinceDto, Province>();
        }
    }
}

选中Sample.Web.Host设为启动项,所有Api已经自动设置好,并可以测试,效果如下

 

posted @ 2020-06-10 09:14  liessay  阅读(2158)  评论(0编辑  收藏  举报