Create A Module In Abp
step by step guide to creating a module in abp framework.
Projects are organized as src, test and host folders:
src folder contains the actual module which is layered based on DDD principles.
test folder contains unit & integration tests.
host folder contains applications with different configurations to demonstrate how to host the module in an application. These are not a part of the module, but useful on development.
Step by Step Module Creation
Step-1. Create The Abp Module Template
abp new MyModule.Abp.Core -t module - no-ui
Step-2. Create all the Entities then dtos and enums.
Step-3. Create Custom repository.
Step-4. Create all Application-Services, Domain-Services and permissions.
Step-5. Configure the DbContext, including the DbSets.
- Add Entity to the EF Core. You will find the DbContext in the MainApp.ModuleA.EntityFrameworkCore project. Add the DbSet to the DbContext.
public DbSet<Employee> Employees { get; set; }
2. Next is to configure the CoreDbContextModelCreatingExtensions class inside the ConfigureMyModuleCore method. This should be available in the MyModule.Abp.Core.EntityFrameworkCore project.
— Once the configuration is completed, invoke the ConfigureMyModule method within the MainAppDbContext.
builder.Entity<Employee>(b =>
{
b.ToTable(MyModuleCoreDbProperties.DbTablePrefix + "Employees", MyModuleCoreDbProperties.DbSchema);
b.ConfigureByConvention(); // Auto configure for the base class properties
});
3. Replace any DbContexts if required.
[ReplaceDbContext(typeof(IIdentityDbContext))]
public class MyModuleCoreDbContext :
AbpDbContext<MyModuleCoreDbContext>,
IIdentityDbContext,
IMyModuleCoreDbContext
{
// DbContext implementation
}
Step-6. Map Entity to the DTOs.
- ABP uses AutoMapper to map Entity to Dto. You can find the CoreApplicationAutoMapperProfile file which is used by AutoMapper in the MyModule.Core.Application project.
CreateMap<Employee, EmployeeDto>();
CreateMap<Employee, CreateEmployeeDto>();
- Make sure, you have removed the parameter validate: true from the ConfigureServices in CoreApplicationModule.
Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<MyModuleApplicationModule>(validate: true);
});
Step-7. Migrating Entities
After configuring the Entity, it’s time to incorporate the migrations.
Navigate to the terminal and set the MainApp.EntityFrameworkCore as startup project.
Generate migrations using the following command:
dotnet ef migrations add created_MyModule
Confirm the creation of migrations within the designated folder.
Execute the following command to update the database:
dotnet ef database update
Step-8. Configure application services as API controllers by convention.
Configure<AbpAspNetCoreMvcOptions>(options =>
{
options.ConventionalControllers.Create(typeof(MyModuleApplicationModule).Assembly);
});
Bonus:
The replacement of modules is a hassle-free operation, much like the straightforward process of switching a computer’s RAM to a different brand.
This modular structure also allows a team of programmers to collaborate on a specific module, implementing changes without delving into the business logic beyond that particular module.
Considering most readers here are new to this terrain, I have created another post where I have provided a in-depth understanding of the rationale behind each steps. Here is the link.