vb.net - Entity Framework and Repository Pattern -
i trying use repository pattern ef4 , not sure how make lazy loading work it. if return object, customer, , want able @ it's orders, maybe, not sure, how should go this?
how long should let context live for? should go , collection of orders?
any examples appreciated!!
thanks!
edit
so if have repository (assuming have poco classes category , subcategory):
public class categoryrepository implements icategoryrepository public function getcategories() list(of category) using db new dbcontext <-- entity framework context return db.categories.tolist() end using end function end class
and consume in controller:
public function index() actionresult dim m new categoryviewmodel m.categories = _repository.getcategories() return view(m) end function
if try in view say:
category.subcategories.count
it blows saying objectcontext disposed.
if you're following concept repositories exist once per aggregate, you'll (likely) load tree way you'll know use it, in repository. example, you'd call db.categories.include("subcategories") eager-load them. might have different methods "getcategoriesdeep()" , "getcategoriesshallow()" differentiate if have different scenarios.
to answer deeper question, context should last single transaction (however define in app). in case, above, seems correct boundary repository's method call. controller should decide whether user requesting collection w/ or w/o subcategories, , call right method on repo answer question. t view shouldn't have have access data context (even indirectly via lazy loading).
Comments
Post a Comment