c# - Converting an entity / model back to DataTable -
for legacy reasons, having phone call asmx web services give me datatable
instead of entity, , in turn pass info table web service. i'm using term entity here, there no orm entire info layer (crud) done these web services. phone call web method gives me info table, convert info table entity, update entity, convert info table , pass info table web method.
at moment i'm using automapper convert info table's row entity profile
:
mapper.createmap<datarow, myentity>() .formember(d => d.property, o => o.mapfrom(s => s["property_column"])) // , on
which works fine, what wanted do entity info table row utilize profile
(which unfortunately doesn't work):
mapper.createmap<myentity, datarow>() .formember(d => d["property_column"], o => o.mapfrom(s => s.property)) // , on
so, because automapper can't think i'll have utilize reflection - brings me other problem. entity property names don't match info row's column names. can't datarow[propertyinfo.name] = value
.
i thinking of using entitytypeconfiguration
class (for each entity) map property target column, mean i'd need references entity framework within project - i'd prefer avoid won't used. sec (and preferred) thought create custom attributes entities contain target column name , in converttodatatable<t>
class reflect on these attributes hydrate info row.
does have improve way of achieving this, i've overlooked?
for benefit of stumbling on question, ended creating custom automapper convertor next itypeconverter
contract. @ moment i'm creating custom convertor each entity type, illustration myentity
here's do:
public class myentitytodatatableconvertor : itypeconvertor<myentity, datatable> { public datatable convert(resolutioncontext context) { myentity myentity = (myentity)context.sourcevalue; datatable dt = getdatatableschema(); datarow nr = dt.newrow(); nr["property_column"] = myentity.property; // , on homecoming dt; } }
then tell automapper convertusing
as:
mapper.createmap<myentity, datatable>() .convertusing(new myentitytodatatableconvertor ());
i'm sure can refactor lot utilize reflection , generics, in short-term working great me.
c# datatable automapper
No comments:
Post a Comment