Anto Subash.

A personal blog from a lazy programmer.

Published on

Add a new Module to the Tiered ABP App with separate database for the module

Add a new Module to the Tiered ABP App with separate database for the module

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

Creating the abp application and run migrations

1abp new MainApp -t app -u mvc --tiered 

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

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 all there host 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. This should be available in the MainApp.ModuleA.EntityFrameworkCore project

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 for the ModuleA

Now the Entity is configured we can add the migrations.

Create EntityFrameworkCore\ModuleA folder in the MainApp.HttpApi.Host project.

Create a ModuleAHttpApiHostMigrationsDbContext.cs file in the EntityFrameworkCore\ModuleA folder

1public class ModuleAHttpApiHostMigrationsDbContext : AbpDbContext<ModuleAHttpApiHostMigrationsDbContext>
2{
3    public ModuleAHttpApiHostMigrationsDbContext(DbContextOptions<ModuleAHttpApiHostMigrationsDbContext> options) : base(options)
4    {
5    }
6
7    protected override void OnModelCreating(ModelBuilder modelBuilder)
8    {
9        base.OnModelCreating(modelBuilder);
10
11        modelBuilder.ConfigureModuleA();
12    }
13}

Create a ModuleAHttpApiHostMigrationsDbContextFactory.cs file in the EntityFrameworkCore\ModuleA folder

1public class ModuleAHttpApiHostMigrationsDbContextFactory : IDesignTimeDbContextFactory<ModuleAHttpApiHostMigrationsDbContext>
2{
3    public ModuleAHttpApiHostMigrationsDbContext CreateDbContext(string[] args)
4    {
5        var configuration = BuildConfiguration();
6
7        var builder = new DbContextOptionsBuilder<ModuleAHttpApiHostMigrationsDbContext>()
8            .UseSqlServer(configuration.GetConnectionString("ModuleA"));
9        return new ModuleAHttpApiHostMigrationsDbContext(builder.Options);
10    }
11
12    private static IConfigurationRoot BuildConfiguration()
13    {
14        var builder = new ConfigurationBuilder()
15            .SetBasePath(Directory.GetCurrentDirectory())
16            .AddJsonFile("appsettings.json", optional: false);
17
18        return builder.Build();
19    }
20}

Update the connection string in appsettings.json in the MainApp.HttpApi.Host

1  "ConnectionStrings": {
2    "Default": "Server=(LocalDb)\\MSSQLLocalDB;Database=MainApp;Trusted_Connection=True",
3    "ModuleA": "Server=(LocalDb)\\MSSQLLocalDB;Database=ModuleA;Trusted_Connection=True",
4  },

To create migration run this command:

1dotnet ef migrations add created_todoone --context ModuleAHttpApiHostMigrationsDbContext --output-dir Migrations/ModuleA

Verify the migrations created in the migrations folder.

To update the database run this command

1dotnet ef database update --context ModuleAHttpApiHostMigrationsDbContext

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 ConfigureAutoApiControllers in the MainAppHttpApiHostModule in the MainApp.HttpApi.Host

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

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