c# - Using Reflection to Upsert Derived Entities in Generic Repository -
a bit new @ repository pattern. trying build generic repository handle state changes derived entities. i've done far create custom attribute flag property 1 needs upserted.
attribute:
public class derivedobjectattribute : attribute { public enum entitytype { reference, object } public entitytype derivedtype { get; set; } } i define attribute property on want cascade state changes.
sample entity:
public class fightingcharacter : baseentity { public costume costume { get; set; } [derivedobject(derivedtype=derivedobjectattribute.entitytype.reference)] public specialfinish specialfinish { get; set; } [derivedobject(derivedtype = derivedobjectattribute.entitytype.reference)] public list<movelist> movelist { get; set; } } so class, costume property not need cascade, specialfinish , movelist properties should.
then in repository:
public class datarepository<t> : irepository<t> t : baseentity { private void tryderivedupsert(t entity) { type type = entity.gettype(); propertyinfo[] pilist = type.getproperties(); foreach (propertyinfo pi in pilist) { foreach (derivedobjectattribute attr in pi.getcustomattributes(typeof(derivedobjectattribute), false)) { // here? } } } } in innermost loop, i'm able pinpoint derivedobjectattributes without problem. question is: how obtain type , value of object, upsert it? in other words: if property pi flagged cascade changes, create repo cast appropriate entity, , upsert it. e.g.:
datarepository<entitytype> repo = new datarepository<entitytype> (); repo.upsert(property entitytype); does make sense? or going generic repo entirely wrong way? if make sense (i'll surprised), how it? (the examples listed here examples, btw. i'm still architecting , have no ef classes @ yet.)
you value pi.getvalue(entity, null), , create generic repository (activate.createinstance) property type (from pi), have lot of reflection.
in case should think dropping classic generic repository idea (separate repository per type) , use extended dbcontext, can handle types.
if have disconnected scenario (wcf), main problem ef itself, because have replicate changes nested lists on server side , manually change entitystate.
Comments
Post a Comment