自定义ResourceProviderFactory依赖注入

在ASP.NET MVC中有没有可以获得对实例化的ResourceProviderFactory的引用,以执行属性注入来添加自定义数据库实现以从中检索资源?

我有一个自定义资源提供程序正在加载,但我想知道是否有替代使用静态DI容器注入提供程序内的依赖关系。

与您可以在MVC中为角色和成员资格提供程序注入的方式类似。


诀窍是将基础设施组件调用实现为MVC的DependencyResolver.Current ,因此您可以使用您最喜爱的DI容器注册真正的ResourceProviderFactory

这是一个能够诀窍的类:

public class MvcResourceProviderFactory : ResourceProviderFactory
{
    public override IResourceProvider CreateGlobalResourceProvider(
        string classKey)
    {
        return GetFactory().CreateGlobalResourceProvider(classKey);
    }

    public override IResourceProvider CreateLocalResourceProvider(
        string virtualPath)
    {
        return GetFactory().CreateLocalResourceProvider(virtualPath);
    }

    private static ResourceProviderFactory GetFactory()
    {
        var resolver = DependencyResolver.Current;

        var factory = resolver.GetService<ResourceProviderFactory>();

        if (factory != null)
        {
            return factory;
        }

        throw new InvalidOperationException(string.Format(
            "No {0} has been registered in the {1}.",
            typeof(ResourceProviderFactory).FullName,
            DependencyResolver.Current.GetType().FullName));
    }
}

这个类可以配置如下:

<globalization
    resourceProviderFactoryType="MyApp.MvcResourceProviderFactory, MyApp"
    enableClientBasedCulture="true" uiCulture="auto" culture="auto"
/>
链接地址: http://www.djcxy.com/p/59467.html

上一篇: Custom ResourceProviderFactory Dependency Injection

下一篇: suggest like google with postgresql trigrams and full text search