I am a bit confused about what the Initialize
method is typically used for in a constructor.
Why can't I just put everything in the constructor and why does the sample below call the initialize method?
private IAzureTable<Product> _productRepository;
public ProductService(string dataSourceID)
{
Initialize(dataSourceID);
}
private void Initialize(string dataSourceID)
{
this._productRepository = StorageHelper.GetTable<Product>(dataSourceID);
}
Is there a convention that is normally used?
In this example do I need the word this
in the Initialize
method?
So in most cases an Initialize method shouldn't be required. In cases where initialization involves more than putting the object into a usable state (e.g., when heavy work needs to be performed or virtual members or external need to be called), then an Initialize method is a good idea. Well put.
Sometimes you have something more complicated though: Sometimes you will want a separate Initialize method because you want to call it at a separate time from construction. Sometimes you want one because you are writing multiple constructors, and you want to share some of the implementation between them.
A constructor should only initialize an object, not perform heavy work. A constructor should not directly or indirectly call virtual members or external code. So in most cases an Initialize method shouldn't be required.
We could create another initialize method ( Java lets us create several methods with identical names) to let the programmer do this. Here's how our class would look:"
Why can't I just put everything in the constructor and why does the sample below call the initialize method?
You can put it all in the constructor. In this simple case, you should. Constructors are for initializing your object.
Sometimes you have something more complicated though:
Initialize
method because you want to call it at a separate time from construction.None of these are the case for this code, so I'd just skip it and throw the code in the constructor.
Is there a convention that is normally used?
No. People do whatever is the easiest to read and understand, whatever requires the least extra code to be written, and whatever causes the least duplication of code.
However, if you're making the Initialize
method public, and not calling it from the constructor, I highly recommend you call it Initialize
. Design it to only be called once.
In this example do I need the word "this." in the Initialize method?
No. You never need to use this
to access class members unless you have another local variable with the same name. Here's such a case:
public class Something
{
private string someValue;
public class Something(string someValue)
{
// must use "this" to access the member variable,
// because a local variable has the same name
this.someValue = someValue;
}
}
What you have posted is a private helper method, not a class.
It is simply used so the constructor is not cluttered with initialization code and in order to give a good name for what is done.
In your specific example it appears to have little value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With