Parameterization in nested mappings

59 views
Skip to first unread message

kristian....@gmail.com

unread,
Nov 2, 2015, 10:55:31 AM11/2/15
to AutoMapper-users
Hi,
I have a question about passing parameters using ProjectTo<SampleViewModel>(), not to "SampleViewModel" but to nested mapped model.

I have custom mapping configuration and create all configurations in the view-models, but sometimes I need to pass parameters not only to the top view model but also to the nested ones.

Example for mappings: here

Scenario:

Controller:
var userId = this.User.Identity.GetUserId();
db.ForumQuestions.ProjectTo<ForumQuestionViewModel>(new { userId });

ForumQuestionViewModel : IMapFrom<ForumQuestion>, IHaveCustomMappings
{
    public string Title { get; set; }

    public string Content { get; set; }
    
    public bool IsReportedByUser { get; set;}

    public IEnumerable<ForumAnswerViewModel> Answers { get; set; }

     public void CreateMappings(IConfiguration configuration)
     {
           var userId = string.Empty;
           configuration.CreateMap<ForumQuestion, ForumQuestionViewModel>()
                .ForMember(m => m.IsReportedByUser, opt => opt.MapFrom(e => e.Reports.Any(r => r.ReporterId == userId)));
     }
}

Let's assume that we need to know if the current user has reported this question for something.
So far so good
This works, but I want to pass parameters to answers to see if the current user has reported the answer too.

ForumAnswerViewModel : IMapFrom<Answer>, IHaveCustomMappings
{
     public string Content { get; set; }

     public bool IsReportedByUser { get; set;}
}

So, the question is: Is this possible?

Jimmy Bogard

unread,
Nov 2, 2015, 1:38:52 PM11/2/15
to automapper-users
It should work, is this not working today?

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kristian....@gmail.com

unread,
Nov 3, 2015, 2:57:39 AM11/3/15
to AutoMapper-users
Hmm, I was not sure, is this possible, and I don't even test it.
But now I test it and it works.
Thanks for the quick response.
Reply all
Reply to author
Forward
0 new messages