Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Dependency Injection is a practice where objects are designed in a manner where they receive instances of the objects from other pieces of code, instead of constructing them internally. This means that any object implementing the interface which is required by the object can be substituted in without changing the code, which simplifies testing, and improves decoupling.</p> <p>For example, consider these clases:</p> <pre class="lang-csh prettyprint-override"><code>public class PersonService { public void addManager( Person employee, Person newManager ) { ... } public void removeManager( Person employee, Person oldManager ) { ... } public Group getGroupByManager( Person manager ) { ... } } public class GroupMembershipService() { public void addPersonToGroup( Person person, Group group ) { ... } public void removePersonFromGroup( Person person, Group group ) { ... } } </code></pre> <p>In this example, the implementation of <code>PersonService::addManager</code> and <code>PersonService::removeManager</code> would need an instance of the GroupMembershipService in order to do its work. Without Dependency Injection, the traditional way of doing this would be to instantiate a new <code>GroupMembershipService</code> in the constructor of <code>PersonService</code> and use that instance attribute in both functions. However, if the constructor of <code>GroupMembershipService</code> has multiple things it requires, or worse yet, there are some initialization "setters" that need to be called on the <code>GroupMembershipService</code>, the code grows rather quickly, and the <code>PersonService</code> now depends not only on the <code>GroupMembershipService</code> but also everything else that <code>GroupMembershipService</code> depends on. Furthermore, the linkage to <code>GroupMembershipService</code> is hardcoded into the <code>PersonService</code> which means that you can't "dummy up" a <code>GroupMembershipService</code> for testing purposes, or to use a strategy pattern in different parts of your application. </p> <p>With Dependency Injection, instead of instantiating the <code>GroupMembershipService</code> within your <code>PersonService</code>, you'd either pass it in to the <code>PersonService</code> constructor, or else add a Property (getter and setter) to set a local instance of it. This means that your <code>PersonService</code> no longer has to worry about how to create a <code>GroupMembershipService</code>, it just accepts the ones it's given, and works with them. This also means that anything which is a subclass of <code>GroupMembershipService</code>, or implements the <code>GroupMembershipService</code> interface can be "injected" into the <code>PersonService</code>, and the <code>PersonService</code> doesn't need to know about the change.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload