forked from xuxiaowei007/FoxOne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectHelper.cs
117 lines (107 loc) · 3.91 KB
/
ObjectHelper.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
using System.IO;
using System.Reflection;
namespace FoxOne.Core
{
public static class ObjectHelper
{
private static IUnityContainer container;
private const string UNITY_DIRECTORY_NAME = "Unity";
static ObjectHelper()
{
Init();
}
public static void RegisterType<TFrom, TTo>() where TTo : TFrom
{
container.RegisterType<TFrom, TTo>();
}
public static void RegisterType<TFrom, TTo>(string name) where TTo : TFrom
{
container.RegisterType<TFrom, TTo>(name);
}
public static void RegisterType(Type from, Type to, params InjectionMember[] injectionMembers)
{
container.RegisterType(from, to, injectionMembers);
}
public static T GetObject<T>() where T : class
{
return container.Resolve<T>();
}
public static T GetObject<T>(string name) where T : class
{
return container.Resolve<T>(name);
}
public static object GetObject(Type type)
{
return container.Resolve(type);
}
/// <summary>
/// only return named registion
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static IEnumerable<T> GetAllObjects<T>() where T : class
{
return container.ResolveAll<T>();
}
public static Type GetRegisterType(Type type)
{
if (type.IsInterface)
{
var instance = container.Resolve(type);
if (instance != null)
{
return instance.GetType();
}
}
return type;
}
public static Type GetRegisterType<TFrom>()
{
return GetRegisterType(typeof(TFrom));
}
private static void Init()
{
container = new UnityContainer();
DirectoryInfo dir = null;
if (Utility.FindConfigDirectory(UNITY_DIRECTORY_NAME, out dir))
{
IEnumerable<FileInfo> files = dir.GetFiles("*.config", SearchOption.AllDirectories);
IEnumerable<Assembly> assemblies = Assemblies.GetAssemblies();
foreach (FileInfo file in files)
{
try
{
UnityConfigurationSection section = Utility.GetConfigSection<UnityConfigurationSection>("unity", file.FullName);
if (null != section)
{
if (section.Containers.Count > 1)
{
throw new FoxOneException("Multiple '<container>' Element Not Supported in {0}", file.FullName);
}
if (section.Containers.Count != 0)
{
foreach (var assembly in assemblies)
{
section.Assemblies.Add(new AssemblyElement() { Name = assembly.GetName().Name });
}
section.Configure(container);
}
}
}
catch (Exception e)
{
string message = string.Format("Load Unity Config '{0}' Error : {1}", file.Name, e.Message);
throw new FoxOneException(message, e);
}
}
}
}
}
}