Home > Mobile >  Complex Mapping of objects Automapper c#
Complex Mapping of objects Automapper c#

Time:06-09

I have 4 classes

Class 1:

Public class user
{
 Public int Id {get;set;}
 Public string username {get;set;}
 Public ICollection<Project> 
 projects{get;set;}
 Public    
 ICollection<Projectmembers>
 Projectmembers{get;set;}
}

Class 2:

Public class project
{
Public int Id{get;set;}
Public string projectname
{get;set;}
Public int userid {get;set;}
Public ICollection<ProjectReq
{get;set;}
[Foreignkey(nameof(userid))]
Public user userfield{get;set;}
}

Class 3:

Public class ProjectReq
{
Public int Id {get;set;}
Public int projectid {get;set;}
Public string role {get;set;}

[Foreignkey(nameof(projectid))]
Public project proj {get;set;}
Public ICollection<Projectmembers> 
{get;set;}
}

Class 4:

Public class Projectmembers
{
Public int Id {get;set;}
Public int reqid {get;set;}
Public int userid {get;set;}

[Foreignkey(nameof(reqid))]
Public project projectReq 
{get;set;}
[Foreignkey(nameof(userid))]
Public user userfield {get;set;}

}

How can I map project class to the below view model;

 Public class Projectmemberview
 {
 Public string 
 projectname{get;set;}
 Public string username{get;set;}
 Public string role{get;set;}
 }

I have used linq and got result to project entity model please help me with mapping it to the Projectmemberview model

CodePudding user response:

Assuming you had lists of each object type (Project, User, ProjectReq), you could iterate over the Project list, and for each one create a ProjectMemberView like so:

            List<Project> projects = new List<Project>();

            List<User> users = new List<User>();

            List<ProjectReq> projectReqs = new List<ProjectReq>();

            // ...


            // ...

            List<ProjectMemberView> projectMemberViews = new List<ProjectMemberView>();
         
            // for each of our projects
            foreach (Project project in projects)
            {
                // add to our project member view list
                projectMemberViews.Add(

                    // a new projectmemberview
                    new ProjectMemberView()
                    {
                        // whose project name is the current project's name
                        ProjectName = project.ProjectName,

                        // the username is the user's whose Id is equal to the current project's user id
                        Username = users.Find(x => x.Id == project.UserId).Username,

                        // the role is the projectreq's role whose project Id is shared with the current project
                        Role = projectReqs.Find(x => x.ProjectId == project.Id).Role
                    });
            }


This code is incomplete because of assumptions made, and may need error handling.

CodePudding user response:

I tried as below:

public class SomeProfile : Profile
    {
        public SomeProfile()
        {
            CreateMap<Projectmembers, Projectmemberview>().ForMember(x => x.projectname, y => y.MapFrom(s => s.projectReq.projectname)).
                ForMember(x => x.username, y => y.MapFrom(s => s.userfield.username)).
                 
                 ForMember(x => x.role, y => y.Ignore());
        }        
    }

I couldn't understand how you want to map the role property (the source of role property is in ProjectReq Class,and in your project class there's a collection of ProjectReq),So I ignored the property,it may help if you could check your model and explain to me .

in startup:

services.AddAutoMapper(typeof(SomeProfile));

In controller:

    public class SomeController : Controller
        {
            
            private readonly IMapper _mapper;
                 
            public SomeController( IMapper mapper)
            {
               
                _mapper = mapper;
            }
    
            
            public IActionResult SomeAction()
            {
                ......
                var user = new user() { Id = 1, username = "somename" };
                var project = new project() { Id = 1, projectname = "someproj" };
                var projmembers = new Projectmembers() { projectReq = project, userfield = user };
                var projectmemberview = _mapper.Map<Projectmemberview>(projmembers);
                ......
            }
         }

The result: enter image description here

  • Related