简体   繁体   English

将空对象实例公开为属性

[英]Exposing a null Object Instance as a Property

Today I was looking at some code atually from Nikhil Kothari's Facebook.NET API. 今天,我实际上正在从Nikhil Kothari的Facebook.NET API中查看一些代码。 I just wanted to check out how he was doing some things in there for ideas. 我只是想看看他是如何在那做一些事情的,以供参考。

One of the things I came across was a Property that just seemed really weird to me. 我遇到的一件事是对我来说似乎真的很奇怪的财产。

check this out: 看一下这个:

FacebookRequest.cs defines a property and sets it in the constructor to a new instance of a custom Class: FacebookRequest.cs定义一个属性,并将其在构造函​​数中设置为自定义Class的新实例:

    public FacebookRequest(FacebookSession session) {
        ....
        _parameters = new FacebookRequestParameterList();
    }

private field: 私人领域:

private FacebookRequestParameterList _parameters; 私人FacebookRequestParameterList _parameters;

and the property: 和属性:

    public FacebookRequestParameterList Parameters {
        get {
            return _parameters;
        }
    }

now the FacebookRequestParameterList is actually a Generic Dictionary because it inherits & extends Dictionary: 现在,FacebookRequestParameterList实际上是通用词典,因为它继承并扩展了Dictionary:

public sealed class FacebookRequestParameterList : Dictionary<string, string> {
...
}

Ok so essentially when you instantiate FacebookRequest, it therefore automatically comes with an auto-instantiated instance of the FacebookRequestParameterList class. 好的,基本上,当您实例化FacebookRequest时,它因此自动带有FacebookRequestParameterList类的自动实例化实例。 So the Property is essentially returning an instance of FacebookRequestParameterList. 因此,该属性本质上将返回FacebookRequestParameterList的实例。

Is that normal? 那是正常的吗? I don't think I've seen that a lot. 我认为我没有看到太多。 Seems sneaky to me or is this standard stuff here? 对我来说似乎是偷偷摸摸的,还是这是标准的东西? It just seemed like an odd way to do it. 这似乎是一种奇怪的方式。 I'm not posting this to bring him down but to understand if this is something standard or sneaky/evil. 我不是要发布此消息来使他失望,而是要了解这是标准行为还是偷偷摸摸的/邪恶的。

It seems to me it would be better to require developers to pass in an instance of FacebookRequestParameterList through the constructor of FacebookRequest instead. 在我看来,最好是要求开发人员改为通过FacebookRequest的构造函数传递FacebookRequestParameterList的实例。 And then work with that by setting the private field _parameters after you initialize it through the constructor. 然后,通过构造函数对其进行初始化后,通过设置私有字段_parameters来进行处理。 Why do I think this is better? 为什么我认为这更好? Because then developers know exactly what's going on. 因为这样开发人员才能确切知道发生了什么。 They know that the class expects a parameter list up front. 他们知道该类需要一个参数列表。 And just exposing an instance like that through a properly to me just seems odd. 只是通过适当的方式向我公开这样的实例,这似乎很奇怪。

Am I off base here? 我在这里基地吗?

I don't think it's odd at all. 我不觉得这很奇怪。 It saves the client the trouble of having to instantiate the FacebookRequestParameterList instance, and the FacebookRequest class is guaranteed that _parameters contains a valid instance of the FacebookRequestParameterList class, and isn't null . 它免除了客户端必须实例化FacebookRequestParameterList实例的麻烦,并且确保FacebookRequest类确保_parameters包含FacebookRequestParameterList类的有效实例,并且不为null

It's a matter of convenience for the client, and object validity for the class itself. 对于客户端来说,这是一个方便的问题,对于类本身,则是对象有效性。

Nothing to see here, people, move along. 人们在这里什么也看不到。

(Edit: clarified the specific instance in the first paragraph) (编辑:在第一段中阐明了具体实例)

The usage is perfectly valid. 该用法是完全有效的。 The class simply uses a Dictionary<string,string> internally and exposes that as a property. 该类仅在内部使用Dictionary<string,string>并将其公开为属性。 There's presumably no need to pass in a pre-baked dictionary to the constructor. 大概不需要将预烘焙的字典传递给构造函数。

What may be better would be to expose it as IDictionary<string,string> instead of an instance of a concrete class. 最好将其公开为IDictionary<string,string>而不是具体类的实例。

Have you ever used a DataTable? 您曾经使用过DataTable吗? When you instantiate a new DataTable, you automatically have a collection of rows and columns. 实例化新的DataTable时,将自动具有行和列的集合。 Would it make sense to have to instantiate a DataTable and then pass it in your own collection of rows and columns? 必须实例化一个DataTable,然后将其传递到您自己的行和列集合中,是否有意义? Obviously not. 显然不是。 They're already there for you, and you can add stuff to them as you see fit. 它们已经为您服务,您可以根据需要向其中添加内容。

(I just picked a DataTable because I think that makes it easy to understand. I'm sure there are many other examples out there.) (我之所以选择一个DataTable是因为我认为这很容易理解。我敢肯定还有很多其他示例。)

I it is normal, and not sneaky at all. 我是正常的,一点也不偷偷摸摸。 Since the collection property that is exposed is read only (you can't replace the whole collection at once), it assures you that you have an instance to work with (unless the owning class does something funky with it). 由于公开的collection属性是只读的(您不能一次替换整个collection),因此可以确保您可以使用一个实例(除非拥有类对其进行了一些时髦处理)。

I do agree though that it might be a better design, but I don't know about the rest of the API, so I can't comment if it is good or bad in that context . 我确实同意这可能是一个更好的设计,但是我不了解API的其余部分,因此我无法评论它在这种情况下是好是坏。 Generally though, I'd agree, the parameters should be passed in (and copied to another dictionary quite frankly, since the session object seems to have a lifetime of some sort). 通常,虽然我同意,但应该传入参数(并且坦率地说,将其复制到另一个字典中,因为会话对象似乎具有某种生存期)。

I don't think there is anything wrong with it. 我认为这没有任何问题。 You could then do stuff like fbreq.Parameters["param"]="value" in a very intuitive way, while requiring the dictionary to be provided on construction can be more confusing (or distracting) and you may not even now all the parameters before the request is created. 然后,您可以以非常直观的方式执行fbreq.Parameters["param"]="value" ,而要求在构造时提供字典可能会更加混乱(或分散注意力),并且您甚至可能现在还无法掌握所有参数在创建请求之前。

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

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