Calling Constructor

beantownace

Active member
Joined
Feb 15, 2019
Messages
26
Programming Experience
5-10
If I have a class that has a constructor that loads a static class I want to access anytime the service is running, how do I call that within a method I have? Essentially I have a class that has methods for loading up a customer object where I only need to set it once for each day then just access it from that point on. I just need to be able to initialize the Customer class in a method that will get called in my service once in the morning. Thanks maybe this is not the best approach but wanting to just set it once and then access it when needed. Thanks

Code Example:
namespace Framework
{
    public class Customer
    {   
        private readonly ICustomerRepository _customerRepository;

        public CustomerSetup(ICustomerRepository customerRepository)
        {
            Customer.CustomerSetup(GetCustomerGroup(), GetCustomerLocation(), GetCustomerActive());
        }

        private string GetCustomerGroup()
        {
        ....
        }

        private string GetCustomerLocation()
        {
        ....
        }

        private string GetCustomerActive()
        {
        ....
        }
    }
}

Then I have my static class different project:

namespace Common.Domain
{
    public static class Customer
    {
        public static string CustomerGroup { get; private set; }

        public static string CustomerLocation { get; private set; }

        public static bool CustomerActive { get; private set; }

        public static void CustomerSetup(string customerGroup, string customerLocation, bool customerActive)
        {
            CustomerGroup = customerGroup;
            CustomerLocation = customerLocation;
            CustomerActive = customerActive;
        }
    }
}
 
You need to take time to read the C# documentation regarding classes and constructors. The static constructor of a static class is only called once the first time anybody accesses that class. More details in the link below.


The unfortunate side effect of this is that it is once and only once. So if your program is running more than 24 hours, the constructor won't be called again tomorrow morning which is what you seem to want.

I recommend looking at either the factory or facade pattern. Within the method of that pattern, check the time. If you need to return a fresh object that create a new object and cache it. If it's not yet time to create a new object, return the cached version.
 
You can use something like this:
C#:
public class Thing
{
    private DateTime creationDate;

    private static Thing currentInstance;

    private Thing()
    {
        creationDate = DateTime.Today;
    }

    public static Thing GetInstance()
    {
        if (currentInstance == null || currentInstance.creationDate < DateTime.Today)
        {
            currentInstance = new Thing();
        }

        return currentInstance;
    }
}
Call GetThing every time you want an instance and it will return a different instance each day but the same instance all day.
 
You can use something like this:
C#:
public class Thing
{
    private DateTime creationDate;

    private static Thing currentInstance;

    private Thing()
    {
        creationDate = DateTime.Today;
    }

    public static Thing GetInstance()
    {
        if (currentInstance == null || currentInstance.creationDate < DateTime.Today)
        {
            currentInstance = new Thing();
        }

        return currentInstance;
    }
}
Call GetThing every time you want an instance and it will return a different instance each day but the same instance all day.
Can just simply create a method outside the constructor in the above where I have "Customer.CustomerSetup(GetCustomerGroup(), GetCustomerLocation(), GetCustomerActive());" instead of the constructor just have below it a public void LoadCustomerSetup { Customer.CustomerSetup(GetCustomerGroup(), GetCustomerLocation(), GetCustomerActive()); } and then call that on a start up method I have? In this case the static class will be loaded and I can just say Customer.CustomerGroup for example anywhere in the application. I am not instantiating the Customer object just accessing at that point.
 

Latest posts

Back
Top Bottom