Saturday, February 26, 2011

Getting Started with Spring.NET

I have been a big fan of the Spring Framework for quite some time. As a matter of fact, I count on the many benefits and features of Spring for most of my Java projects. I have found Spring to be a gateway to productivity and better practice development for a number of solutions in the enterprise.

Recently, I have been investigating the .NET platform and C#. We have a mixed Java/C# environment at work and I would like to be a more flexible resource in order to help out on more projects. As I started looking into C#, I thought it was only appropriate to look into the Spring.NET project. It is quite similar to its Java counterpart and here is how I got things started with a very simple C# project:

First, I downloaded Visual C# 2010 Express. It is not quite as fancy as the variety of Visual Studio 2010 offerings, but it is a great IDE to get started with learning C#. Next, I downloaded the latest Spring.NET release, which happens to be version 1.3.1. In Visual C# 2010 Express, I then created a new blank project, 'SpringNET1'. Next, I added the Spring.Core.dll and Common.Logging.dll as references in my new project.

I created a new Class, MyApp.cs. This is the entry point of my application. I will go over the details of the Main(string[] args) function shortly, but basically it grabs the application context, gets an instance of the MyService class, calls MyService's GetName() function and writes the output to the console.

using System;
using Spring.Context;
using Spring.Context.Support;

namespace App.Core
{
    class MyApp
    {
        static void Main(string[] args)
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            ServiceInterface myService = (MyService) ctx.GetObject("MyService");
            Console.WriteLine(myService.GetName());
            Console.ReadLine();
        }
    }
}

Then I created an interface named ServiceInterface.cs with one function, GetName(), which returns a type of String.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace App.Core
{
    interface ServiceInterface
    {
        String GetName();
    }
}


Below is an implementation of the ServiceInterface, MyService.cs, which implements the GetName() function by returning my name, "RJ Salicco".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace App.Core
{
    class MyService : ServiceInterface
    {
        public String GetName()
        {
            return "RJ Salicco";
        }
    }
}

Lastly we need to create an Application Config file and let the runtime know about Spring and our Spring objects. The important thing to note here is the way that objects are configured. Using the <object> tag, we define the 'id' of the object and the 'type' of the object which is denoted by the fully qualified class name (namespace and class name) and the Assembly name separated by a comma. The 'id' is what I used to lookup the MyService instance in our context used in the MyApp class.

<configuration>
    <configSections>
        <sectionGroup name="spring">
        <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
        <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
    </configSections>
    <spring>
        <context>
            <resource uri="config://spring/objects"/>
        </context>
        <objects xmlns="http://www.springframework.net">
            <description>Simple bean setup.</description>
            <object id="MyService" type="App.Core.MyService, App.Core"></object>
        </objects>
    </spring>
</configuration>


I knowing this example is really, really simple, but I just wanted to document a starting point. We can all imagine a real world example implementation, wiring in some more objects, referencing objects as constructor arguments or properties and possibly utilizing the features of Spring AOP. As I dig deeper into C# and the .NET platform I plan on sharing what I learn and if Spring.NET delivers just some of the features its Java counterpart delivers, I will feel confident delivering C# solutions along with my Java solutions.
Post a Comment