linq to sql - LINQtoSQL , repository pattern and lazy load -
how use linqtosql repository pattern?
i’m new l2s , find lazy loading real impediment using repo pattern.
usually, think of repository pattern this:
var mycustomer = null; using (var myrepo = new repo()){ mycustomer = myrepo.getcustomerforcustomerid(123); } if(mycustomer.orders.any()){ //do }
trouble is, won’t l2s attempt make data connection when mycustomer.orders interrogated? doesn’t lead unpredicatable database access issues?
i mean, yes, tell repo verify orders inside of repo confident our complete test coverage verifies developers never call entity didn't explicitly load, rather rid of lazy loading/object-datacontext persistence.
so have 4 options
- create domain objects created l2s objects – lots of work , maintainance
- create derived verisons of l2s object break linkage (http://www.codeproject.com/kb/linq/linq-to-sql-detach.aspx)
- use llblgenpro instead.
- appeal wisdom of stack overflow readers
i’m going 4 now.
how ensure objects wont call db after repo closed?
and yes, did read every stack question talks l2s , repos , none of them answer question.
sometimes is useful eager-load entity's children. can dataloadoptions.loadwith tells datacontext automatically load entity's children when parent entity loaded.
snippet msdn link above:
northwnd db = new northwnd(@"c:\northwnd.mdf"); dataloadoptions dlo = new dataloadoptions(); dlo.loadwith<customer>(c => c.orders); db.loadoptions = dlo
you can use dataloadoptions.associatewith further customize behavior of automatic loading.
Comments
Post a Comment