I'm currently working on a .NET 4.7.1 application. We don't use Dependency Injection in our project.
I extracted some logic into a service class, which gets called on an HTTP request.
Currently I instantiate my service class like a regular private member.
I was just wondering if I should implement the Singleton pattern into my Service class to perhaps improve performance. Hence not to instantiate a new service instance on each HTTP request.
My code looks like this:
public class TheServer
{
private static readonly object locker = new object();
private TomatoService tomatoService => new TomatoService();
private void SendVegies()
{
lock(locker)
{
var result = tomatoService.CreateSalad();
// ... some send method call, that's why I need to lock the thread
}
}
}
public class TomatoService
{
public List<string> CreateSalad()
{
// some logic
// return someTomatoSalad;
}
}
Everything seems to work fine, I am just concerned about my code quality. Should I write my TomatoService as a Singleton class?
Would a Singleton TomatoService be better for the performance?
What do you think?
Thank you very much!
Aucun commentaire:
Enregistrer un commentaire