Home > OS >  Get categoryName for task by categoryId ASP.NET Core 6 MVC
Get categoryName for task by categoryId ASP.NET Core 6 MVC

Time:05-02

I have a CategoryModel and TaskModel. I need pass to these fields - Id, Name, Deadline, CategoryName - from the Categories model to a view model class.

I have classes TaskRepository and CategoryRepository to get data from the database with Dapper and AutoMapper.

I don't have any idea how to do this correctly. Maybe you have an idea? Thank you!

Category model

public class CategoryModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Task Model

public class TaskModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsDone { get; set; }
    public DateTime? Deadline { get; set; }
    public DateTime? DateExecution { get; set; }
    public int CategoryId { get; set; }
}

View model class:

public class CurrentTaskItemViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime? Deadline { get; set; }
    public string? CategoryName { get; set; }
}

ICategoryRepository:

public interface ICategoryRepository
{
    IEnumerable<CategoryModel> GetList();
    CategoryModel GetById(int id);

    void Delete(int id);
    void Create(CategoryModel model);
    void Update(CategoryModel model);
}

ITaskRepository:

public interface ITaskRepository
{
    IEnumerable<TaskModel> GetList();
    TaskModel GetById(int id);

    void Delete(int id);
    void Create(TaskModel task);
    void Perform(int id);
}

CodePudding user response:

I think it would be better to write an extra method in the TaskRepository class and pull data according to the fields you need. For example, it can be like GetTaskWithCategoryName(). After all, using mapper brings extra costs.

CodePudding user response:

You could refer to the following structure:

enter image description here

API Layer

It will be used to handle the request and send back the appropriate responses. This layer doesn’t know about the service layer functionality, its main function is to pass the request to the service layer and handle any Global Exception. Authentication and authorization will fall under this layer.

Service Layer

The main role of this layer is to have the business logic as well as to convert the ViewModel object to DTO ( Data Transfer Object) and vise versa. You can have a private validation method to validate the ViewModel object and take necessary actions on it.

DAL

It is known as a Data Access Layer, it is used to communicate with the Databases. It could be SQL Server, MySQL, NoSQL, Redis Cache, etc. We are using Entity Framework In Memory to fetch and post data.

So you can create a service, then you can get data from the Repository and then convert the ViewModel object to DTO ( Data Transfer Object).

More detail information, see:

.Net Core Web API (Dependency Inject - AutoMapper - Repository Pattern)

Repository Pattern with AutoMapper in .Net Core

  • Related