Table of Contents
Intro
In this post we will see how to create a CRUD API in ABP Application framework with dotnet core and ef core. It will be simple Todo api.
You can watch the videos or continue with the post.
Preparing the Project
Use the Getting started at https://docs.abp.io/en/abp/latest/getting-started?ui=mvc&db=ef&tiered=no guide to setup the basic abp application
Entity
1. Create anFirst step is to create an Entity. Create the Entity in the Domain
project
public class Todo : 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 EntityFrameworkCore
project. Add the DbSet to the DbContext
public DbSet<Todo> Todos { get; set; }
ef core
3. Configure Entity inConfiguration is done in the DbContextModelCreatingExtensions
class. This should be available in the EntityFrameworkCore
project
builder.Entity<Todo>(b =>
{
b.ToTable(TodosConsts.DbTablePrefix + "Todos", TodosConsts.DbSchema);
b.ConfigureByConvention(); //auto configure for the base class props
});
4. Adding Migrations
Now the Entity is configured we can add the migrations.
Go the EntityFrameworkCore.DbMigrations
project in the terminal and create migrations.
To create migration run this command:
dotnet ef migrations add created_todo
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 Contracts
project
public class TodoDto : 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 Application
project.
CreateMap<Todo, TodoDto>();
CreateMap<TodoDto, Todo>();
Application Services
7. Create anApplication service are created in the Application
project
public class TodoAppService : YourProjectAppService
{
private readonly IRepository<Todo, Guid> todoRepository;
public TodoAppService(IRepository<Todo, Guid> todoRepository)
{
this.todoRepository = todoRepository;
}
public async Task<List<TodoDto>> GetAll()
{
return ObjectMapper.Map<List<Todo>, List<TodoDto>>(await todoRepository.GetListAsync());
}
public async Task<TodoDto> CreateAsync(TodoDto todoDto)
{
var todo = ObjectMapper.Map<TodoDto, Todo>(todoDto);
var createdTodo = await todoRepository.InsertAsync(todo);
return ObjectMapper.Map<Todo, TodoDto>(createdTodo);
}
public async Task<TodoDto> UpdateAsync(TodoDto todoDto)
{
var todo = ObjectMapper.Map<TodoDto, Todo>(todoDto);
var createdTodo = await todoRepository.UpdateAsync(todo);
return ObjectMapper.Map<Todo, TodoDto>(createdTodo);
}
public async Task<bool> DeleteAsync(Guid id)
{
var todo = await todoRepository.FirstOrDefaultAsync(x=> x.Id == id);
if(todo != null)
{
await todoRepository.DeleteAsync(todo);
return true;
}
return false;
}
}
Once you created the Application configuration ABP can automatically configure your service as API controllers by conventions.
https://github.com/antosubash/Todos
Github Repo Link :Related Posts
Changing theme for your ABP app. Part 10
In this post we will explore how to change the theme for your ABP application.
Deploy ABP Framework dotnet core tiered app to docker swarm. Part 9
In this post we will see how to deploy your dotnet core app with docker container.
Centralized logging for .net core ABP microservices app using Seq. Part 8
In this post we will see how to implement a central logging system for your ABP app using Seq.