November 25, 2014
Hot Topics:

Dependency Injection with Spring.Net

  • January 22, 2008
  • By David Consdorf
  • Send Email »
  • More Articles »

Constructer Injection vs. Property Injection

In the last example, you used constructor injection to set the message property. Another way to set the message property is by property injection. Take the last example and add another mapping:

<object name="SimpleMessageByProperty"
        type="SimpleMessage, __Code"
        singleton="false">
   <property name="Message" value="Hello, I am a simple message
      set by using property-based dependency injection." />
</object>

Listing 5: Simple Message by Property Object Mapping

In this example, the no-argument constructor is used to instantiate the object and the message property is set directly. The only difference in the code that calls this object is to ask for a "SimpleMessageByProperty" object instead of a "SimpleMessage" object. Otherwise, the object behaves the same, aside from a slightly different message.

Generally, I like to use constructor injection because it makes it a bit clearer how the object is intended to be initialized. But, there are some cases that require the flexibility of property injection over constructor injection. In the end, it really just comes down to a matter of preference for which type of injection to use.

Reference Injection Example with the Strategy Pattern

The previous simple message example is a good introduction to mapping an object, but it really doesn't serve as a good example of how Spring.Net can be a powerful tool. Setting a simple string property is nothing special. So now, look at a more interesting example that injects some object references into an object and you'll combine that with the strategy pattern to give it some pizzazz.

First off, for those of you unfamiliar with the strategy pattern, it is a pattern in which you abstract out a specific behavior of an object that may have several different implementations into a separate set of strategy classes. Generally, this is implemented by giving an object a reference to one of several concrete strategy objects though a strategy interface. The strategy interface then is used to run the specified strategy for the desired behavior.

Figure 3: Strategy Pattern

The strategy pattern is useful for abstracting out complex calculations, algorithms, or specialized actions that may change sometime in the future. For this example, look at a simple number array class and give it a sort function that utilizes the different possible sorting functions (Bubble Sort, Heap Sort, Quick Sort, and so forth) through the use of strategies.

Here is the code:

public interface INumberArray {
   void addNumber(int number);
   int[] getNumbers();
   void sortNumbers();
}

public class NumberArray : INumberArray {
   private int _numberOfElements = 0;
   private int[] _numbers = new int[25];
   private ISortStrategy _sortStrategy;

   public NumberArray(ISortStrategy sortStrategy) {
      _sortStrategy = sortStrategy;
   }

   public void addNumber(int number) {
      _numbers[_numberOfElements] = number;
      _numberOfElements++;
   }

   public int[] getNumbers() {
      int[] result = new int[_numberOfElements];
      for (int x = 0; x < _numberOfElements; x++ ) {
         result[x] = _numbers[x];
      }
      return result;
   }

   public void sortNumbers() {
      _sortStrategy.sort(_numbers, _numberOfElements);
   }
}

public interface ISortStrategy {
   void sort(int[] numbers, int numOfElements);
}

public class BubbleSortStrategy : ISortStrategy {
   public void sort(int[] numbers, int numOfElements) {
      // Bubble Sort Algorithm (See example code)
   }
}

public class HeapSortStrategy : ISortStrategy {
   public void sort(int[] numbers, int numOfElements) {
      // Heap Sort Algorithm (See example code)
   }
}

public class QuickSortStrategy : ISortStrategy {
   public void sort(int[] numbers, int numOfElements) {
      // Quick Sort Algorithm (See example code)
   }
}

Listing 6: Number Array & Strategies





Page 3 of 6



Comment and Contribute

 


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

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel