问题
I have a SocketManager
class that contains a Socket
and other fields. All fields except the Socket
can be injected during the composition of the object graph with a DI framework. My idea was to simply build the entire object graph upfront by leaving Socket
empty and set it during runtime. This would allow me to complete the SocketManager
instantiation at one point in the code and use that instance throughout my entire program (as it was already set as an dependency through the DI framework)? Is that the standard way to "inject" runtime dependencies or is it bad practice?
A abstract factory seems to be a bad idea for two reasons: a) it creates a different object everytime b) It requires the runtime parameters at every place where I want to create the object
Let me illustrate my problem:
SocketManager class:
public class SocketManager {
//i'll only receive the socket at runtime
Socket socket;
//this object is available at compile-time and can be injected through the DI container
InjectableObject obj;
}
Somewhere in my code [CodePosition1] I will receive the socket like this :
public class SocketCreator{
SocketManager socketManager; //will be injected through DI container at startup
Socket socket = this.serverSocket.accept();
// at this point the socket manager is fully initialized
socketManager.setSocket(socket);
}
At numerous other places [CodePosition2] I can now use the SocketManager dependency
public class RandomClass {
//injected at compile-time through DI container, but only usable after [CodePosition1]
// was executed
SocketManager socketManager;
...
socketManager.getSocket().doSth()
...
}
The problem is that SocketManager
is not fully initialized, until [CodePosition1] at runtime, so I don't know any other way than using a init() or setter on SocketManager to "complete" the initialization of the SocketManager. This is however a leaky abstraction, as explained in this post: Is there a pattern for initializing objects created via a DI container
回答1:
It's best to compose complete object graphs right from the start. Injection of null
values should be prevented, because it complicates the consuming class.
In your case, however, it seems like the Socket
is not a 'real' component, but rather runtime data. As described here, you should prevent injecting runtime data into the object graph during construction.
That article gives two solutions to work around that problem, but there are more solutions. Abstract factories are, however, typically not a good solution, as you already alluded, and this blog post describes in more general sense what the problem is with Abstract Factories. Chapter 6.2 of this book even goes into more details about the problem with Abstract Factories from a DI point of view.
A solution given in the blog post is the use of a 'context' abstraction. In your case, for instance, a SocketContext
interface that allows you to get the Socket
runtime value by a consumer, once that consumer's methods are called, and thus after the consumer's object graph is constructed. For instance:
public interface SocketContext
{
Socket get_CurrentSocket();
}
Another option is to use a Proxy class that either hides the real Socket
or the real SocketManager
(depending on which level you can place the Proxy). This allows a consumer to be unaware that some piece of runtime data needs to be initialized under the covers and that it might be done lazily once the first call is made. For instance:
public class SocketManagerLazyProxy : SocketManager
{
private SocketManager mananger;
public void DoSomething()
{
if (manager == null) manager = new RealSocketManager(new Socket());
manager.DoSomething();
}
}
Another option is to set the Socket
value using Property Injection after the object graph is built. This allows you to construct the object graph much earlier in time, and set the runtime value once a request comes in, by setting it when a request comes in:
void HandleRequest(RequestData data)
{
SocketManager manager = GetSocketManagerForThisRequest();
manager.Socket = new Socket();
Handler handler = GetHandler(data.Name);
handler.Handle(data);
}
来源:https://stackoverflow.com/questions/52507754/is-it-a-bad-practice-to-set-dependencies-to-null-in-a-ioc-container-and-supply-t