-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIocLocator.cs
99 lines (81 loc) · 3.4 KB
/
IocLocator.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Practices.Unity;
namespace sharp_net {
/// <summary>
/// Represents the Service Locator.
/// </summary>
public sealed class IocLocator {
private IocLocator() {
//使用Web.config配置:
//UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
//section.Configure(container);
}
private static IUnityContainer container;
public static IUnityContainer Container {
get { return container ?? (container = new UnityContainer()); }
}
private static IocLocator instance;
public static IocLocator Instance {
get { return instance ?? (instance = new IocLocator()); }
}
private IEnumerable<ParameterOverride> GetParameterOverrides(object overridedArguments) {
List<ParameterOverride> overrides = new List<ParameterOverride>();
Type argumentsType = overridedArguments.GetType();
argumentsType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.ToList()
.ForEach(property => {
var propertyValue = property.GetValue(overridedArguments, null);
var propertyName = property.Name;
overrides.Add(new ParameterOverride(propertyName, propertyValue));
});
return overrides;
}
public T GetImple<T>() {
return Container.Resolve<T>();
}
public T GetImple<T>(string name) {
return Container.Resolve<T>(name);
}
public T GetImple<T>(object overridedArguments) {
var overrides = GetParameterOverrides(overridedArguments);
return Container.Resolve<T>(overrides.ToArray());
}
public T GetImple<T>(string name, object overridedArguments) {
var overrides = GetParameterOverrides(overridedArguments);
return Container.Resolve<T>(name, overrides.ToArray());
}
public object GetImple(Type serviceType) {
return Container.Resolve(serviceType);
}
public object GetImple(string name, Type serviceType) {
return Container.Resolve(serviceType, name);
}
public object GetImple(Type serviceType, object overridedArguments) {
var overrides = GetParameterOverrides(overridedArguments);
return Container.Resolve(serviceType, overrides.ToArray());
}
public object GetImple(string name, Type serviceType, object overridedArguments) {
var overrides = GetParameterOverrides(overridedArguments);
return Container.Resolve(serviceType, name, overrides.ToArray());
}
#region IDependencyResolver
public object GetService(Type serviceType) {
try {
return Container.Resolve(serviceType);
} catch {
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType) {
try {
return Container.ResolveAll(serviceType);
} catch {
return new List<object>();
}
}
#endregion
}
}