Search code examples
c#apiasp.net-corevisual-studio-2017automapper

How do I map the DTO files to my Models in my .Net Core project


I've never worked with a .Net Core project before but have a history with .Net including MVC and entity framework. I'm working with a new .Net Core project which has five solution folders, EHA.PROJ.API, EHA.PROJ.DTO,EHA.PROJ.Repository, EHA.PROJ.Repository.Test and EHA.PROJ.Web. The EHA.PROJ.DTO folder has a number of files such as CategoryDTO.cs which looks like this

namespace EHA.PROJ.DTO
{
    public class CategoryDescDTO
    {
        public int CategoryRef { get; set; }

        public string CategoryName { get; set; }
    }
}

I'm looking to set up a mapping arrangement to get the data from the EHA.PROJ.DTO files to the model files in my models folder in my EHA.PROJ.Web folder. I've been browsing as I've never done anything like this before as I've previously worked with data from a DAL folder using entity framework and connection done through connection strings. I'm guessing that there must be some process to map the data in my dbContext to connect the files in both folders. I did find some information on AutoMapper but was unsure how to implement it.

This arrangement with .Net Core is new to me so if anyone can help with any examples or point me in the right direction I would be grateful.


Solution

  • This article is a good reference to start: https://buildplease.com/pages/repositories-dto/

    My suggestion is to have a DTO assembler that maps your model to the DTO object. So, you start with your DTO class:

    namespace EHA.PROJ.DTO
    {
        public class CategoryDescDTO
        {
            public int CategoryRef { get; set; }
    
            public string CategoryName { get; set; }
        }
    }
    

    Then build the assembler:

    public class CategoryDescAssembler {
        public CategoryDescDTO WriteDto(CategoryDesc categoryDesc) {
            var categoryDescDto = new CategoryDescDTO();
            categoryDescDto.CategoryRef = categoryDesc.CategoryRef;
            categoryDescDto.CategoryName = categoryDesc.CategoryName;
            return categoryDescDto;
        }
    }
    

    Now you implement the service to do all the work required to get the DTO object:

    public class CategoryDescService : ICategoryDescService {
        private readonly IRepository<CategoryDesc> _categoryDescRepository;
        private readonly CategoryDescAssembler _categoryDescAssembler;
    
        public CategoryDescService(IRepository<CategoryDesc> categoryDescRepository, CategoryDescAssembler categoryDescAssembler) {
            _categoryDescRepository= categoryDescRepository;
            _categoryDescAssembler= categoryDescAssembler;
        }
    
        public CategoryDescDTO GetCategoryDesc(int categoryRef) {
            var categDesc = _categoryDescRepository.Get(x => x.CategoryRef == categoryRef);
            return _categoryDescAssembler.WriteDto(categDesc);
        }
    }
    

    With the interface looking like this:

    public interface ICategoryDescService
    {
        CategoryDescDTO GetCategoryDesc(int categoryRef);
    } 
    

    You would then need to add the service to your Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddTransient<ICategoryDescService, CategoryDescService>();
    }
    

    Now you can call your service from you view controller.