Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Utility functionality Static Method / Static Class / Singleton pattern

I am creating a utility method GetServiceTicketNumber() inside a utility class, since the method will be used often i do not want to instantiate each and every time so i made the method & _ticket as static.

UtilityManager contains several other methods too.

My question is :

1) Is this the right way to implement the functionality ?

2) whether to make UtilityManager also a static class/not?, what difference does it make ?

3) Whether the below code (for TicketProvider functionality) is written in singleton pattern ? (Considering most of the singleton class instantiate the same class UtilityManager.)

other info : class called in Asp.Net Application

public  sealed class UtilityManager
{    
    public static readonly TicketProvider _ticket = new TicketProvider();

    public static int GetServiceTicketNumber()
    {       
        return _ticket.GetTicket();
    }
}
like image 339
user758405 Avatar asked Jan 19 '23 16:01

user758405


2 Answers

1: sounds viable; often it is a subjective call; for example if your utility relies on static fields, this limits you to a single setup per AppDomain. This may be fine, but may be limiting if you later move to multi-tenancy. It also may be harder to test.

2: a static class cannot have instances (or instance methods); if the methods are all implemented as static then probably it should be a static class

3: I see no benefit in singleton over static here. A singleton is useful if you need to treat is as an instance, for example to implement an interface.

Another choice here might be regular instance, but just ensure all your code talks to he same instance - perhaps via IoC/DI (and perhaps not). This will give you similar convenience, but more flexibility for testing and multi-tenancy

As a side note, you may also want to consider the threading implications, especially in a web app (highly threaded). Shared data (including static fields and shared instances) should be correctly synchronised (or immutable).

like image 87
Marc Gravell Avatar answered Jan 22 '23 10:01

Marc Gravell


Utility methods are best declared static and many code checker tools like stylecop will actually recommend your utility functions to be static, so you're on the right track. If you want to have a singleton instance of the TicketProvider, you can use a static constructor to make sure that the field gets initialized before it is actually accessed and initialized only once. Also you can make the class static to indicate that this class is not designed to be instantiated but for utility use only. Below is my recommendation:

public static class UtilityManager
{  
    static UtilityManager()
    {
        Ticket = new TicketProvider();
    }

    public static TicketProvider Ticket { get; private set; }

    public static int GetServiceTicketNumber()
    {       
        return Ticket.GetTicket();
    }
}
like image 20
Teoman Soygul Avatar answered Jan 22 '23 11:01

Teoman Soygul