使用 json 的 Unity IOC 配置
Unity IOC configuration with json
在 app.config 中,它可能如下所示,服务和存储库在不同的文件中定义 Unity.config。我的问题是,是否可以在 appsettings.json 而不是 app.config 和 Unity.json 而不是 Unity.config 中执行以下操作?
app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" requirePermission="false" restartOnExternalChanges="true" />
</configSections>
<Unity configSource="Config\Unity.config"/>
<appSettings>
<add key="DateFormat" value="yyyyMMdd"/>
</appSettings>
</configuration>
Unity.config
<Unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IHelpfulRepository" type="Repositories.IHelpfulRepository, Repositories" />
<alias alias="HelpfulRepository" type="Repositories.Implementation.HelpfulRepository, Repositories" />
</Unity>
不,这不可能。有解决办法。
所以我要做的就是制作自己的 UnityContainerExtensions class,加载一个看起来像 app.config 文件的 xml 文件,获取统一部分,然后将其转换到 UnityConfigurationSection。下面的代码
MyUnityContainerExtensions.cs
public static class MyUnityContainerExtensions
{
public static IUnityContainer LoadXML(this IUnityContainer container, string xmlFilePath = ".\Config\Unity.config")
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = xmlFilePath };
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection("unity");
return section.Configure(container, string.Empty);
}
}
Unity.config
<configuration>
<configSections>
<section name="Unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration" requirePermission="false" restartOnExternalChanges="true" />
</configSections>
<Unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IHelpfulRepository" type="Repositories.IHelpfulRepository, Repositories" />
<alias alias="HelpfulRepository" type="Repositories.Implementation.HelpfulRepository, Repositories" />
</Unity>
...
</configuration>
然后在加载统一配置的地方,我只调用新的扩展方法。
IUnityContainer container = new UnityContainer().LoadXML();
在 app.config 中,它可能如下所示,服务和存储库在不同的文件中定义 Unity.config。我的问题是,是否可以在 appsettings.json 而不是 app.config 和 Unity.json 而不是 Unity.config 中执行以下操作?
app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" requirePermission="false" restartOnExternalChanges="true" />
</configSections>
<Unity configSource="Config\Unity.config"/>
<appSettings>
<add key="DateFormat" value="yyyyMMdd"/>
</appSettings>
</configuration>
Unity.config
<Unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IHelpfulRepository" type="Repositories.IHelpfulRepository, Repositories" />
<alias alias="HelpfulRepository" type="Repositories.Implementation.HelpfulRepository, Repositories" />
</Unity>
不,这不可能。有解决办法。
所以我要做的就是制作自己的 UnityContainerExtensions class,加载一个看起来像 app.config 文件的 xml 文件,获取统一部分,然后将其转换到 UnityConfigurationSection。下面的代码
MyUnityContainerExtensions.cs
public static class MyUnityContainerExtensions
{
public static IUnityContainer LoadXML(this IUnityContainer container, string xmlFilePath = ".\Config\Unity.config")
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = xmlFilePath };
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection("unity");
return section.Configure(container, string.Empty);
}
}
Unity.config
<configuration>
<configSections>
<section name="Unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration" requirePermission="false" restartOnExternalChanges="true" />
</configSections>
<Unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IHelpfulRepository" type="Repositories.IHelpfulRepository, Repositories" />
<alias alias="HelpfulRepository" type="Repositories.Implementation.HelpfulRepository, Repositories" />
</Unity>
...
</configuration>
然后在加载统一配置的地方,我只调用新的扩展方法。
IUnityContainer container = new UnityContainer().LoadXML();