none
Is it okay to call private method in constructor and call class public method with object initializer? RRS feed

  • Question

  • In my class, i am creating a new token and calling service method which then delete a token. Instead of creating a new instance, generating new token and then calling service method, can i just create a new token in constructor and then call service method at the same time?

    All the methods in GCService class require token to perform some operation like get balance, verify customer and etc.

    I have read that it is legit to call non virtual methods in constructor. I have never used this approach before so not sure if its a normal practise and can this be improved?

    First approach

    Public Class Customer : Implements ICustomer
    {
       //creating a new instance , setting class properties in constructor and then calling class method.
    
    Dim result = New GCService(a,b).GetBalance(a);
    }
    
    public class GCService : IGCService
    {
    private string Token { get; set; }
    
    //constructor 
    public GCService(string _a, string _a)
    {
        //set some properties and get token
        Token = GetToken(_a, _b);
    }
    
    private string GetToken(string a, string b)
    {
        //get token and return response
        return response;
    }
    }

    Second Approach

    Public Class Customer : Implements ICustomer { //creating a new instance , setting class properties in constructor and then calling class method. Dim result = New GCService(a,b).GetBalance(a); } public class GCService : IGCService { private string Token { get; set; } //constructor public GCService(string _a, string _a) { //set some properties and get token }

    public BalanceService GetBalance(string _a)

    {

    GetToken();

    //call service method to get balance

    } private string GetToken(string a,string b){ //get token and return response

    return response; } }

    • Edited by momer79 Wednesday, June 20, 2018 7:49 PM
    Wednesday, June 20, 2018 7:40 PM

All replies

  • You don't have virtual methods there. Problem of virtual method is that constructor is called in lowest inheritance level and virtual method is top inheritance level. 

    https://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor

    Private methods cannot be virtual because there are not visible in inherited class. Public or protected could be virtual only. 

    So I think your code is right.

    Thursday, June 21, 2018 4:41 AM
  • Greetings Momer79.

    To answer your question, there's nothing wrong with calling class methods in the constructor. In fact, if the method in question must be called before anything else in the class will work, it makes sense to call it in the constructor so that you can't forget to call it later.

    As an aside, have you considered making the class GCService static? If you only ever create an instance to get a result as per your example, and you never save the instance, static methods would be more efficient because you wouldn't need the instance at all.

    Thursday, June 21, 2018 6:43 AM
  • I personally wouldn't recommend this route for a variety of reasons. The biggest reason is that in a typical architecture business objects shouldn't be creating services, it goes the other way. Secondly, the business object would need to accept all the parameters needed to create the service (a and b in your case). Thirdly you cannot change the underlying service implementation without modifying the business object with breaks the single responsibility principle. Of course your example is a little too generic to be able to tell what use case you have in mind.

    I would personally recommend that your service, if it is absolutely needed by the business object, be a parameter to the constructor. The upside is that you have moved the creation of the service outside the object. Whether you create it immediately before you need it, use a singleton or a DI container is completely up to the app - not the business object. The downside is that the service will now have a lifetime tied to an arbitrary business object which may or may not make sense. If you only really need the service for one specific call then forego using the service and make the call results a parameter to the object. In your case, for example, why not have the token be a parameter to the constructor?


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, June 21, 2018 1:57 PM
    Moderator