c# - Linq to SQL DataContext usage -


i creating asp .net website , use linq sql class datalayer have project service datalayer. im new linq sql.

the structure have interface lists crud (create, read, update , delete) operations need each table in database in separate interface.

i have class implements interface write code get, update, delete data etc i.e.

public function getcustomers iqueryable(of customer) dim service new storedatacontext return c in service.customers select c end function 

what noticed was writing dim service new storedatacontext in methods in order data.

this led me think , create property in each class initialises property datacontext. go 1 step better thought create mustinherit class classes inherit class , changes need made done @ 1 stage rather going classes

public mustinherit class myservice      public readonly property currentdatacontext storedatacontext                     return new storedatacontext         end     end property  end class 

my customer class like

public class customerser inherits myservice implements icustomer  public function getcustomers iqueryable(of customer) return c in currentdatacontext.customers select c end function 

as can tell function above using currentdatacontext being inherited class created.

questions:

  1. is ok or there flaw in design?
  2. do need close service @ stage or in class?

thanks

you should create new data context each time because want sure when submit changes occurs. follows software engineering pattern called unit of work, open context, bunch of work tree of entities , hit submit. in 1 method nice , clear in future understand going on.

if move create property, not clear submit changes takes affect. have 1 method open it, make changes, third submit changes , more changes not submitted, difficult detect.

i know looks boilerplate, isn't dry (don't repeat yourself) still programming practise.

here's nice explanation in more detail using submit changes scott guthrie.


Comments