使用一个DataContext静态变量 [英] Using a DataContext static variable

查看:159
本文介绍了使用一个DataContext静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用LINQ2SQL最近inheited一个ASP.Net应用程序。目前,它有它的DataContext对象宣布每一页为静态,我创建它们第一次我觉得他们是空(单身,排序的)。

I have recently inheited a ASP.Net app using Linq2SQL. Currently It has its DataContext objects declared as static in every page, and i create them the first time i find they are null (singleton, sort of).

我要评论,如果这是好还是坏。在情况下,当我只需要从数据库中,并在我需要编写以及情况阅读。

I need comments if this is good or bad. In situations when I only need to read from the DB and in situations where i need to write as well.

如何有整个应用程序只有一个DataContext的实例?

How about having just one DataContext instance for the entire application?

推荐答案

每一个应用程序的DataContext会表现不好,我害怕。所以即使使用一个作为页面的静态成员是一个坏主意,在DataContext不是线程安全的,对于初学者来说,。由于asgerhallas提到它是理想的使用环境工作的单位 - 通常是一个单一的请求。还有什么,你会开始发现你所有的数据在内存中并没有一个明确的刷新,你将不会看到更新。下面是跟这两个问题一对夫妇的帖子:身份地图与工作单位

One DataContext per application would perform badly, I'm afraid. The DataContext isn't thread safe, for starters, so even using one as a static member of a page is a bad idea. As asgerhallas mentioned it is ideal to use the context for a unit of work - typically a single request. Anything else and you'll start to find all of your data is in memory and you won't be seeing updates without an explicit refresh. Here are a couple posts that talk to those two subjects: Identity Maps and Units of Work

这篇关于使用一个DataContext静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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