Click here to Skip to main content
Click here to Skip to main content

SPRING.NET + iBATIS.NET With ASP.NET MVC - Part 1

, 26 Apr 2009
Rate this:
Please Sign up or sign in to vote.
This article explains how to make a web board with Spring.NET, Ibatis.NET Framework.

1. Introduction

Recently, Microsoft announced the ASP.NET MVC. A pattern that supports these features is the MVC (Model-View-Controller). The ASP.NET team has recognized the benefits of this pattern and is working to incorporate it into ASP.NET. This article will focus on building an ASP.NET MVC web application with SPRING.NET and iBATIS.NET.

1. 1. Prerequisites

  • OS: Windows Server 2003; Windows Server 2008; Windows Vista; Windows XP
  • IDE: Visual Studio 2008, Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1
  • Database: Microsoft SQL 2000 or higher
  • Framework: .NET 3.5 SP1
  • ASP.NET MVC 1.0 - Download
  • SPRING.NET 1.2.0 - Download
  • iBATIS.NET DataMapper 1.6.1 - Download

1. 2. What is SPRING.NET ?

SRING.NET is an application framework or lightweight container. It allows .NET developers who are building enterprise .NET application to get to the heart of their real domain problems and stop spending so much time on the minutiae of providing services to their domain. It also allows you to remove incidental complexity when using the base class libraries makes best practices, such as test driven development, easy practices.

The design of SPRING.NET is based on the Java version of the Spring Framework, which has shown real-world benefits and is used in thousands of enterprise apps world wide. but, SRING.NET is not a quick port from the Java version. I think .NET Framework is very powerful and made up of huge useful libraries. So, SRING.NET Framework is just helping .NET Framework.

1. 2. 1. Background

In early 2004, Martin Fowler asked the readers of his site: when talking about Inversion of Control: “the question is, what aspect of control are [they] inverting?”. Fowler then suggested renaming the principle (or at least giving it a more self-explanatory name), and started to use the term Dependency Injection. His article then continued to explain the ideas underpinning the Inversion of Control (IoC) and Dependency Injection (DI) principle. If you need a decent insight into IoC and DI, please do refer to the article: http://martinfowler.com/articles/injection.html.

1. 2. 2. Modules

Spring.Core is the most fundamental part of the framework allowing you to configure your application using Dependency Injection. Other supporting functionality, listed below, is located in Spring.Core.

Spring.Aop - Use this module to perform Aspect-Oriented Programming (AOP). AOP centralizes common functionality that can then be declaratively applied across your application in a targeted manner. Spring's aspect library provides predefined easy to use aspects for transactions, logging, performance monitoring, caching, method retry, and exception handling.

Spring.Data - Use this module to achieve greater efficiency and consistency in writing data access functionality in ADO.NET and to perform declarative transaction management.

Spring.Data.NHibernate - Use this module to integrate NHibernate with Spring’s declarative transaction management functionality allowing easy mixing of ADO.NET and NHibernate operations within the same transaction. NHibernate 1.0 users will benefit from ease of use APIs to perform data access operations.

Spring.Web - Use this module to raise the level of abstraction when writing ASP.NET web applications allowing you to effectively address common pain-points in ASP.NET such as data binding, validation, and ASP.NET page/control/module/provider configuration.

Spring.Web.Extensions - Use this module to raise the level of abstraction when writing ASP.NET web applications allowing you to effectively address common pain-points in ASP.NET such as data binding, validation, and ASP.NET page/control/module/provider configuration.

Spring.Services - Use this module to adapt plain .NET objects so they can be used with a specific distributed communication technology, such as .NET Remoting, Enterprise Services, and ASMX Web Services. These services can be configured via dependency injection and ‘decorated’ by applying AOP.

Spring.Testing.NUnit - Use this module to perform integration testing with NUnit.

SPRING.NET consist of 8 libraries. However, I'll use 3 libraries that are marked bold libraries (Core, Aop, Web).

The Spring.Core's DI is very useful function. That is completely disconnected the dependency.
And Spring.Aop provides declarative enterprise services, especially as a replacement for COM+ declarative services. The most important such service is declarative transaction management, which builds on Spring.NET's transaction abstraction. This functionality is planned for an upcoming release of Spring.NET.
Spring.Web is amazingly located in the top of the Spring IoC Container.

