Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the. NET Core Dto mapping

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to understand. NET Core Dto mapping, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Let's assume that in a scenario where EF Core+Web Api is used, there may be a deviation in the correspondence between the Entity Model in EF Core and the Model used in the project. If one-to-one correspondence is used, there will inevitably be a lot of work.

All right, let's go straight to the code to explain.

EntityModels.Employee.cs

Public class Employee {public Guid Id {get; set;} public Guid CompanyId {get; set;} public string EmployeeNo {get; set;} public string FirstName {get; set;} public string LastName {get; set;} public Gender Gender {get; set;} public DateTime DateOfBirth {get; set;} public Company Company {get; set;}}

Models.EmployeeDto.cs

Public class EmployeeDto {public Guid Id {get; set;} public Guid CompanyId {get; set;} public string EmployeeNo {get; set;} public string Name {get; set;} public string GenderDisplay {get; set;} public int Age {get; set;}}

We can observe that there are many differences between the two entities, such as name split, date of birth and age, and so on. What I did before was hard hand-written code conversion, but after learning the code of the bosses, I found a class library, so let's make a record here.

AutoMapper.Extensions.Microsoft.DependencyInjection

You can install it through NuGet, and it's very easy to use it. Set up a Profile folder and create a new EmployeeProfile class that inherits from Profile.

Public class EmployeeProfile: Profile {public EmployeeProfile () {CreateMap () .ForMember (dest = > dest.Name, opt = > opt.MapFrom (src = > $"{src.FirstName} {src.LastName}")) .ForMember (dest = > dest.GenderDisplay, opt = > opt.MapFrom (src = > src.Gender.ToString () .ForMember (dest = > dest.Age Opt = > opt.MapFrom (src = > DateTime.Now.Year-src.DateOfBirth.Year)) }}

In the use of this only need to inject mapper, and then is a very simple operation, simply, the function is very powerful!

[ApiController] [Route ("api/companies/ {companyId} / employees")] public class EmployeesController: ControllerBase {private readonly IMapper _ mapper; private readonly ICompanyRepository _ companyRepository; public EmployeesController (IMapper mapper, ICompanyRepository companyRepository) {_ mapper = mapper? Throw new ArgumentNullException (nameof (mapper)); _ companyRepository = companyRepository? Throw new ArgumentNullException (nameof (companyRepository));} [HttpPost] public async Task CreateEmployeeForCompany (Guid companyId, EmployeeAddDto employee) {if (! await _ companyRepository.CompanyExistsAsync (companyId)) {return NotFound ();} var entity = _ mapper.Map (employee); _ companyRepository.AddEmployee (companyId, entity); await _ companyRepository.SaveAsync () Var dtoToReturn = _ mapper.Map (entity); return CreatedAtRoute (nameof (GetEmployeeForCompany), new {companyId = companyId, employeeId = dtoToReturn.Id}, dtoToReturn);}} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report