【问题标题】:Generics and Property binding in NinjectNinject 中的泛型和属性绑定
【发布时间】:2013-02-12 01:00:07
【问题描述】:

我正在寻找一种将设置合并到我的 webapp (asp mvc) 中的好方法。我在 NopCommerce 中遇到了一个非常好的实现。 NopCommerce 使用名称和值将值存储在数据库表中。名称派生自类和属性名称(例如 customersettings.settingname1)

可以在以下问题中找到 NopCommerce 使用设置的确切方式:Understanding how Nop Commerce settings are loaded from the database

NopCommerce 使用 Autofac 作为 DI 框架将 Settings 绑定到 ConfigurationProvider 如下(我是正确的)。

return RegistrationBuilder
                .ForDelegate((c, p) => c.Resolve<IConfigurationProvider<TSettings>>().Settings)
                .InstancePerHttpRequest()
                .CreateRegistration();

在适当的类中,您现在可以使用ClientSettings 作为参数,它会自动填充数据库中的数据。

我真的很喜欢这个实现,因为它非常灵活。然而问题是我正在使用 Ninject。我已经尝试了几件事来获得正确的绑定,但似乎找不到正确的实现。有谁知道如何让它工作?

编辑:

我找到了直接绑定ClientSettings的方法:

kernel.Bind<ClientSettings>()
      .ToMethod(ctx => ctx.Kernel.Get<IConfigurationProvider<ClientSettings>>().Settings)
      .InRequestScope();

但是有没有办法做到这一点?

kernel.Bind<ISettings>()
      .ToMethod(ctx => ctx.Kernel.Get<IConfigurationProvider<ISettings>>().Settings)
      .InRequestScope();

编辑 2

我想我已经接近了,但仍然遇到了一些问题。我创建了一个自定义绑定生成器:

public class SettingsBindGenerator : IBindingGenerator
{
    static readonly MethodInfo BuildMethod = typeof(SettingsBindGenerator).GetMethod(
        "BuildRegistration",
        BindingFlags.Static | BindingFlags.NonPublic);      

    public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot)
    {
        var obj = typeof (object).IsAssignableFrom(type);
        if (type != null && typeof(ISettings).IsAssignableFrom(type))
        {
            var buildMethod = BuildMethod.MakeGenericMethod(type);
            var methodResult = buildMethod.Invoke(null, new object[]{bindingRoot});
            var castedResult = methodResult as IBindingWhenInNamedWithOrOnSyntax<object>;
            yield return castedResult;
        }
    }


    static IBindingWhenInNamedWithOrOnSyntax<TSettings> BuildRegistration<TSettings>(IBindingRoot bindingRoot) where TSettings : ISettings, new()
    {
        return bindingRoot.Bind<TSettings>().ToMethod(
            ctx => ctx.Kernel.Get<IConfigurationProvider<TSettings>>().Settings);
    }
}

这适用于 99%。但是,由于某种原因,buildMethod.Invoke 返回一个 BindingConfigurationBuilder,而不是 IBindingWhenInNamedWithOrOnSyntax。因此, castedResult 始终为 NULL。有人知道如何纠正这个问题吗?

最后编辑

我不知道为什么,但突然它起作用了!很高兴我终于弄明白了。谢谢雷莫!

【问题讨论】:

    标签: c# asp.net-mvc-3 ninject nopcommerce


    【解决方案1】:

    您有多种选择:

    1. 像 NopCommerce 一样进行操作,扫描设置类并使用反射从您的第一次编辑中调用注册方法。
    2. 使用约定扩展注册所有实现ISettings的类,使用自定义绑定生成器https://github.com/ninject/ninject.extensions.conventions/wiki/Projecting-Services-to-Bind
    3. 使用约定扩展注册所有实现ISettings的类以绑定所有接口。并向 Ninject 添加一个自定义 IActivationStrategy,它会像 NopCommerce 一样分配属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多