developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




Vote for the Developer.com Product of the Year Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Big Video Demands Big Routers    November 11, 2008
Surprise, an iPhone Failure? Dev Event Canceled    November 11, 2008
Microsoft Updates Best Practices for Developers    November 11, 2008
IBM's Advanced Chip Fab Available for Hire    November 11, 2008
Free Tech Newsletter -

Dependency Injection with Spring.Net
By David Consdorf

Go to page: Prev  1  2  3  4  5  6  Next  

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

Go to page: Prev  1  2  3  4  5  6  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Open Source Archives






internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers