0

I´m building an automapper with the following structures:

Domain:

public class EnterpriseInfo
{
    public string Name { get; set; }
    public string Description { get; set; }
    public bool Installed { get; set; }
    public bool Enabled { get; set; }
    public List<Msg> MsgList { get; set; }

    ...some methods...
}


public class Msg
{ 
    public DateTime DateTime { get; set; }
    public string From { get; set; }
    public string Message { get; set; }
 }

ViewModel classes:

public class HomeLogonHomeViewModel
{
    public string CompanyName;
    public List<SysMsg> MsgList;
}

   public class SysMsg 
   {   
        public DateTime DateTime { get; set; }
        public string From { get; set; }
        public string Message { get; set; }
   }

In the view I need to show something like:

Messages for company: BALBALBALBA

12/12/12 00:00 From: AAA Message: Nnonononon
12/12/12 00:00 From: AAA Message: Nnonononon
12/12/12 00:00 From: AAA Message: Nnonononon
12/12/12 00:00 From: AAA Message: Nnonononon
12/12/12 00:00 From: AAA Message: Nnonononon
12/12/12 00:00 From: AAA Message: Nnonononon

I´m in trouble configuring the automapper, as I have a sigle field with a single field and a list with another list. Can someone please help me...

Also, I may ask if this does indeed needs an Automapper....

[EDIT]

Working code:

    Mapper.CreateMap<EnterpriseInfo, HomeLogonHomeViewModel>()
        .ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => src.Name));

There is not need to explicit Msg to SysMsg if it was changed to be the same name.

1 Answer 1

1

You need to map EnterpriseInfo to HomeLogonHomeViewModel. I would suggest naming the Msg class in the domain to SysMsg or visa versa. Then explicity map the CompanyName member and the SysMsg member. Something like this...

Mapper.CreateMap<EnterpriseInfo,HomeLogonHomeViewModel>()
  .ForMember(dest=>dest.CompanyName,opt=> opt.Name)
  .ForMember(dest=>dest.SysMsg, opt=>opt.SysMsg);
Sign up to request clarification or add additional context in comments.

3 Comments

Grag, thanks. My worry is about the List<> type. Is AutoMapper going to solve MsgList automatically ?
You do not need to worry about List<> constructs. Automapper will automatically map each item in the list if the object item is mapped.
Yes, I noticed it after some doc reading. It´s working fine. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.