{"id":194602,"date":"2020-07-05T09:18:07","date_gmt":"2020-07-05T01:18:07","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=194602"},"modified":"2020-06-27T11:19:59","modified_gmt":"2020-06-27T03:19:59","slug":"netcore-dto-automapper","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/netcore-dto-automapper.html","title":{"rendered":".NET Core Dto\u6620\u5c04(AutoMapper)"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6211\u4eec\u5047\u8bbe\u4e00\u4e2a\u573a\u666f, \u91c7\u7528EF Core+Web Api, \u8fd9\u65f6\u5019\u53ef\u80fd\u4f1a\u51fa\u73b0EF Core\u4e2d\u7684Entity Model\u548c\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u7684Model\u4e4b\u95f4\u5bf9\u5e94\u5173\u7cfb\u51fa\u73b0\u504f\u5dee, \u5982\u679c\u4f7f\u7528\u5c5e\u6027\u610f\u4e00\u4e00\u5bf9\u5e94, \u4e0d\u514d\u4f1a\u6709\u5927\u91cf\u7684\u5de5\u4f5c\u91cf<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u597d\u4e86\uff0c\u63a5\u4e0b\u6765\u76f4\u63a5\u4e0a\u4ee3\u7801\u6765\u8bf4\u660e\u3002<\/p>\n

EntityModels.Employee.cs<\/p>\n

\r\npublic class Employee\r\n{\r\n    public Guid Id { get; set; }\r\n    public Guid CompanyId { get; set; }\r\n\r\n    public string EmployeeNo { get; set; }\r\n    public string FirstName { get; set; }\r\n    public string LastName { get; set; }\r\n    public Gender Gender { get; set; }\r\n    public DateTime DateOfBirth { get; set; }\r\n\r\n    public Company Company { get; set; }\r\n}\r\n<\/pre>\n

Models.EmployeeDto.cs<\/p>\n

\r\npublic class EmployeeDto\r\n{\r\n    public Guid Id { get; set; }\r\n    public Guid CompanyId { get; set; }\r\n\r\n    public string EmployeeNo { get; set; }\r\n    public string Name { get; set; }\r\n    public string GenderDisplay { get; set; }\r\n    public int Age { get; set; }\r\n}\r\n<\/pre>\n

\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230, \u4e24\u4e2a\u5b9e\u4f53\u4e4b\u95f4\u6709\u5f88\u591a\u4e0d\u540c\u7684\u5730\u65b9, \u6bd4\u5982\u540d\u5b57\u62c6\u5206\u554a, \u51fa\u751f\u65e5\u671f\u548c\u5e74\u9f84\u554a\u7b49\u7b49, \u6211\u4e4b\u524d\u7684\u505a\u6cd5\u5c31\u662f\u786c\u751f\u751f\u624b\u5199\u4ee3\u7801\u8f6c\u6362, \u4f46\u662f\u7ecf\u8fc7\u5b66\u4e60\u5927\u4f6c\u4eec\u7684\u4ee3\u7801, \u53d1\u73b0\u4e86\u4e00\u4e2a\u7c7b\u5e93, \u5728\u6b64\u7b97\u662f\u8bb0\u5f55\u4e00\u4e0b\u5427<\/p>\n

AutoMapper.Extensions.Microsoft.DependencyInjection<\/code><\/p>\n

\u53ef\u4ee5\u901a\u8fc7NuGet\u5b89\u88c5\uff0c\u4f7f\u7528\u7684\u65f6\u5019\u4e5f\u975e\u5e38\u7b80\u5355, \u5efa\u7acbProfile\u6587\u4ef6\u5939, \u65b0\u5efa\u4e00\u4e2aEmployeeProfile\u7c7b, \u7ee7\u627f\u4e8eProfile\u3002<\/p>\n

\r\npublic class EmployeeProfile: Profile\r\n{\r\n    public EmployeeProfile()\r\n    {\r\n        CreateMap()\r\n            .ForMember(dest => dest.Name,\r\n                opt => opt.MapFrom(src => $\"{src.FirstName} {src.LastName}\"))\r\n            .ForMember(dest => dest.GenderDisplay,\r\n                opt => opt.MapFrom(src => src.Gender.ToString()))\r\n            .ForMember(dest => dest.Age,\r\n                opt => opt.MapFrom(src => DateTime.Now.Year - src.DateOfBirth.Year));\r\n    }\r\n}\r\n<\/pre>\n

\u5728\u4f7f\u7528\u7684\u65f6\u5019\u53ea\u9700\u8981\u8fd9\u6837, \u6ce8\u5165mapper, \u7136\u540e\u5c31\u662f\u5f88\u7b80\u5355\u7684\u64cd\u4f5c\u4e86, \u7b80\u76f4\u4e86, \u529f\u80fd\u975e\u5e38\u5f3a\u5927!!!<\/p>\n

\r\n[ApiController]\r\n[Route(\"api\/companies\/{companyId}\/employees\")]\r\npublic class EmployeesController: ControllerBase\r\n{\r\n    private readonly IMapper _mapper;\r\n    private readonly ICompanyRepository _companyRepository;\r\n\r\n    public EmployeesController(IMapper mapper, ICompanyRepository companyRepository)\r\n    {\r\n        _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));\r\n        _companyRepository = companyRepository ?? throw new \r\n            ArgumentNullException(nameof(companyRepository));\r\n    }\r\n    [HttpPost]\r\n    public async Task>\r\n        CreateEmployeeForCompany(Guid companyId, EmployeeAddDto employee)\r\n    {\r\n        if (!await _companyRepository.CompanyExistsAsync(companyId))\r\n        {\r\n            return NotFound();\r\n        }\r\n\r\n        var entity = _mapper.Map(employee);\r\n\r\n        _companyRepository.AddEmployee(companyId, entity);\r\n        await _companyRepository.SaveAsync();\r\n\r\n        var dtoToReturn = _mapper.Map(entity);\r\n\r\n        return CreatedAtRoute(nameof(GetEmployeeForCompany), new\r\n        {\r\n            companyId = companyId,\r\n            employeeId = dtoToReturn.Id\r\n        }, dtoToReturn);\r\n    }\r\n}\r\n<\/employeedto><\/employee><\/employeedto><\/pre>\n","protected":false},"excerpt":{"rendered":"

\u597d\u4e86\uff0c\u63a5\u4e0b\u6765\u76f4\u63a5\u4e0a\u4ee3\u7801\u6765\u8bf4\u660e\u3002 EntityModels.Employee.cs public class E […]<\/p>\n","protected":false},"author":1903,"featured_media":194886,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-194602","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/194602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=194602"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/194602\/revisions"}],"predecessor-version":[{"id":195036,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/194602\/revisions\/195036"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/194886"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=194602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=194602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=194602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}