Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Three Ways to Implement Dependency Injection in .NET Applications : Page 3

Replace dependencies with Dependency Injection pattern code to make your classes easier to test and reuse.


advertisement
Implementing Setter Injection
Setter injection uses properties to inject the dependencies, which lets you create and use resources as late as possible. It's more flexible than constructor injection because you can use it to change the dependency of one object on another without having to create a new instance of the class or making any changes to its constructor. Further, the setters can have meaningful, self-descriptive names that simplify understanding and using them. Here's an example that adds a property to the BusinessFacade class which you can use to inject the dependency.

The following is now our BusinessFacade class with the said property.

public class BusinessFacade { private IBusinessLogic businessLogic; public IBusinessLogic BusinessLogic { get { return businessLogic; } set { businessLogic = value; } } }

The following code snippet illustrates to implement setter injection using the BusinessFacade class shown above.

IBusinessLogic productBL = new ProductBL(); BusinessFacade businessFacade = new BusinessFacade(); businessFacade.BusinessLogic = productBL;

The preceding code snippet uses the BusinessLogic property of the BusinessFacade class to set its dependency on the BusinessLogic type.The primary advantage of this design is that you can change the dependency between the BusinessFacade and the instance of BusinessLogic even after instantiating the BusinessFacade class.

Even though setter injection is a good choice, its primary drawback is that an object with setters cannot be immutable—and it can be difficult to identify which dependencies are needed, and when. You should normally choose constructor injection over setter injection unless you need to change the dependency after instantiating an object instance, or cannot change constructors and recompile.

Implementing Interface Injection
You accomplish the last type of dependency injection technique, interface injection, by using a common interface that other classes need to implement to inject dependencies. The following code shows an example in which the classes use the IBusinessLogic interface as a base contract to inject an instance of any of the business logic classes (ProductBL or CustomerBL) into the BusinessFacade class. Both the business logic classes ProductBL and CustomerBL implement the IBusinessLogic interface:



interface IBusinessLogic { //Some code } class ProductBL : IBusinessLogic { //Some code } class CustomerBL : IBusinessLogic { //Some code } class BusinessFacade : IBusinessFacade { private IBusinessLogic businessLogic; public void SetBLObject(IBusinessLogic businessLogic) { this.businessLogic = businessLogic; } }

In the code snippet above, the SetBLObject method of the BusinessFacade class accepts a parameter of type IBusinessLogic. The following code shows how you'd call the SetBLObject() method to inject a dependency for either type of BusinessLogic class:

IBusinessLogic businessLogic = new ProductBL(); BusinessFacade businessFacade = new BusinessFacade(); businessFacade.SetBLObject(businessLogic);

Or:

IBusinessLogic businessLogic = new CustomerBL(); BusinessFacade businessFacade = new BusinessFacade(); businessFacade.SetBLObject(businessLogic);

All three forms of dependency injection discussed in this article passed a reference to a BusinssLogic type rather than an instance of the type by using interfaces. According to Jeremy Weiskotten, a senior software engineer for Kronos:

"Coding to well-defined interfaces, particularly when using the dependency injection pattern, is the key to achieving loose coupling. By coupling an object to an interface instead of a specific implementation, you have the ability to use any implementation with minimal change and risk."
Dependency Injection can reduce the coupling between software components and it promises to become the paradigm of choice for designing loosely coupled, maintainable and testable objects. It can be used to abstract the dependencies of an object outside of it and make such objects loosely coupled with each other.



Joydip Kanjilal has over 10 years of industry experience with C, C++, Java, C#, VB, VC++, ASP.Net, XML, Design Patterns, UML, etc. He currently works as a senior project leader in a reputable multinational company in Hyderabad, India, and has contributed articles on .NET and related technologies to www.aspalliance.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap