Click here to Skip to main content
15,883,606 members
Articles / Programming Languages / C#

Dependency Injection Frameworks - Part 1 - Introduction

Rate me:
Please Sign up or sign in to vote.
4.65/5 (26 votes)
12 Jun 2008CPOL4 min read 81.7K   630   65  
An introduction to the dependency injection design pattern and frameworks, illustrated by a simple example.
using System;
using Spring.Context;
using Spring.Context.Support;

namespace SpringDotNETExample
{
    public interface ISayHello
    {
        String SayHello { get; }
    }

    public class WorldSayHello : ISayHello
    {        
        public string SayHello
        {
            get { return "Hello Friend!"; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            ISayHello hello = (ISayHello)ctx.GetObject("ISayHelloInterface");
            Console.WriteLine(hello.SayHello);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) MixModes Inc. | Research In Motion
Canada Canada
Ashish worked for Microsoft for a number of years in Microsoft Visual Studio (Architect edition) and Windows Live division as a developer. Before that he was a developer consultant mainly involved in distributed service development / architecture. His main interests are distributed software architecture, patterns and practices and mobile device development.

Currently Ashish serves as a Technical Lead at RIM leading next generation BlackBerry media experience and also runs his own company MixModes Inc. specializing in .NET / WPF / Silverlight technologies. You can visit MixModes at http://mixmodes.com or follow it on Twitter @MixModes

In his free time he is an avid painter, hockey player and enjoys travelling. His blog is at: http://ashishkaila.serveblog.net

Comments and Discussions