More specifically, what is the best approach for classes in which state matters in an application that implements dependency injection.
Let's say I need access to an object that is in a certain state. For example, this object could be initiated in another thread or in a process in which I do not control.
A good example of such an object that already exists in .NET is the HttpContext.
In this case, Microsoft decided to go with a static approach, so I just say:
var currentObj = HttpContext.Current;
And that gives me a specific instance of the object, without worrying about where it came from.
The problem with the static approach is that it doesn't work very well with dependency injection.
Another option is to configure your specific class as Singleton in an IoC container. This means that you can enter it, and depending on the current configuration of the IoC container, this will be the correct instance of the class.
However, the downfall of this approach is that the value of the state that matters to the state is no longer explicit in the code, it is not obvious if you look at it. With the Static class used for access and instance, it is more clear that state is important. Maybe it doesn't matter though.
So, is there a pattern that helps me here?
Context:
In the context, I am working on an application that has many instances of a class that performs I / O. They exist in their flows.
( ) -, . ..
:
, , "stateful" . - :
- "state" - , , . , .
- , "stateful", . HttpContext, , . Current , .
- , DI, , . , , ?
- Singleton. , IoC.