c# - Is there a way to call generic repository methods on an entity that was generated at run time with codeDOM? -


i using codedom generate entity classes @ run time. have generic repository deal various db functionality. here insert method example method in generic repository:

public void insert<tentity>(tentity entity) tentity : class, ibusinessentity {     if (entity == null)     {         throw new argumentnullexception("entity");     }      tentity existing = existing(entity);     if (existing == null)     {         _context.set<tentity>().add(entity);         this._context.savechanges();     } } 

here example code of how generate entity class , how create entity based on entity class using codedom:

//generate fields of new entity class entitygenerator.entityfieldinfo entityfield1 = new entitygenerator.entityfieldinfo("name", typeof(string), relationshiptype.norelation); entitygenerator.entityfieldinfo entityfield2 = new entitygenerator.entityfieldinfo("shape", typeof(string), relationshiptype.norelation); icollection<entitygenerator.entityfieldinfo> entityfieldlist = new list<entitygenerator.entityfieldinfo> { entityfield1, entityfield2 };  // create new entity class using fields established above  // name of entity (typename = "thing") string typename = "thing"; entitygenerator.createentityclass(entityfieldlist, typename); compilerresults results = entitygenerator.getcompiledentity(typename);  // create entity instance based on new entity class created object newthing = entitygenerator.createinstanceofentity(results, typename); setobjectfield(newentity, "name", "box"); setobjectfield(newentity, "shape", "cuboid"); 

as can see, newthing (the new entity instance) object type. if hardcoded entity class

thing newthing; 

but thing entity created codedom isn't hardcoded class have use type object instead of type thing. problem because using generic repository. let's want insert entity database. call:

myrepository.insert<thing>(newthing); 

however, thing created codedom @ run time, isn't class, means can't go inside <>. may have noticed above in insert method, tentity ibusinessentity. if try

myrepository.insert<ibusinessentity>(newthing); 

i error:

argument type 'object' not assignable parameter type 'models.ibusinessentity'

if try without inside <>, this:

myrepository.insert(newthing);

i error:

the type 'object' must convertible 'models.ibusinessentity' in order use parameter 'tentity' in generic method 'void insert(tentity)'.

does know how can reconcile codedom generated entity generic repository? reflection help? nice if reflection somehow give me class of thing passed <>. should note entities create codedom extend ibusinessentity.

i think hard make work because dbsets contained in dbcontext used ef create mappings. how think create them?

anyway, don't need type work ef, can use gettype. in methods (existing(.) missing think similar) can use

public void insert(object entity) {     if (entity == null)         throw new argumentnullexception("entity");      if (!(entity ibusinessentity))         throw new argumentinvalidexception("entity not ibusinessentity");      object existing = existing(entity);     if (existing == null)     {         _context.set(entity.gettype()).add(entity);         this._context.savechanges();     } 

}

using set<> or set(.) i'm quite sure ef search in mappings created starting dbsets contained in dbcontext. can't remember exact exception sow different times (when used dbcontext.set(myentitytype)).


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -