Table of Contents
- Add a new Module to the ABP App
- Creating the abp application and run migrations
- Run Migrations
- Add a new Module
- Add new Entity to the ModuleA
- 1. Create an Entity
- 2. Add Entity to ef core
- 3. Configure Entity in ef core
- 4. Adding Migrations
- 5. Create a Entity Dto
- 6. Map Entity to Dto
- 7. Create an Application Services
- 8. Update AddAbpDbContext method in the ModuleAEntityFrameworkCoreModule
- 9. Update the ConfigureServices in the ModuleAWebModule in the ModuleA.Web
- 10. Test you api
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
abp new MainApp
Run Migrations
Change directory to src/MainApp.DbMigrator and run the migration project
dotnet 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
abp 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
.
Entity
1. Create anFirst step is to create an Entity. Create the Entity in the MainApp.ModuleA.Domain
project.
public class TodoOne : Entity<Guid>
{
public string Content { get; set; }
public bool IsDone { get; set; }
}
ef core
2. Add Entity toNext 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
public DbSet<TodoOne> TodoOnes { get; set; }
ef core
3. Configure Entity inConfiguration 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
.
builder.Entity<TodoOne>(b =>
{
b.ToTable(options.TablePrefix + "TodoOnes", options.Schema);
b.ConfigureByConvention(); //auto configure for the base class props
});
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:
dotnet ef migrations add created_todoone
Verify the migrations created in the migrations folder.
To update the database run this command
dotnet ef database update
5. Create a Entity Dto
Dto are placed in MainApp.ModuleA.Application.Contracts
project
public class TodoOneDto : EntityDto<Guid>
{
public string Content { get; set; }
public bool IsDone { get; set; }
}
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.
CreateMap<TodoOne, TodoOneDto>();
CreateMap<TodoOneDto, TodoOne>();
Application Services
7. Create anApplication service are created in the MainApp.ModuleA.Application
project
public class TodoOneAppService : ModuleAAppService
{
private readonly IRepository<TodoOne, Guid> todoOneRepository;
public TodoOneAppService(IRepository<TodoOne, Guid> todoOneRepository)
{
this.todoOneRepository = todoOneRepository;
}
public async Task<List<TodoOneDto>> GetAll()
{
return ObjectMapper.Map<List<TodoOne>, List<TodoOneDto>>(await todoOneRepository.GetListAsync());
}
public async Task<TodoOneDto> CreateAsync(TodoOneDto todoOneDto)
{
var TodoOne = ObjectMapper.Map<TodoOneDto, TodoOne>(todoOneDto);
var createdTodoOne = await todoOneRepository.InsertAsync(TodoOne);
return ObjectMapper.Map<TodoOne, TodoOneDto>(createdTodoOne);
}
public async Task<TodoOneDto> UpdateAsync(TodoOneDto todoOneDto)
{
var TodoOne = ObjectMapper.Map<TodoOneDto, TodoOne>(todoOneDto);
var createdTodoOne = await todoOneRepository.UpdateAsync(TodoOne);
return ObjectMapper.Map<TodoOne, TodoOneDto>(createdTodoOne);
}
public async Task<bool> DeleteAsync(Guid id)
{
var TodoOne = await todoOneRepository.FirstOrDefaultAsync(x=> x.Id == id);
if(TodoOne != null)
{
await todoOneRepository.DeleteAsync(TodoOne);
return true;
}
return false;
}
}
AddAbpDbContext
method in the ModuleAEntityFrameworkCoreModule
8. Update options.AddDefaultRepositories(includeAllEntities: true);
ConfigureServices
in the ModuleAWebModule
in the ModuleA.Web
9. Update the Configure<AbpAspNetCoreMvcOptions>(options =>
{
options.ConventionalControllers.Create(typeof(ModuleAApplicationModule).Assembly);
});
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
Related Posts
ABP-Powered Web App with Inertia.js, React, and Vite
Building a web application with ABP Framework, Inertia.js, React, and Vite.
Migrating Tye to Aspire
In this post we will see how to migrate the Tye to Aspire
ABP React Template V2
Version 2 of React Starter Template for ABP application with Next.js, Tailwind CSS, and shadcn-ui.