It's a good idea to have a Singleton implementation with dependency injection? I have some classes that performs some heavy tasks on instantiation (related to database loading). I would like to use them always as new instances but is problematic due to performance. The code I am working with is from a legacy app, so it's preferible to dont touch it too much.
My question is: Can I create singletons as this example?
IMPORTANT: consider de dependency is kind of a logger, so i don´t have multiple implementations of it, only one. Is not a problem if all the instances shares the same dependency instantiation.
I am interested in using interfaces as they make my solution less coupled between projects.
public class HeavyTaskDb()
{
private static HeavyTaskDb instance;
private IDependency dependency;
private HeavyTaskDb(IDependency dependency)
{
this.dependency=dependency;
}
public static HeavyTaskDb Instance(IDependency dependency)
{
if(instance==null)
{
instance=new HeavyTaskDb (dependency);}
}
return instance;
}
}