问题描述
有没有在ASP.NET MVC中的任何地方,你可以得到该是为了实例进行物业注入添加自定义DB实现从获取的资源ResourceProviderFactory参考?
Is there anywhere in ASP.NET MVC where you can get a reference to the ResourceProviderFactory that is instantiated in order to perform Property Injection to add a custom DB Implementation to retrieve the resources from?
我有被加载自定义资源提供者,但我想知道是否有使用静态DI容器注入提供商内部的依赖的替代品。
I have a custom resource provider being loaded but I wanted to know whether there was an alternative to using a Static DI container to inject the dependency within the Provider.
如何您可以在MVC中的作用和成员机构注入类似。
Similar to how you can inject for the Role and Membership providers in MVC.
推荐答案
关键是要实现基础设施组件调用到MVC的 DependencyResolver.Current
,这样你就可以注册真正用你喜欢的DI容器 ResourceProviderFactory
。
The trick is to implement an infrastructure component calls into MVC's DependencyResolver.Current
, so you can register the real ResourceProviderFactory
using your favorite DI container.
下面是这样一个类,将这样的伎俩:
Here is such a class that will do the trick:
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"
/>
这篇关于自ResourceProviderFactory依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!