在linq中创建懒惰是正确的吗? [英] create lazy in linq is correct?

查看:78
本文介绍了在linq中创建懒惰是正确的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家,
我有一个大问题,请帮我,我在asp.net中定义了一个用户类,它有几种方法,用于与数据库连接,我使用linq,在每种方法中我都必须使用:

hi experts,
i have a big question plz help me,i defined a user class in asp.net that it has several method ,for connect with database i use linq,in every method i have to use:

TestDatacontext db=new TestDatacontex();
...


在该课程的第一部分,我定义了:


at the first of this class i defined:

private static MyDataClassesDataContext _db;
        public static MyDataClassesDataContext Db
        {
            get
            {
                if (_db == null)
                {
                    _db = new MyDataClassesDataContext();
                }
                return (_db);
            }
        }


在我从该变量使用的每个方法中,并且我没有在方法中定义


and in every method i used from this variable and i dont define

TestDatacontext db=new TestDatacontex();

,它正确还是我必须在方法中定义db?
预先感谢您的回答.

in methods ,its correct or i must define db in methods?
Thanks in advance for an answer.

推荐答案

joonjooni,

您已经为entityframework实现了单例设计模式:

http://www.dofactory.com/Patterns/PatternSingleton.aspx [
例如,这个人反对它:
http://www.britishdeveloper.co.uk/2011/03/dont-use-singleton-datacontexts-entity.html [ ^ ]

这个家伙就是这样的:
http://cgeers.com/2009/02/21/entity-framework-objectcontext/ [ ^ ]

在我看来,有一个使用DataContext的单例模式是一个好主意.

希望对您有所帮助.
Hi joonjooni,

You have implemented singleton design pattern for entityframework :

http://www.dofactory.com/Patterns/PatternSingleton.aspx[^]

Its a controversy subject that people should use singleton design pattern for EF or not. so try to google for "entity framework singleton pattern"

for example this guy is against it :
http://www.britishdeveloper.co.uk/2011/03/dont-use-singleton-datacontexts-entity.html[^]

And this guy is for it :
http://cgeers.com/2009/02/21/entity-framework-objectcontext/[^]

In my opinion its a good idea to have a singleton pattern for working with DataContext.

Hope it helps.


这篇关于在linq中创建懒惰是正确的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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