Ninject 中 Windsor 的 DictionaryAdapterFactory 相当于什么?
What's the equivalent of the DictionaryAdapterFactory of Windsor in Ninject?
我正在尝试在 Ninject 上重现 Windsor 的以下配置。
我知道 ninject 有一个工厂扩展,但它与 windsor 拥有的字典适配器相去甚远。
WindsorContainer container = new WindsorContainer();
container.AddFacility<Castle.Facilities.FactorySupport.FactorySupportFacility>();
container.Register(
Component.For<IApplicationSettings>().UsingFactoryMethod(
() => new DictionaryAdapterFactory()
.GetAdapter<IApplicationSettings>(ConfigurationManager.AppSettings)));
有人知道 ninject 的工厂扩展吗?
Factory 扩展允许您提供自己的 IInstanceProvider
。您可以将其用于配置中的 return 值:
class Program
{
static void Main(string[] args)
{
var kernel = new StandardKernel();
kernel.Bind<IApplicationSettings>().ToFactory(() => new AppSettingProvider());
var settings = kernel.Get<IApplicationSettings>();
Console.WriteLine("One: " + settings.One);
}
}
public interface IApplicationSettings
{
string One { get; }
int Two { get; }
bool Three { get; }
}
public class AppSettingProvider : Ninject.Extensions.Factory.IInstanceProvider
{
public object GetInstance(IInstanceResolver instanceResolver, MethodInfo methodInfo, object[] arguments)
{
string key = methodInfo.Name;
if (key.StartsWith("get_"))
key = key.Substring(4);
string value = ConfigurationManager.AppSettings[key];
return Convert.ChangeType(value, methodInfo.ReturnType);
}
}
我正在尝试在 Ninject 上重现 Windsor 的以下配置。 我知道 ninject 有一个工厂扩展,但它与 windsor 拥有的字典适配器相去甚远。
WindsorContainer container = new WindsorContainer();
container.AddFacility<Castle.Facilities.FactorySupport.FactorySupportFacility>();
container.Register(
Component.For<IApplicationSettings>().UsingFactoryMethod(
() => new DictionaryAdapterFactory()
.GetAdapter<IApplicationSettings>(ConfigurationManager.AppSettings)));
有人知道 ninject 的工厂扩展吗?
Factory 扩展允许您提供自己的 IInstanceProvider
。您可以将其用于配置中的 return 值:
class Program
{
static void Main(string[] args)
{
var kernel = new StandardKernel();
kernel.Bind<IApplicationSettings>().ToFactory(() => new AppSettingProvider());
var settings = kernel.Get<IApplicationSettings>();
Console.WriteLine("One: " + settings.One);
}
}
public interface IApplicationSettings
{
string One { get; }
int Two { get; }
bool Three { get; }
}
public class AppSettingProvider : Ninject.Extensions.Factory.IInstanceProvider
{
public object GetInstance(IInstanceResolver instanceResolver, MethodInfo methodInfo, object[] arguments)
{
string key = methodInfo.Name;
if (key.StartsWith("get_"))
key = key.Substring(4);
string value = ConfigurationManager.AppSettings[key];
return Convert.ChangeType(value, methodInfo.ReturnType);
}
}