作为“ref”参数传递DataContext对象的任何缺点? [英] Any downsides to passing around a DataContext object as a 'ref' parameter?

查看:156
本文介绍了作为“ref”参数传递DataContext对象的任何缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在我的EF4项目中,我打开了DataContext文件本身的部分类,以及DataContext生成的几个Table / Object。但是,如果我打开一个产品类作为部分,就没有(据我所知)从产品的直接链接到产生它的DataContext类。

So in my EF4 project I have opened up the partial classes of both the DataContext file itself, as well as a couple of the Table/Object generated by the DataContext. However, if I open up a "Products" class as a partial, there isn't (as far as I can tell) a direct link from product back up to the DataContext class that spawned it.

public partial class Product 
{
    public DataContext GetContext() 
    {
        return this.DataContext; 
        // FAILS!!! No connection from 'this' to DataContext
        // makes sense because "Product" isn't REALLY derived from DataContext
        //...but still, I want this to work!
    }
}

但是在部分产品类里面,我肯定会喜欢能够直接查询数据库,我真的很想初始化DataContext的一个实例,并将其用于我的aspx.cs页面查询,以及从aspx调用的部分类中执行的查询。 cs页面。

But inside by partial product class, I sure would like to be able to query the database directly, and I really like to be able to initialize just one instance of DataContext and use it for my aspx.cs page queries, as well as queries executed from the partial classes that are called from the aspx.cs page.

所以我的解决方案到目前为止是将DataContext的实例作为一个'ref'参数传递给我的部分类的方法,需要戳数据库。这是部分类:

So my solution so far is to pass in the instance of DataContext as a 'ref' parameter to the methods of my partial class that need to poke around the database. Here's the partial class:

public partial class Complaint
{
    public IEnumerable<Person> GetPByRole(InvestigationRole roleEnum, ref DataContext dbase)
    {
        var role = dbase.GetRole(roleEnum);
        return this.PeopleOnInvestigations
                   .Where(x => x.InvestigationRoleID == 1)
                   .Select(x => x.Person);
     }
}

所以有一个缺点我的DataContext对象作为ref参数,需要通过此连接访问数据库的任何部分类方法?其中一个优点是,一旦作为参考传递,我可以将AddObject()新实体它们在这些部分类中,一旦我的SaveChanges调用回到我的asp.cs页面,所有的更改(从aspx和部分类方法)都被执行。

So is there a downside to passing around my DataContext object as a ref parameter to any partial class methods that need access to the database through this connection? One of the upsides is that once its passed in as a ref, I can "AddObject()" new entities to it from within these partial classes, and once my SaveChanges call back on my asp.cs page is made, ALL the changes (from the aspx and from the partial class methods) get executed.

推荐答案

首先,传递一个 ref 变量用于能够更改保存引用的变量。但是,由于您不在 GetPByRole 方法中更改 DataContext dbase 引用,将其作为 ref 是无用的,甚至会混淆其他开发者。也许你误解价值类型和参考类型。引用类型(例如 DataContext )始终通过引用传递,通过方法调用传递它不会使对象本身的新副本,仅引用的副本(这是一个32或64位值)。

First of all, passing a ref variable is used to be able to change variable that holds the reference. But since you are not changing the DataContext dbase reference in your GetPByRole method, passing it as a ref is useless and would even confuse other developers. Perhaps you misunderstand value types and reference types. Reference types (such as DataContext) are always passed by reference, passing it around through method calls will not make new copies of the object itself, merely copies of the reference (which is a 32 or 64 bits value).

其次,你在这里混合责任。您的产品类是一个实体,但您似乎在其上实现各种数据检索方法。这很快就会变成一个大混乱。为您的系统中的每个课程承担一个责任。 Person 类的职责是成为一个人。

Second, you are mixing responsibilities here. Your Product class is an entity, but you seem to implementing all kind of data retrieval methods on it. This will become a big mess very soon. Give every class in your system a single responsibility. The responsibility of the Person class is to be a person.

换句话说,你正在尝试的更适合于存储库类(或者甚至是服务类)。例如,创建一个包含这些方法的 PersonRepository PersonRepository 将能够返回新的 Person 实例(实际上,一个存储库应该是数据源之间的接口和您的应用程序,通常不会实现业务相关的查询方法)。这样,您可以保持您的实体免于了解数据上下文(这是ADO.NET团队在开发实体框架时非常刻意的设计决策)。

In other words, what you are trying to to is much more suited for repository classes (or even service classes around that). For instance, create a PersonRepository that holds these methods. The PersonRepository will be able to return new Person instances (in fact a repository should just be the interface between your data source and your application and would normally not implement business related query methods). This way you keep your entities free from knowing the data context (which is a very deliberate design decision of the ADO.NET team while developing Entity Framework).

这篇关于作为“ref”参数传递DataContext对象的任何缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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