Anto Subash.

A personal blog from a lazy programmer.

Published on

Add a new Module to the ABP App

Add a new Module to the ABP App

In this post we will see how to develop a modular abp application. We will add a new module to default abp app and then use the same database to store the modules data and the identity data.

Creating the abp application and run migrations

1abp new MainApp

Run Migrations

Change directory to src/MainApp.DbMigrator and run the migration project

1dotnet run

This will apply the migrations to the db and we can run the MainApp.Web project. This will host the UI and API..

Add a new Module

Now we will add a new module to our MainApp. Navigate to the solution folder of the MainApp and run the following command

1abp add-module ModuleA --new --add-to-solution-file

This command will create a new module and add the new module to the solution.

Now you can run the MainApp.Web and see the Api and UI available in the app.

Add new Entity to the ModuleA

We will create a new Entity inside the MainApp.ModuleA.Domain called TodoOne.

1. Create an Entity

First step is to create an Entity. Create the Entity in the MainApp.ModuleA.Domain project.

1public class TodoOne : Entity<Guid>
2{
3    public string Content { get; set; }
4    public bool IsDone { get; set; }
5}

2. Add Entity to ef core

Next is to add Entity to the EF Core. you will find the DbContext in the MainApp.ModuleA.EntityFrameworkCore project. Add the DbSet to the DbContext

1public DbSet<TodoOne> TodoOnes { get; set; }

3. Configure Entity in ef core

Configuration is done in the DbContextModelCreatingExtensions class inside the ConfigureModuleA method. This should be available in the MainApp.ModuleA.EntityFrameworkCore project. ConfigureModuleA is invoked in the MainAppDbContext.

1builder.Entity<TodoOne>(b =>
2{
3    b.ToTable(options.TablePrefix + "TodoOnes", options.Schema);
4    b.ConfigureByConvention(); //auto configure for the base class props
5});

4. Adding Migrations

Now the Entity is configured we can add the migrations.

Go the MainApp.EntityFrameworkCore project in the terminal and create migrations.

To create migration run this command:

1dotnet ef migrations add created_todoone

Verify the migrations created in the migrations folder.

To update the database run this command

1dotnet ef database update

5. Create a Entity Dto

Dto are placed in MainApp.ModuleA.Application.Contracts project

1public class TodoOneDto : EntityDto<Guid>
2{
3    public string Content { get; set; }
4    public bool IsDone { get; set; }
5}

6. Map Entity to Dto

Abp uses AutoMapper to map Entity to Dto. you can find the ApplicationAutoMapperProfile file which is used by the AutoMapper in the MainApp.ModuleA.Application project.

1CreateMap<TodoOne, TodoOneDto>();
2CreateMap<TodoOneDto, TodoOne>();

7. Create an Application Services

Application service are created in the MainApp.ModuleA.Application project

1public class TodoOneAppService : ModuleAAppService
2{
3    private readonly IRepository<TodoOne, Guid> todoOneRepository;
4
5    public TodoOneAppService(IRepository<TodoOne, Guid> todoOneRepository)
6    {
7        this.todoOneRepository = todoOneRepository;
8    }
9
10    public async Task<List<TodoOneDto>> GetAll()
11    {
12        return ObjectMapper.Map<List<TodoOne>, List<TodoOneDto>>(await todoOneRepository.GetListAsync());
13    }
14
15    public async Task<TodoOneDto> CreateAsync(TodoOneDto todoOneDto)
16    {
17        var TodoOne = ObjectMapper.Map<TodoOneDto, TodoOne>(todoOneDto);
18        var createdTodoOne = await todoOneRepository.InsertAsync(TodoOne);
19        return ObjectMapper.Map<TodoOne, TodoOneDto>(createdTodoOne);
20    }
21
22    public async Task<TodoOneDto> UpdateAsync(TodoOneDto todoOneDto)
23    {
24        var TodoOne = ObjectMapper.Map<TodoOneDto, TodoOne>(todoOneDto);
25        var createdTodoOne = await todoOneRepository.UpdateAsync(TodoOne);
26        return ObjectMapper.Map<TodoOne, TodoOneDto>(createdTodoOne);
27    }
28
29    public async Task<bool> DeleteAsync(Guid id)
30    {
31        var TodoOne = await todoOneRepository.FirstOrDefaultAsync(x=> x.Id == id);
32        if(TodoOne != null)
33        {
34            await todoOneRepository.DeleteAsync(TodoOne);
35            return true;
36        }
37        return false;
38    }
39}

8. Update AddAbpDbContext method in the ModuleAEntityFrameworkCoreModule

1options.AddDefaultRepositories(includeAllEntities: true);

9. Update the ConfigureServices in the ModuleAWebModule in the ModuleA.Web

1Configure<AbpAspNetCoreMvcOptions>(options =>
2            {
3                options.ConventionalControllers.Create(typeof(ModuleAApplicationModule).Assembly);
4            });

10. Test you api

Run the MainApp.Web project and navigate to https://localhost:<port>/swagger/ you will see the todo apis. You can test your API there.

Repo: https://github.com/antosubash/NewModuleWithAbp