Thoughts on Software by Andrew Davey
| | Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|
| 29 | 30 | 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 | 1 | 2 | | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Search
Navigation
Categories
Blogroll
|

Tuesday, April 29, 2008
Ninject Open Generic Types
I have started using Ninject a dependency injection framework for .NET. I find it very easy to use and recommend that people take a look.
Something in particular that I found useful is the ability to bind open generic types.
class Program
{
static void Main(string[] args)
{
var k = new StandardKernel(new MyModule());
var di = k.Get<Data<int>>();
di.Content = 14;
Console.WriteLine(di.Content);
var ds = k.Get<Data<string>>();
ds.Content = "hello";
Console.WriteLine(ds.Content);
}
}
class MyModule : StandardModule
{
public override void Load()
{
Bind(typeof(IData<>)).To(typeof(Data<>));
}
}
class Data<T> : IData<T>
{
public T Content { get; set; }
}
interface IData<T>
{
T Content { get; set; }
}
Very cool!
Tuesday, April 29, 2008 6:36:59 PM (GMT Standard Time, UTC+00:00)
.net | c# | ninject | programming