【问题标题】:How to use griffin.mvccontrib.localization with Ninject如何在 Ninject 中使用 griffin.mvccontrib.localization
【发布时间】:2012-10-18 15:15:49
【问题描述】:

我正在尝试在我的 Asp.net MVC 应用程序中使用 MVC Contrib 本地化,现在我让它处理资源文件,但我想将它与 Sql Server 一起使用,

我正在查看本教程:http://www.codeproject.com/Articles/352583/Localization-in-ASP-NET-MVC-with-Griffin-MvcContri

但它使用 Autofac 作为 IoC 容器,我不明白,有人用过 Ninject 吗?或者任何人都知道如何将此 Autofac 代码转换为 Ninject:

// Loads strings from repositories.
builder.RegisterType<RepositoryStringProvider>().AsImplementedInterfaces().InstancePerLifetimeScope();
builder.RegisterType<ViewLocalizer>().AsImplementedInterfaces().InstancePerLifetimeScope();

// Connection factory used by the SQL providers.
builder.RegisterInstance(new AdoNetConnectionFactory("DemoDb")).AsSelf();
builder.RegisterType<LocalizationDbContext>().AsImplementedInterfaces().InstancePerLifetimeScope();

// and the repositories
builder.RegisterType<SqlLocalizedTypesRepository>().AsImplementedInterfaces().InstancePerLifetimeScope();
builder.RegisterType<SqlLocalizedViewsRepository>().AsImplementedInterfaces().InstancePerLifetimeScope();

提前致谢。

【问题讨论】:

    标签: asp.net-mvc localization ninject autofac griffin.mvccontrib


    【解决方案1】:

    我目前正在尝试使用 Ninject.Web.MVC NuGet 包做同样的事情。

    我不确定 Ninject 是否有任何类似于 .AsImplementedInterfaces() 的东西,但是如果没有,您仍然可以自己绑定接口,它只是更多的手动工作,查看 Griffin.MvcContrib 的类以及它们实现的接口。

    NinjectWebCommon RegisterServices 方法的一个例子是:

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ILocalizedStringProvider>()
               .To<RepositoryStringProvider>().InRequestScope();
    ...
    }  
    

    InRequestScope 扩展名 (https://github.com/ninject/Ninject.Web.Common/wiki/Inrequestscope),就我目前所读到的而言,是我能看到的最接近 AutoFac .InstancePerLifetimeScope()http://code.google.com/p/autofac/wiki/InstanceScope

    至于.RegisterInstance(new AdoNetConnectionFactory("DemoDb")).AsSelf();

    Ninject 有一个 .ToSelf() 方法,但我自己还不完全确定这条线在做什么。

    【讨论】:

      猜你喜欢
      • 2012-09-07
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 2021-10-05
      • 2015-02-10
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多