LinqToSql 声明和实例化 DataContext 最佳实践? [英] LinqToSql declare and instantiate DataContext best practice?

查看:28
本文介绍了LinqToSql 声明和实例化 DataContext 最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置 DataContext 以便在扩展的 LinqToSql 类中轻松访问的最佳实践是什么?

What's the best practice in terms of setting up my DataContext for easy access in my extended LinqToSql classes?

例如,我的 dbml 中有一个用户"实体,我想向该类添加方法,如下所示:

For example, I have a "User" entity in my dbml and I want to add methods to that class like so:

Partial Public Class User

    Public Function GetUser(ByVal UserID as Integer) as User
         'Do Work
    End Function

End Class

为了访问我的 DataContext,我必须像这样在方法中声明它:

In order to access my DataContext I'd have to declare it inside the method like so:

Partial Public Class User

    Public Function GetUser(ByVal UserID as Integer) as User
         Dim dc as New MyDataContext()
         Return (From u in dc.Users Where u.ID = UserID).Single()
    End Function

End Class

我不想对每一种方法都这样做.通常(如果我不扩展 LinqToSql dbml 类)我可以这样做:

I'd like to not have to do that for every single method. Normally (if I weren't extending the LinqToSql dbml classes) I could just do this:

Partial Public Class User
    Private dc as MyDataContext

    Public Sub New()
         dc = new MyDataContext()
    End Sub

    Public Function GetUser(ByVal UserID as Integer) as User
         Return (From u in dc.Users Where u.ID = UserID).Single()
    End Function

    Public Function GetAllUsers() as IEnumerable(Of User)
         Return From u in dc.Users
    End Function

    'etc...

End Class

这将允许我访问每个方法的数据上下文,而不必每次都重新声明它.但当然你不能这样做,因为 dbml 已经有一个构造函数.如果有任何变化,将代码添加到 dbml 总是会被覆盖.

This would allow me to access the datacontext for each method without having to declare it newly each time. But of course you can't do that because the dbml already has a constructor. And adding code into the dbml always gets overwritten if anything ever changes.

有人对如何在这里节省一些多余的代码有什么好主意吗?

Anyone have any good ideas on how to save myself some excess code here?

TIA!

推荐答案

首先,请确保在完成后处理 DataContext!他可以是一个笨重的小混蛋(编辑不是为了实例化而沉重,而是如果你继续使用它而不处理它就很沉重);您不希望旧的 DataContexts 在内存中闲逛.

First, make sure you are disposing your DataContext when you're done! He can be a heavy little bastard (edit not heavy to instantiate, but heavy to keep around if you keep using it without disposing); you don't want old DataContexts hanging around in memory.

其次,DataContext 旨在表示单个逻辑事务.例如.每次要开始新事务时都应该创建一个新事务,并在该事务完成时删除它..所以就您的目的而言,这可能GetUser 方法的范围.如果您有一系列需要作为一个组进行的 DB 调用,则在摆脱它之前,它们都应该使用相同的 DC.

Second, the DataContext is intended to represent a single logical transaction. E.g. you should create a new one every time you want to start a new transaction, and get rid of it when that transaction is complete. So for your purposes, that is probably the scope of the GetUser method. If you have a series of DB calls that need to be made as a group, they should all use the same DC before getting rid of it.

这篇关于LinqToSql 声明和实例化 DataContext 最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