如何使用AutoMapper映射对象的子级列表

时间:2019-07-04 14:19:49

标签: c# .net-core automapper

我有这两个表“ User”和“ Project”。他们分享一对多的关系。 我得到的响应是一个列表,我想用我创建的GetProjectManager的dto映射此响应,但是我不确定如何在自动映射器配置文件中定义CreateMap。 我从ProjectManager类型的API得到以下响应:

ProjectManager:

'Insert Shape into Footer
'behaves differently for doc/docx
Sub CATMain()
    Dim strPathImg As String
    strPathImg = "C:\Test\avatar.jpg"
    Dim wrdApp
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Dim myWrdDoc
    Set myWrdDoc = 'wrdApp.Documents.Open("C:\Test\Document.doc") 'change path for .docx to: "C:\Test\Document.docx"

    Dim wrdFootShape
    Set wrdFootShape = myWrdDoc.Sections.Last.Footers(1).Shapes
    Dim objPic As Object
    Dim posPic As Double
    'Convert position in centimeters to points for the AddPicture method
    posPic = CentimetersToPoints(2)

    If myWrdDoc.SaveFormat = 0 Then
        'correction for doc file
        posPic = posPic - myWrdDoc.PageSetup.PageHeight + myWrdDoc.PageSetup.TopMargin
    ElseIf myWrdDoc.SaveFormat = 12 Then
        'docx file
        posPic = posPic
    End If
    Set objPic = wrdFootShape.AddPicture(fileName:=strPathImg, _
      LinkToFile:=False, SaveWithDocument:=True, Left:=120, Top:=posPic)
End Sub

我想将此响应与我创建的Dto(GetProjectManager)映射。

[
  {
    "id": 1,
    "projectId": 1,
    "userId": 1,
    "project": {
      "id": 1,
      "projectName": "iDocument-FBAG",
      "plannedStartDate": "2019-07-03T00:00:00",
      "plannedEndDate": "2019-07-10T00:00:00",
      "actualStartDate": null,
      "actualEndDate": null,
      "projectDescription": "The client wants to enhance their existing .Net application. They have a WebForms application that uses DevExpress controls. They would like to start by implementing a chart type and creating a reusable report, also use that reusable report to implement some new reports.",
      "onProjects": [],
      "projectManagers": []
    },
    "user": {
      "firstName": "Abc",
      "lastName": "Efg",
      "isProjectManager": true,
      "registrationTime": "2019-06-28T19:28:55.8386138",
      "employees": null,
      "id": 1,
      "userName": "xyz@abc.com"
    }
  },
  {
    "id": 3,
    "projectId": 8,
    "userId": 1,
    "project": {
      "id": 8,
      "projectName": "Aquatrols-FBAG",
      "plannedStartDate": "2019-07-03T00:00:00",
      "plannedEndDate": "2019-07-10T00:00:00",
      "actualStartDate": null,
      "actualEndDate": null,
      "projectDescription": "The client wants to enhance their existing .Net application. They have a WebForms application that uses DevExpress controls. They would like to start by implementing a chart type and creating a reusable report, also use that reusable report to implement some new reports.",
      "onProjects": [],
      "projectManagers": []
    },
    "user": {
      "firstName": "Abc",
      "lastName": "Efg",
      "isProjectManager": true,
      "registrationTime": "2019-06-28T19:28:55.8386138",
      "employees": null,
      "id": 1,
      "userName": "xyz@abc.com"
    }
  }
]

2 个答案:

答案 0 :(得分:0)

首先,您需要将响应json映射为类型

namespace Response
{
    public class ProjectManager
    {
        public int id { get; set; }
        public int projectId { get; set; }
        public int userId { get; set; }
        public Project project { get; set; }
        public User user { get; set; }
    }

    public class Project
    {
        public int id { get; set; }
        public string projectName { get; set; }
        public DateTime plannedStartDate { get; set; }
        public DateTime plannedEndDate { get; set; }
        public object actualStartDate { get; set; }
        public object actualEndDate { get; set; }
        public string projectDescription { get; set; }
        public object[] onProjects { get; set; }
        public object[] projectManagers { get; set; }
    }

    public class User
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public bool isProjectManager { get; set; }
        public DateTime registrationTime { get; set; }
        public object employees { get; set; }
        public int id { get; set; }
        public string userName { get; set; }
    }
}

这是你的dto

namespace Dto
{
    public class GetProjectManager
    {
        public string ProjectManagerId { get; set; }
        public string Email { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public IList<GetProjectDto> GetProjects { get; set; }
    }

