c# - Change EF entity with descendant proxy class -


i have .net 4.0 application use entityframework 5.0 access data ms sql database.

i use database first approach. tableas mapped poco entities, has additional properties contains entities recieved web service.

database:

wh_product (id, nomenklatureid, seriesid, quantity) 

service have such data:

nomenklature (id, name, producer...) series (id, number, date...) 

poco entity:

product (id, nomenklatureid, nomenklature, seriesid, series, quantity) 

i have problem repository realisation. need implement lazy loading nomenklature , series properties.

i make productproxy class implements such loading this:

public class productproxy:product {     private nomenklature _nomenklature;    public override nomenklature nomenklature    {             {          if (_nomenklature==null)          {             _nomenklature = <code load nomenklature webservice base.nomenklatureid>          }          return _nomenklature;       }    }     private series _series;    public override series series    {             {          if (_series==null)          {             _series = <code load series webservice base.nomenklatureid>          }          return _series;       }    } } 

then change person class personproxy class in dbcontext.

public class productcontext:dbcontext {    ...    public dbset<personproxy> person {get; set;}    ... } 

the load method:

public list<person> getpersons() {     using (var ctx = new productcontext())     {         var persons = ctx.person.asenumerable().cast<person>().tolist();         return persons;     } } 

question: better way realize getpersons method without asenumerable().cast()? way of changing entity type descendant proxy type?

the proxy pattern taking advantage of polymorphism (one of 'the 3 pillars of oop'), consumer of object think deals person while in-fact he's dealing personproxy. possible because personproxy is person because derives it.

that's why can write:

person person = new personproxy(); 

then in case, getpersons() method return ienumerable<person>, follows:

public ienumerable<person> getpersons() {     using (var ctx = new productcontext())     {         return ctx.person;     } } 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -