Quantcast
Channel: How should I refactor a singleton (to be used by a container) when the refactored class requires initialization and takes a dependency? - Software Engineering Stack Exchange
Viewing all articles
Browse latest Browse all 4

How should I refactor a singleton (to be used by a container) when the refactored class requires initialization and takes a dependency?

$
0
0

I'm refactoring an old app to use dependency injection.n I'm pretty new to DI.

I have a class that used to be a singleton. I'm refactoring it into a non-singleton class and using the container to manage its lifetime (as a single instance). It has some initialization that used to be done in the constructor but it seems that this is generally frowned-on in the DI world: SRP says methods should just do one thing and so constructors are just for dependencies, I read somewhere.

If the class had no dependencies, I could just create the instance, call .Initialize() and register the instance with the container as a singleton. But the class has a dependency which I'd prefer to resolve using the container.

Am I going about this the wrong way? Am I missing some pattern?


Viewing all articles
Browse latest Browse all 4

Trending Articles