    public class GetProjectDto
    {
        public int ProjectId { get; set; }
        public string ProjectName { get; set; }
        public DateTime PlannedStartDate { get; set; }
        public DateTime PlannedEndDate { get; set; }
        public DateTime? ActualStartDate { get; set; }
        public DateTime? ActualEndDate { get; set; }
        public string ProjectDescription { get; set; }
    }
}

然后,创建自动映射器配置文件以响应dto映射

public class ResponseToDtoProfile : AutoMapper.Profile
{
    public ResponseToDtoProfile()
    {
        CreateMap<Response.Project, Dto.GetProjectDto>()
            .ForMember(dst => dst.ProjectId, opt => opt.MapFrom(src => src.id))
            .ForMember(dst => dst.ProjectName, opt => opt.MapFrom(src => src.projectName))
            .ForMember(dst => dst.PlannedStartDate, opt => opt.MapFrom(src => src.plannedStartDate))
            .ForMember(dst => dst.PlannedEndDate, opt => opt.MapFrom(src => src.plannedEndDate))
            .ForMember(dst => dst.ActualStartDate, opt => opt.MapFrom(src => src.actualStartDate))
            .ForMember(dst => dst.ActualEndDate, opt => opt.MapFrom(src => src.actualEndDate))
            .ForMember(dst => dst.ProjectDescription, opt => opt.MapFrom(src => src.projectDescription));

        CreateMap<Response.ProjectManager, Dto.GetProjectManager>()
            .ForMember(dst => dst.ProjectManagerId, opt => opt.MapFrom(src => src.id))
            .ForMember(dst => dst.FirstName, opt => opt.MapFrom(src => src.user.firstName))
            .ForMember(dst => dst.LastName, opt => opt.MapFrom(src => src.user.lastName))
            .ForMember(dst => dst.GetProjects, opt => opt.MapFrom(src => src.project));
    }
}

要投影json数组,让我们在集合上添加扩展方法

public static class ExtensionsHelper
{
    public static List<TProjection> ProjectAsCollection<TProjection>(this IEnumerable<object> items) where TProjection : class
    {
        //var adapter = <Create Type Adapter>
        return adapter.Adapt<List<TProjection>>(items);
    }
}

最后,您可以将json数组投影到dto。

static void Main(string[] args)
{
    string jsonString = "[ { \"id\": 1, \"projectId\": 1, \"userId\": 1, \"project\": { \"id\": 1, \"projectName\": \"iDocument-FBAG\", \"plannedStartDate\": \"2019-07-03T00:00:00\", \"plannedEndDate\": \"2019-07-10T00:00:00\", \"actualStartDate\": null, \"actualEndDate\": null, \"projectDescription\": \"The client wants to enhance their existing .Net application. They have a WebForms application that uses DevExpress controls. They would like to start by implementing a chart type and creating a reusable report, also use that reusable report to implement some new reports.\", \"onProjects\": [], \"projectManagers\": [] }, \"user\": { \"firstName\": \"Abc\", \"lastName\": \"Efg\", \"isProjectManager\": true, \"registrationTime\": \"2019-06-28T19:28:55.8386138\", \"employees\": null, \"id\": 1, \"userName\": \"xyz@abc.com\" } }, { \"id\": 3, \"projectId\": 8, \"userId\": 1, \"project\": { \"id\": 8, \"projectName\": \"Aquatrols-FBAG\", \"plannedStartDate\": \"2019-07-03T00:00:00\", \"plannedEndDate\": \"2019-07-10T00:00:00\", \"actualStartDate\": null, \"actualEndDate\": null, \"projectDescription\": \"The client wants to enhance their existing .Net application. They have a WebForms application that uses DevExpress controls. They would like to start by implementing a chart type and creating a reusable report, also use that reusable report to implement some new reports.\", \"onProjects\": [], \"projectManagers\": [] }, \"user\": { \"firstName\": \"Abc\", \"lastName\": \"Efg\", \"isProjectManager\": true, \"registrationTime\": \"2019-06-28T19:28:55.8386138\", \"employees\": null, \"id\": 1, \"userName\": \"xyz@abc.com\" } } ]";
    var rootObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Response.ProjectManager[]>(jsonString).ToList().ProjectAsCollection<Dto.GetProjectManager>();
}

我希望能回答这个问题。

答案 1 :(得分:0)

I had to play around with the CreateMap in the AutoMapper profile and it solved my problem. 

这就是我定义映射的方式。

CreateMap<ProjectManager, GetProjectDto>()
                            .ForMember(dst => dst.ProjectId, opt => opt.MapFrom(src => src.Project.Id))
                            .ForMember(dst => dst.ProjectName, opt => opt.MapFrom(src => src.Project.ProjectName))
                            .ForMember(dst => dst.PlannedStartDate, opt => opt.MapFrom(src => src.Project.PlannedStartDate))
                            .ForMember(dst => dst.PlannedEndDate, opt => opt.MapFrom(src => src.Project.PlannedEndDate))
                            .ForMember(dst => dst.ActualStartDate, opt => opt.MapFrom(src => src.Project.ActualStartDate))
                            .ForMember(dst => dst.ActualEndDate, opt => opt.MapFrom(src => src.Project.ActualEndDate))
                            .ForMember(dst => dst.ProjectDescription, opt => opt.MapFrom(src => src.Project.ProjectDescription));

                CreateMap<List<ProjectManager>, GetProjectManager>()
                               .ForMember(dst => dst.ProjectManagerId,
                                            opt => opt.MapFrom(src =>
                                                                src.FirstOrDefault().UserId))
                               .ForMember(dst => dst.FirstName,
                                            opt => opt.MapFrom(src =>
                                                                src.FirstOrDefault().User.FirstName))
                               .ForMember(dst => dst.LastName,
                                            opt => opt.MapFrom(src =>
                                                                src.FirstOrDefault().User.LastName))
                               .ForMember(dst => dst.Email,
                                            opt => opt.MapFrom(src =>
                                                                src.FirstOrDefault().User.Email))
                                .ForMember(dst => dst.GetProjects,
                                             opt => opt.MapFrom(src =>
                                                                src.Select(x => new ProjectManager
                                                                {
                                                                    Project = x.Project
                                                                })));