Domanda

Or is there another solution... Here comes my explanation: I have a typed object called Customer (for example) with lots of properties and I would like to map it to a dynamic Expando Object or IDictionary. I mean this would be the desired solution but the second type could also be a static type. But anyway more details here:

src object would be

Customer
  -> Name
  -> LastName
  -> DOB
  -> FirstPurchaseDate
  -> LastVisitHompage

what ever 20 properies more...

Now I would like to take a Dictionary that defines my mappings of property names.

Name     = Line1TextField
LastName = Line17TextField
DOB      = Line3DateField
...

and so on

now I would like to pass in the src object (my customer) and get out an expando Object with the properties according to my definition dictionary.

I thought first I could use the ForMember method in a foreach loop but ForMember is strongly typed with lambda expression. So somthing like ForMember(IDictrionary) or ForMember(string, string) does not exist to give the member mapping some naming hints

I have to admit that I do not know enough about AutoMapper (but heard a lot of good things when it comes to object mapping). So is my use case a case where I would use AutoMapper at all?

I could make the second type also a static type because I think AutoMapper does not work with dynamic types. But the core question is more can I define my member string map in a dictionary or read it from xml to pass it to my mapping? Does this makes sense at all? Are there other ways to define my mapping without manually wiring up all Members for all classes?

Or do I have just write some left, right code over my mapping dictionary? With some reflections or expressions?

Sorry that I cannot be more precise at this moment or have any code...

Thanks for any help or opinions?

È stato utile?

Soluzione

After researching a little more and a nice conversation with Jimmy Bogard the author of AutoMapper on twitter. I realized that AutoMapper's main purpose and usefulness is to map objects if the target object and source objects naming convention is the same.

Or at least when you be able to define a convention. Since this is not the case in my example AutoMapper is not so usefull for this. So I ended up writing my own projection code.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top