本文介绍了这层应该创建的DataContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须决定哪些层在我的系统应该创建的DataContext的一个问题。我看过一本书,说如果不通过同一个DataContext对象为所有
数据库的更新,它有时会获得在DataContext抛出的异常。这就是为什么我最初创建在业务层的DataContext的新实例,并将其传递到数据访问层。使相同的datacontext用于所有的更新。但是,这导致了一个设计问题,如果我想改变我的DAL到非LinqToSQL未来,我需要重新写在业务层的code为好。请给我一些这方面的建议。谢谢你。

举例code

 业务层
公用Sub保存数据(名称作为字符串)
采用TS作为新的TransactionScope()
使用DB作为新MyDataContext()
    DAL.Insert(DB,名)
    DAL.Insert(DB,名)
使用完
ts.Complete()
使用完
结束小组数据访问层
公用Sub插入(DB作为MyDataContext,名称作为字符串)
    db.TableAInsert(名)
结束小组


解决方案

作为DataContext的紧密耦合的LINQ to SQL,你一定要在DAL创建它。

有一个在我这给出了答案somewhat相关SO质疑。它带来了是否在DataContext(ObjectContext的实体框架)应在多个数据库操作保持活着,还是应该创建和设置临时的同样的问题。

你指的问题是涉及到了 模型,通过一个DataContext检索对象只能使用同一DataContext的实例来更新 。现在,如果原来的DataContext的已被释放,任何试图更新模型对象附加到一个新的DataContext实例将失败。

然而,这不一定是一个问题,根据您的应用程序的体系结构。
例如,FF模型对象中检索,并通过操纵LINQ到SQL正在连载来回​​客户端之间(如在Web应用程序或网络服务)大于更新后的对象将永远相同最初检索的那些。在这种情况下,它们可以安全地连接到一个新的DataContext

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 thedatabase 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
解决方案

Being the DataContext tightly coupled to LINQ to SQL, you should definitely create it in the DAL.

Have a look at the answer I provided to this somewhat related SO question. It brings up the same problem of whether the DataContext (ObjectContext in Entity Framework) should be kept alive across multiple database operations, or it should be created and disposed of ad-hoc.

The problems you are referring to are related to the fact that model objects retrieved through a DataContext can only be updated using the same DataContext instance. Now, if the original DataContext has been disposed, any attempt to attach an updated model object to a new DataContext instance will fail.

However this isn't necessarily an issue, depending on your application's architecture.
For example, ff the model objects retrieved and manipulated through LINQ to SQL are being serialized back and forth between a client (like in a web application or a web service) than the updated objects will never be the same as the ones that were originally retrieved. In that case they can safely be attached to a new DataContext.

这篇关于这层应该创建的DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:29