Why do people talk about DI frameworks "instantiating an object graph" rather than "instantiating objects"?
I'm reading a book about DI that always talks about DI frameworks "instantiating an o开发者_JAVA技巧bject graph". Why say it this way rather than "instantiating objects"?
A graph of objects consists of objects that hold references to each other. Another name for a graph in this context is a network.
An IOC system wouldn't be much use if it forgot to set up the references between the objects! It's the way the objects automatically get references to other objects they need that is the whole value of an IOC system.
The term "object graph" implies that you have a set of objects that will be created at the same time. Using this term can help distinguish between the Dependency Injection pattern (where you will usually only refer to the DI framework in a single place) and the Service Locator pattern (where you will use it to create/access objects in multiple places throughout your application).
A graph is a set of interconnected objects. Thus, its instantiation may represent the instantiation of several individual objects.
To distinguish the creation of a single object from the creation of a set of objects, we use the term "object graph". It helps to communicate the scope of the Resolve
operation.
精彩评论