Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. What is the correct location to inject objects while using dependency injection?

What is the correct location to inject objects while using dependency injection?

Scheduled Pinned Locked Moved Design and Architecture
questiondesignarchitecturedatabasebusiness
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SSEAR
    wrote on last edited by
    #1

    In my recent project, I am trying to implement the dependency injection. This project implements with three tire architecture. One layer is to access database, second one is to manage business logic and last one is for the UI. Suppose there is a class "Customer" in data layer which implements the "ICustomer" interface for necessary data operations related to the customer. In the business layer, there is another "Customer" class for implement the business logic for the customer entity. This class has an "ICustomer" type property to inject the customer data layer class. Now my question is, from where I should inject this data objects to business objects? To do this, I have following solutions. 1. Construct the business object and inject the data object from UI layer. But I feel it is ugly because I have to access data layer from UI layer to do this. 2. Create something like factory pattern from business layer to extract business objects with injected data objects. So factory pattern is the correct design pattern for this? If so, how can I implement it? Is there any other method to do this? Thanks, Thomas

    W M R 3 Replies Last reply
    0
    • S SSEAR

      In my recent project, I am trying to implement the dependency injection. This project implements with three tire architecture. One layer is to access database, second one is to manage business logic and last one is for the UI. Suppose there is a class "Customer" in data layer which implements the "ICustomer" interface for necessary data operations related to the customer. In the business layer, there is another "Customer" class for implement the business logic for the customer entity. This class has an "ICustomer" type property to inject the customer data layer class. Now my question is, from where I should inject this data objects to business objects? To do this, I have following solutions. 1. Construct the business object and inject the data object from UI layer. But I feel it is ugly because I have to access data layer from UI layer to do this. 2. Create something like factory pattern from business layer to extract business objects with injected data objects. So factory pattern is the correct design pattern for this? If so, how can I implement it? Is there any other method to do this? Thanks, Thomas

      W Offline
      W Offline
      walterhevedeich
      wrote on last edited by
      #2

      SSEAR wrote:

      1. Construct the business object and inject the data object from UI layer. But I feel it is ugly because I have to access data layer from UI layer to do this.

      Not entirely true. DAL is supposed to be an independent layer so you need another tier(Object Mapping Tier) to make the data transfer from BLL to DAL. See here[^] for the details and the flow of data between the tiers.

      Signature construction in progress. Sorry for the inconvenience.

      S 1 Reply Last reply
      0
      • W walterhevedeich

        SSEAR wrote:

        1. Construct the business object and inject the data object from UI layer. But I feel it is ugly because I have to access data layer from UI layer to do this.

        Not entirely true. DAL is supposed to be an independent layer so you need another tier(Object Mapping Tier) to make the data transfer from BLL to DAL. See here[^] for the details and the flow of data between the tiers.

        Signature construction in progress. Sorry for the inconvenience.

        S Offline
        S Offline
        SSEAR
        wrote on last edited by
        #3

        Thanks for your reply. All the day I was trying to find a solution on google. I concluded with the solution to use a service locator. By the way, several people mentioned that service locator is an anti-pattern. I think it is true in some sense. What do you think about it? Thanks, Thomas

        1 Reply Last reply
        0
        • S SSEAR

          In my recent project, I am trying to implement the dependency injection. This project implements with three tire architecture. One layer is to access database, second one is to manage business logic and last one is for the UI. Suppose there is a class "Customer" in data layer which implements the "ICustomer" interface for necessary data operations related to the customer. In the business layer, there is another "Customer" class for implement the business logic for the customer entity. This class has an "ICustomer" type property to inject the customer data layer class. Now my question is, from where I should inject this data objects to business objects? To do this, I have following solutions. 1. Construct the business object and inject the data object from UI layer. But I feel it is ugly because I have to access data layer from UI layer to do this. 2. Create something like factory pattern from business layer to extract business objects with injected data objects. So factory pattern is the correct design pattern for this? If so, how can I implement it? Is there any other method to do this? Thanks, Thomas

          M Offline
          M Offline
          Mahmud Hasan
          wrote on last edited by
          #4

          1. You should register all of your services and objects on bootstrap of your application. 2. You always should use factory pattern to instantiate your objects. 3. Service locator gives you another level of abstraction over your DI container, so you may change your container later time if needed.

          Mahmud Hasan Software Engineer from Bangladesh

          1 Reply Last reply
          0
          • S SSEAR

            In my recent project, I am trying to implement the dependency injection. This project implements with three tire architecture. One layer is to access database, second one is to manage business logic and last one is for the UI. Suppose there is a class "Customer" in data layer which implements the "ICustomer" interface for necessary data operations related to the customer. In the business layer, there is another "Customer" class for implement the business logic for the customer entity. This class has an "ICustomer" type property to inject the customer data layer class. Now my question is, from where I should inject this data objects to business objects? To do this, I have following solutions. 1. Construct the business object and inject the data object from UI layer. But I feel it is ugly because I have to access data layer from UI layer to do this. 2. Create something like factory pattern from business layer to extract business objects with injected data objects. So factory pattern is the correct design pattern for this? If so, how can I implement it? Is there any other method to do this? Thanks, Thomas

            R Offline
            R Offline
            RichardGrimmer
            wrote on last edited by
            #5

            You could make your life a HELL of a lot simpler by not attempting to reinvent the wheel - there are loads of DI containers around (Castle Windsor, Unity et al) - take a look and you will probably find it easier (and will definately find it quicker) to use...

            C# has already designed away most of the tedium of C++.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups