What is the correct location to inject objects while using dependency injection?
-
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
-
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
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.
-
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.
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
-
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
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
-
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
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++.