Something cool I created today with Ninject...
I created a provider that allows me to do this:
class Foo {
public Foo(AppSetting bar) { // AppSetting is a simple wrapper
around a string
Console.Write(bar.Value)
}
}
where web.config contains:
<appSettings> <add key="bar" value="123" /> </appSettings>
The single line to bind the provider:
Bind<AppSetting>().ToProvider<AppSettingProvider>();
An finally the provider:
class AppSettingProvider : Ninject.Core.Creation.IProvider
{
public object Create(Ninject.Core.Activation.IContext context)
{
return new
AppSetting(WebConfigurationManager.AppSettings[
context.Member.Name]);
}
public Type GetImplementationType(Ninject.Core.Activation.IContext
context)
{
return typeof(AppSetting);
}
public bool IsCompatibleWith(Ninject.Core.Activation.IContext
context)
{
return
WebConfigurationManager.AppSettings[
context.Member.Name] != null;
}
public Type Prototype
{
get { return typeof(AppSetting); }
}
}
This was so simple and easy to figure out - beautiful!
I'm more impressed with Ninject each time I use it.
:)