1. 2. 3. A Simple Example of SPRING.NET Configuration

 <system.web>
    <httpHandlers>

        <add verb="*" path="*.aspx" 
		type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
    </httpHandlers>

    <httpModules>

        <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
    </httpModules>

    ...

</system.web>

1. 3. What is iBATIS.NET ?

The iBATIS is a simple but complete framework that makes it easy for you to map your objects to your SQL statements or stored procedures. One definition of a Mapper is an "object that sets up communication between two independent objects." A Data Mapper is a "layer of mappers that moves data between objects and a database while keeping independent of each other and the mapper itself." - Patterns of Enterprise Architecture(ISBN 0-321-12742-0) -

1. 3. 1. How Does iBATIS.NET Work?

  • Separating SQL code from programming code
  • Passing input parameters to the library classes and extracting the output
  • Caching often-used data until it changes
  • Managing transactions and threading
  • Using the Entity Class (in other words, VO;Value Object)

1. 3. 2. A simple Data Map

 <?xml version="1.0" encoding="UTF-8" ?>
<sqlMap namespace="LineItem"     
        xmlns="http://ibatis.apache.org/mapping"     
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <!--Type aliases allow you to use a shorter name for long 
	fully qualified class names.-->
  <alias>
    <typeAlias alias="LineItem" 
	type="NPetshop.Domain.Billing.LineItem, NPetshop.Domain" />
  </alias>
  <statements>
    <insert id="InsertLineItem" parameterClass="LineItem">   
      INSERT INTO [LinesItem](Order_Id, LineItem_LineNum, 
		Item_Id, LineItem_Quantity, LineItem_UnitPrice)
      VALUES (#Order.Id#, #LineNumber#, #Item.Id#, #Quantity#, #Item.ListPrice#)  
	</insert>
  </statements>
</sqlMap>

This map takes some properties from a LineItem instance and merges the values into the SQL statement. The value-add is that our SQL is separated from our program code, and we can pass our LineItem instance directly to a library method:

Mapper.Instance().Insert("InsertLineItem", lineItem)

This code shows completely separating SQL code from programming code.

1. 4. ASP.NET MVC Life Cycle

요청① = Request① Smile | :)

ASP.NET MVC's Front Controller pattern has more complex life cycle than classic ASP.NET.
In addition, A front controller centralizes functions such as view selection, security, and templating, and applies them consistently across all pages or views. when the behavior of these functions need to change, only a small part of the application needs to be changed. It is often called Facade Pattern.

If you design for Facade pattern, that makes it easy implementing DI.

  1. Receive first request for the application. In the Global.asax file, Route objects are added to the RouteTable object.
  2. Perfume routing: The UrlRoutingModule module uses the first matching Route object in the RouteTable collection to create the RouteData object, which it then uses to create a RequestCntext(IHttpContext) object.
  3. Create MVC request handler: The MvcRouteHandler object creates an instance of the MvcHandler class and passes it the RequestContext instance.
  4. Create controller: The MvcHandler object uses the RequestContext instance to identify the IControllerFactory object (typically an instance of the DefaultControllerFactory class) to create the controller instance with.
    Execute controller: The MvcHandler instance calls the controller's Execute method.
  5. Invoke action : Most controllers inherit from the Controller base class. For controllers that do so, the ControllerActionInvoker object that is associated with the controller determines which action method of the controller class to call, and then calls that method.
  6. Execute result (result Views): A typical action method might receive user input, prepare the appropriate response data, and then execute the result by returning a result type. The built-in result types that can be executed include the following: ViewResult (which renders a view and is the most-often used result type), RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, and EmptyResult.

About the Korea VB.NET User Group

Article History

  • Initial article - Saturday, April 25, 2009

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

You may also be interested in...

About the Author

Moonhyuk
Software Developer Incruit
Korea (Republic Of) Korea (Republic Of)
Software developer in Korea.
Microsoft MVP - Visual Basic
Member of VB-Insiders Group.
Member of MHVB.net
Follow on   Twitter

Comments and Discussions

 
GeneralWow Nice~~ Pinmemberopclo29-Apr-09 16:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140902.1 | Last Updated 26 Apr 2009
Article Copyright 2009 by Moonhyuk
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid