Which layer should create DataContext?
- by Kevin
I have a problem to decide which layer in my system should create DataContext. I have read a book, saying that if do not pass the same DataContext object for all the
database updates, it will sometimes get an exception thrown from the DataContext. That's why i initially create new instance of DataContext in business layer, and pass it into data access layer. So that the same datacontext is used for all the updates. But this lead to one design problem, if i wanna change my DAL to Non-LinqToSQL in future, i need to re-write the code in business layer as well. Please give me some advice on this. Thanks.
Example code
'Business Layer
Public Sub SaveData(name As String)
Using ts AS New TransactionScope()
Using db As New MyDataContext()
DAL.Insert(db,name)
DAL.Insert(db,name)
End Using
ts.Complete()
End Using
End Sub
'Data Access Layer
Public Sub Insert(db as MyDataContext,name As string)
db.TableAInsert(name)
End Sub