Hallo,

hier mal ein einfaches Beispiel dazu wie man das neue Dependency Injection Framework Unity von Microsoft verwendet:

http://www.codeplex.com/unity

Code csharp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
 
namespace De.Tutorials.Unity
{
    public class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer().Register<IFoo, Foo>();
            Bar bar = container.Get<Bar>();
            Console.WriteLine(bar);
        }
    }
 
    public interface IFoo{}
 
    public class Foo : IFoo{}
 
    public class Bar
    {
        [Dependency]
        public IFoo Foo { get; set; }
 
        public override string ToString()
        {
            return "Foo: " + Foo;
        }
    }
}

Ausgabe:
Code :
1
Foo: De.Tutorials.Unity.Foo

Die API fühlt sich ein wenig so an wie die von Googles Guice:
http://code.google.com/p/google-guice/

Gruß Tom