SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about Dependency Injection

    I'm considering using PicoContainer for an upcoming project, but have a more general question about Dependency Injection. I've never implemented IoC/DI in any way in the past so I'm a bit shady on how it all works.

    I understand that a container like PicoContainer provides the injection of dependent objects for a class. However what I'm not getting is how DI works in the event that the dependency needs to be a specific instance of an object.

    So lets say I have a customer class and a budget class. A dependency of the customer class is an Instance of a budget class. So if I create an instance of the customer class, PicoContainer gives me a new instance with an instance of the budget class injected. Cool - great. So this new "budget" will be populated with data by the end user and saved to the database. All is fine.

    Now, lets say I want to retrieve that customer again and view their budget. If I create an instance of the Customer class and retrieve the customer, PicoContainer will provide me with an instance of a budget class but it will not be the same instance that was saved to the database before.

    I'm guessing that I need to write a method on the budget class called "getBudget()" and pass in the identifier. As long as the Customer class is aware of the budget interface, I am still decoupled and can "load" the instance of the budget into the budget class itself from the database.

    Is this how it works?

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,629
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I don't have much seat time with DI, but I think you are missing part of the point--you inject the concrete types of IBusiness and ICustomer, but the application still is responsible for loading the correct instance of the concrete IBusiness and ICustomer.

    The way I would do it is I would have Business and Customer classes, and use the DI/IoC framework to inject the right sort of Business or Customer Repositories.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    I don't have much seat time with DI, but I think you are missing part of the point--you inject the concrete types of IBusiness and ICustomer, but the application still is responsible for loading the correct instance of the concrete IBusiness and ICustomer.

    The way I would do it is I would have Business and Customer classes, and use the DI/IoC framework to inject the right sort of Business or Customer Repositories.
    I understand what your saying but PicoContainer is just a container, not a framework. Maybe I have to read the documentation to see if this clears it up, but I don't see how the container which populates the dependencies can know which instance of Budget belongs to Customer unless the key is defined in the container.

    EDIT: I believe my assumption is correct. Found this on the pico container web site:

    "PicoContainer's most important feature is its ability to instantiate arbitrary objects. This is done through its API, which is similar to a hash table. You can put java.lang.Class objects in and get object instances back."

    The key here is "arbitrary" - to me that means standalone empty dependent objects.
    Last edited by progrmr; May 19, 2009 at 12:57. Reason: additional info

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A DI container is mostly useful for injecting objects with a longer lifetime. Transient objects, such as value objects and entities, are usually not managed with a container. A container should manage the objects that you might otherwise be tempted to make globals/singletons.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •