1

I'm working in a project that uses AutoMapper to map from our ORM (Object-Relational Mapper) POCOs (Plain Old CLR Objects) to the view models we present to the UI.

I have an instance in which doing so involves aggregating records.

The POCO that is our input:

public class WorkRecord
{
    public int id {get; set; }
    public string work { get; set; }
    public string type { get; set; }
    public string result { get; set; }
}

And what we want as an output:

public class WorkViewModel
{
    public string Work { get; set; }
    public List<WorkInstance> WorkInstances { get; set; }

    public class WorkInstance
    {
        public string Type { get; set; }
        public string Result { get; set; }
    }
}

What I want is to convert a list of WorkRecords into a list of WorkViewModels, where the number of WorkViewModels output is not the same as the number of WorkRecords.

I want one WorkViewModel for each distinct WorkRecord.work, with a child WorkViewModel.WorkInstance created for each WorkRecord with that WorkRecord.work.

E.g.:

[
    {id: 1, work: "WorkA", type: "Type1", result: "Success"},
    {id: 2, work: "WorkA", type: "Type2", result: "Failure"},
    {id: 3, work: "WorkB", type: "Type3", result: "Success"},
    {id: 4, work: "WorkB", type: "Type4", result: "Failure"},
]

Should result in:

[
    {
        Work: "WorkA",
        WorkInstances: [
            WorkInstance: {
                Type: "Type1",
                Result: "Success"
            },
            WorkInstance: {
                Type: "Type2",
                Result: "Failure"
            }
        ]
    },
    {
        Work: "WorkB",
        WorkInstances: [
            WorkInstance: {
                Type: "Type3",
                Result: "Success"
            },
            WorkInstance: {
                Type: "Type4",
                Result: "Failure"
            }
        ]
    }
]

I'm not seeing a great way of doing this in AutoMapper.

Any ideas?


Edited to fix syntax on WorkInstance declaration.

What I've not been about to figure out is how to map a collection of n input objects to m output objects, where m < n.

Jeff Dege
  • 11,190
  • 22
  • 96
  • 165
  • You might want to consider using a [canonical data model (CDM)](https://www.enterpriseintegrationpatterns.com/patterns/messaging/CanonicalDataModel.html) and use it across all tiers instead of creating unique variations per tier. These POCOs are tech-and-tier-neutral whilst still able to be used in EF, WCF/REST, ASP.NET, WPF and so on. The benefit of CDM is it `reduces duplication; maintenance; testing and eliminates potential data fidelity loss`. Not to mention _compute_ in any code-based data transformation that normally may be required –  Jan 16 '20 at 22:37
  • Does this answer your question? [Automapper and mapping list within a complex object / nested mappings](https://stackoverflow.com/questions/24809956/automapper-and-mapping-list-within-a-complex-object-nested-mappings) –  Jan 16 '20 at 22:49
  • We deliver content to multiple customers, with distinct delivery models for each. Using a single constant model won't work for us. – Jeff Dege Jan 16 '20 at 23:54
  • The reason I would like to do this in AutoMapper is so that we can quit for the appropriate type for a specific customer, then use AutoMapper's generic mapping functions to map too that type. – Jeff Dege Jan 16 '20 at 23:56
  • Sure. You can still have a _CDM per customer_. –  Jan 17 '20 at 08:55

0 Answers0