views:

224

answers:

1

Is there a way to get Automapper to map a complex source graph like:

public class Source {
    public string Name { get; set; }
    public SourceSub Sub { get; set; }
}

public class SourceSub {
    public string ValA { get; set; }
    public string ValB { get; set; }
}

to a flat destination that looks like:

public class Dest {
    public string Name { get; set; }
    public string ValA { get; set; }
    public string ValB { get; set; }
}

I know something like this will work for a destination:

public class Dest {
    public string Name { get; set; }
    public string SubValA { get; set; }
    public string SubValB { get; set; }
}

However, I am looking for a way to map to the destination without requiring a prefix in the destination properties (for the child class in the source) as long as the names in the child class properties of the source match the destination property names. Is there a way to tell Automapper to project properties in a child class of the source to a flat destination class without mapping each individual member?

+1  A: 

No, this isn't a supported scenario right now. We looked at it for a while, but found the naming collision rate too high for our apps, and having the name flattened preserved the full context for where that value came from.

Jimmy Bogard
@Jimmy: Thanks for answering. First, let me say that we do love AutoMapper. Thanks for your contribution.It would be nice to add a feature to V2 to support an option for mapping different prefixes than what the child class is named to the flattened object. Also, maybe an option to turn on/off what I am trying to do for individual maps. Just a request, I know there are plenty of other requests in your queue and this one may conflict or be too difficult.
Matt Spradley
I would also like a way to be able to do this.
Schneider