Today I was looking at some code atually from Nikhil Kothari's 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:
public FacebookRequest(FacebookSession session) {
....
_parameters = new FacebookRequestParameterList();
}
private field:
private FacebookRequestParameterList _parameters;
and the property:
public FacebookRequestParameterList Parameters {
get {
return _parameters;
}
}
now the FacebookRequestParameterList is actually a Generic Dictionary because it inherits & extends 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. So the Property is essentially returning an instance of 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. 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 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?