Revealing a null instance of an object as a property

Today I was looking for some code from Niki Kothari Facebook.NET API. I just wanted to test how he does things for ideas.

One of the things I had to deal with was real estate, which seemed to me the strangest.

check it:

FacebookRequest.cs defines a property and sets it in the constructor to a new instance of the custom class:

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

      

private field:

private user_parameters_Example_parameters_options;

and property:

    public FacebookRequestParameterList Parameters {
        get {
            return _parameters;
        }
    }

      

now FacebookRequestParameterList is actually a generic dictionary because it inherits and extends the dictionary:

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

      

Okay, so when you instantiate FacebookRequest, it automatically gets an auto-instantiated FacebookRequestParameterList class. So the property essentially returns an instance of FacebookRequestParameterList.

This is normal? I don't think I've seen that much. Seems to be sneaky to me or is this standard stuff here? It seemed like a strange way to do it. I am not posting this to bring it down, but understand that it is something standard or sneaky / evil.

I think it would be better to require developers to pass the FacebookRequestParameterList instance through the FacebookRequest constructor. And then work with that by setting the private field parameters after you initialize it through the constructor. Why do I think this is better? Because then the developers know exactly what's going on. They know the class expects a list of parameters ahead. And just exposing such an instance using the right one just seems strange to me.

Am I here?

0


source to share


5 answers


I don't think this is strange. This saves the client from having to have an instance FacebookRequestParameterList

, and the class is FacebookRequest

guaranteed to _parameters

contain a valid class instance FacebookRequestParameterList

and is not null

.

It's a matter of convenience for the client and the validity of the object for the class itself.



You can't see anything here, people, move.

(Edit: clarified a specific instance in the first paragraph)

+3


source


The use is absolutely correct. The class just uses Dictionary<string,string>

internally and provides this property. There, apparently, there is no need to pass in a pre-prepared dictionary to the constructor.



It would be better to imagine it as IDictionary<string,string>

instead of an instance of a concrete class.

+2


source


Have you ever used a DataTable? When you instantiate a new DataTable, you automatically get a collection of rows and columns. Does it make sense to instantiate the DataTable and then pass it to your collection of rows and columns? Obviously not. They are already there for you, and you can add material to them as you see fit.

(I just chose DataTable because I think it's easy to understand. I'm sure there are many other examples.)

0


source


I'm okay, not mean. Since the property of the collection that is being displayed is read-only (you cannot replace the entire collection at once), it reassures you that you have an instance to work with (unless the owner class does something funky with it).

I agree that this might be a better design, but I am not aware of the rest of the API, so I cannot comment on whether it is good or bad in this context. In general, although I would agree, the parameters should be passed (and copied to another dictionary, quite frankly, since the session object seems to have a lifetime of some kind).

0


source


I don't think there is anything wrong with that. You could be doing things like in a fbreq.Parameters["param"]="value"

very intuitive way, requiring a dictionary to be provided when building, it can be more confusing (or distracting) and you can't even now all the parameters before making the query.

0


source







All Articles