1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;
namespace UnityConfigurationTree
{
public static class UnityConfExt
{
const string basePath = @"unity";
public static void Load(IUnityContainer container)
{
var rsRoot = RelationShip.Load($@"{basePath}\relationship.json");
Configure(rsRoot, container);
}
public static IUnityContainer Load()
{
IUnityContainer container = new UnityContainer();
Load(container);
return container;
}
static void Configure(RelationShip rs, IUnityContainer container)
{
foreach (var path in rs.Path)
{
var confSection = GetSection($@"{basePath}\{path}");
confSection.Configure(container);
}
if (rs.Children != null)
{
foreach (var rsChildren in rs.Children)
{
var subContainer = container.CreateChildContainer();
container.RegisterInstance(rsChildren.Name, subContainer);
Configure(rsChildren, subContainer);
}
}
}
static UnityConfigurationSection GetSection(string path)
{
var exeConfigurationFileMap = new ExeConfigurationFileMap { ExeConfigFilename = path };
System.Configuration.Configuration configuration =
ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None);
var unityConfigurationSection = configuration.GetSection("unity") as UnityConfigurationSection;
return unityConfigurationSection;
}
}
}