c# - Encapsulate context constructor on a query object -
it idea create class holds both query , context constructors func of context , func of iqueryable of tentity solving context lifetime problem?
example: on data layer, if use 1 context per method using "using" statement, can't return iqueryables because wouldn't valid after context has been released, makes use of:
- one context per form (this need injecting form context onto data layer)
- thread static context
- call tolist() before returning query (disable query composition)
as far recall, contexts designed created , recreated needed, not statically kept in memory (though have done way well). find fine create context in class (like controller class, or view model class, of mvc app), have methods use that.
however, if have large amount of static data want cache users, have kept statically in memory. you'd have consider threading, mentioned. if reading only, can create read-only locks access data (not c# lock{}
).
for more details if going static: https://msdn.microsoft.com/en-us/library/system.threading.readerwriterlock(v=vs.110).aspx
you don't need dispose of them in cases - , that's design https://stackoverflow.com/a/389871/1236397