简体   繁体   English

Linq to SQL DataContext的用法

[英]Linq to SQL DataContext usage

I am creating an ASP .Net website and would like to use Linq to SQL class for my datalayer then have another project which would service the datalayer. 我正在创建一个ASP .Net网站,并想对我的数据层使用Linq to SQL类,然后有另一个项目可以为数据层提供服务。 Im fairly new with Linq to SQL. 我对Linq to SQL相当陌生。

The structure i have is an Interface which lists all the CRUD (Create, Read, Update and Delete) operations i need for each table in the database in a separate Interface. 我拥有的结构是一个接口,该接口在单独的接口中列出了数据库中每个表所需的所有CRUD(创建,读取,更新和删除)操作。

I then have another class which implements the Interface were i write all my code to get, update, delete data etc ie 然后,我还有另一个实现接口的类,即我编写所有代码以获取,更新,删除数据等,即

Public Function GetCustomers As Iqueryable(of Customer)
Dim Service as New StoreDataContext
Return From c in Service.Customers select c
End Function

What i noticed was i was always writing Dim Service As New StoreDataContext in all my methods in order to do anything with my data. 我注意到的是,我总是在所有方法中都将Dim Service As New StoreDataContext编写为对数据进行任何处理。

This then led me to think and create a property in each class which initialises the property with a datacontext. 然后,这导致我思考并在每个类中创建一个属性,并使用数据上下文初始化该属性。 To go one step better i thought to create a MustInherit class so all the classes would then inherit this class and any changes need to be made could be done at one stage rather than going into all the classes 为了更进一步,我想创建一个MustInherit类,以便所有类都可以继承该类,并且需要进行任何更改都可以在一个阶段完成,而不是进入所有类

Public MustInherit Class MyService

    Public ReadOnly Property CurrentDataContext As StoreDataContext
        Get
            Return New StoreDataContext
        End Get
    End Property

End Class

My customer class would look like 我的客户类别看起来像

Public Class CustomerSer
Inherits MyService
Implements ICustomer

Public Function GetCustomers As Iqueryable(of Customer)
Return From c in CurrentDataContext.Customers select c
End Function

As you can tell the function above is using CurrentDataContext which is being Inherited from the class created. 如您所见,上面的函数使用的是CurrentDataContext,它是从创建的类继承的。

Questions: 问题:

  1. Is this ok or would there be some flaw in this design? 这样可以吗,或者这种设计会有缺陷吗?
  2. Do i need to close this service at any stage or in my class? 我是否需要在任何阶段或在课堂上关闭此服务?

Thanks 谢谢

You should create a new data context each time because you want to be sure when Submit Changes occurs. 每次都应创建一个新的数据上下文,因为您想确定何时发生“提交更改”。 This follows a software engineering pattern called Unit Of Work , where you open a context, do a bunch of work to the tree of entities and then hit submit. 这遵循一种称为Unit Of Work的软件工程模式,您可以在其中打开上下文,对实体树进行大量工作,然后单击Submit。 Do that all in one method so that it is nice and clear for anyone in the future to understand what is going on. 用一种方法完成所有操作,以便将来任何人都可以清楚了解正在发生的事情。

If you move the create into a property, it is not as clear where the submit changes takes affect. 如果将创建内容移到属性中,则不清楚提交更改在何处生效。 You could have one method open it, another make some changes, a third then do Submit Changes and then another do some more changes that are not then submitted, which will be difficult to detect. 您可以打开一种方法,另一种进行一些更改,第三种然后进行提交更改,然后另一种进行更多然后未提交的更改,这将很难检测到。

I know that this looks like boilerplate, isn't DRY (Don't repeat yourself) but is still good programming practise. 我知道这看起来像样板,不是DRY(不要重复自己),但仍然是良好的编程习惯。

Here's a nice explanation in more detail about using Submit Changes by Scott Guthrie . 这是Scott Guthrie撰写的有关使用Submit Changes的更详细的很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM