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?
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:
Initializemethod 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.
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
Initializemethod public, and not calling it from the constructor, I highly recommend you call itInitialize. Design it to only be called once.No. You never need to use
thisto access class members unless you have another local variable with the same name. Here’s such a case: