I have been scouring the internet but still haven't found what I'm looking for ( what U2? ). Enough of that, what I am looking for is an intuitive way to parse a poco ( complex with Lists, nullables etc ) to a Dictionary to be stored in a key/value database and then a means to re-populate the model back. In essence I am trying to achieve what the MVC model binder does when it maps from form parameters to a model, but them be able to do the reverse as well. I have tried digging into the source but it requires all the dependencies of a controller and I dont really want that. here is some source as an example
public class Item
{
public string Name { get; set; }
public decimal? Amount { get; set; }
public List<RowItemModel> Items { get; set; }
}
public class RowItemModel
{
public string Description { get; set; }
public decimal Amount { get; set; }
}
So there is the model, and here is what I would like the outcome to look like :
P.S. : Note I have added a prefix for demarcation purposes in the db -> "Item."
Item.Name : The Name Here
Item.Amount : 0.0
Item.Items[0].Description : Description Here
Item.Items[0].Amount : 50
Item.Items[1].Description : Second Description
As you can see, it follows the exact same naming conventions as you would use when creating form elements for binding to a model ( using MVC ). What I am looking for is an elegant way to bind to the model from that ( dictionary ) and then to generate the dictionary from the populated model.
I have already achieved this using some really ugly string manipulation and then basic reflection combined with recursion, but it's leaving me with a bad taste in my mouth and I can't bring myself to push it to production, so any frameworks / elegant solutions would be appreciated.
Aucun commentaire:
Enregistrer un commentaire