September 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Dependency Injection with Spring.Net

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

In this example, there is a presentation/view layer that takes care of displaying the application data either though a web page, client application, mobile application, or whatever. Then, you have a service/business layer that takes care of the application logic such as any calculations, operations, and so on. And finally, you have a data layer that handles communications with the database, XML, or whatever other data sources the application might have. Each layer communicates only with the other layers via an interface, but each layer has no idea how the other layers are setup or how they operate.

For example, the presentation/view layer will ask the service layer to create a new business object of some sort for the given inputs from the user, the service layer will do some logic to build an object based on the inputs and then it will call the data layer to persist the object. The service layer doesn't need to concern itself with what type of application the presentation layer collected the inputs with (in other words, Web or Client) and the service layer also doesn't care how the data layer persists the object (in other words, MS SQL, DB2, or XML file). This makes the application easily configurable for different uses. If the one layer needs to change, only that layer needs to be rewritten and the rest can be reused.

Spring.Net and Dependency Injection help implement multi-tier application architectures by splitting an application into configurable components. With Spring.Net, it is easy to keep all the components of one layer separate from the other layers and to inject dependencies with other layers transparently through the use of XML configuration files.

Take a quick look at some example code for wiring a Service object from the service/business layer with a data access object (DAO) from the data layer:

public interface IServiceExample {

   void doOperationOnObject(Object obj);
   Object getObject(int objectID);
   void saveObject(Object obj);
}

public class ServiceExample : IServiceExample {

   public IDAOExample _daoExample;

   public ServiceExample(IDAOExample daoExample) {
      _daoExample = daoExample;
   }

   public void doOperationOnObject(Object obj) {
      // Enter business logic for operation here
   }

   public Object getObject(int objectID) {
      // Use DAO to retrieve object from the database
      return _daoExample.getObject(objectID);
   }

   public void saveObject(Object obj) {
      // Use DAO to save object to the database
      _daoExample.saveObject(obj);
   }
}

public interface IDAOExample {
   Object getObject(int objectID);
   void saveObject(Object obj);
}

public class DAOExample : IDAOExample {

   public DAOExample() {
   }

   public Object getObject(int objectID) {
      // Put database code to retrieve object here
   }

   public void saveObject(Object obj) {
      // Put database code to save object here
   }
}

Listing 7: Service & DAO

<!-- Service & DAO -->

<object name="ServiceExample"
        type="ServiceExample, __Code"
        singleton="true">
   <constructor-arg name="daoExample" ref="DAOExample" />
</object>

<object name="DAOExample"
        type="DAOExample, __Code"
        singleton="true">
</object>

Listing 8: Service & DAO Mappings

In this simple example, you have a service object that communicates with a DAO object to save objects to the database. The DAO object is injected into the service object via Spring.Net. By using injection, there are no specific links between layers within the code.

Say your application is moving from a MySQL database implementation to an Oracle database implementation. All you would need to do is to create a new DAO class for Oracle, and then switch the Spring.Net configuration to inject the new Oracle DAO object into the service instead of the MySQL DAO object. Nothing in the service layer would need to change at all.

Note that in this case, the presentation layer using the service objects would generally either call Spring.Net to get the service objects or call some sort of service manager to get the service. Spring.Net also provides other options worth looking into like their web framework which allows you to directly inject the service objects into ASP.Net pages.

Also note that both the service and DAO objects are singletons because they are used just for their operations and don't hold any specific state.





Page 5 of 6



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel