Anto Subash.

ABP Framework - Getting started -Part : 1

ABP Getting Started With .Net Core and EF Core. ABP Part 1

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.

IMAGE ALT TEXT HERE

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

1. Create an Entity

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

1public class Todo : 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 EntityFrameworkCore project. Add the DbSet to the DbContext

1public DbSet<Todo> Todos { get; set; }

3. Configure Entity in ef core

Configuration is done in the DbContextModelCreatingExtensions class. This should be available in the EntityFrameworkCore project

1builder.Entity<Todo>(b =>
2{
3    b.ToTable(TodosConsts.DbTablePrefix + "Todos", TodosConsts.DbSchema);
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 EntityFrameworkCore.DbMigrations project in the terminal and create migrations.

To create migration run this command:

1dotnet ef migrations add created_todo

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 Contracts project

1public class TodoDto : 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 Application project.

1CreateMap<Todo, TodoDto>();
2CreateMap<TodoDto, Todo>();

7. Create an Application Services

Application service are created in the Application project

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

Once you created the Application configuration ABP can automatically configure your service as API controllers by conventions.

Buy Me a Coffee at ko-fi.com