|
1 | 1 | module mvc {
|
2 |
| - //不导出 |
3 |
| - class ClassInjectData { |
4 |
| - |
5 |
| - public propertys: { [index: string]: any } = {}; |
6 |
| - //typeEvents |
7 |
| - public typeEventHandles: { [index: string]: Array<InjectEventTypeHandle> } = {}; |
8 |
| - //指令 |
9 |
| - public cmds: { [index: number]: Array<(e: IStream) => void> } = {}; |
10 |
| - public constructor(public classPrototype: Function) { |
11 |
| - } |
12 |
| - /** |
13 |
| - * 取得完整类名 |
14 |
| - */ |
15 |
| - getFullClassName(): any { |
16 |
| - return this.classPrototype["constructor"]["name"]; |
17 |
| - } |
18 |
| - } |
19 |
| - |
20 | 2 | /**
|
21 | 3 | * 注入管理类
|
22 | 4 | */
|
@@ -50,58 +32,12 @@ module mvc {
|
50 | 32 | }
|
51 | 33 | }
|
52 | 34 |
|
53 |
| - private static injectMapping: Array<ClassInjectData> = new Array<ClassInjectData>(); |
54 |
| - |
55 |
| - public static AddMVC(classPrototype: Function, property: string) { |
56 |
| - |
57 |
| - let classInjectData: ClassInjectData = null; |
58 |
| - |
59 |
| - for (let item of MVCInject.injectMapping) { |
60 |
| - if (item.classPrototype == classPrototype) { |
61 |
| - classInjectData = item; |
62 |
| - break; |
63 |
| - } |
64 |
| - } |
65 |
| - |
66 |
| - if (!classInjectData) { |
67 |
| - classInjectData = new ClassInjectData(classPrototype); |
68 |
| - MVCInject.injectMapping.push(classInjectData); |
69 |
| - } |
70 |
| - |
71 |
| - classInjectData.propertys[property] = 1; |
72 |
| - } |
73 |
| - |
74 |
| - public static AddCMD(classPrototype: Function, cmd: number, handle: (e: IStream) => void) { |
75 |
| - |
76 |
| - let classInjectData = null; |
77 |
| - for (let item of MVCInject.injectMapping) { |
78 |
| - if (item.classPrototype == classPrototype) { |
79 |
| - classInjectData = item; |
80 |
| - break; |
81 |
| - } |
82 |
| - } |
83 |
| - |
84 |
| - if (!classInjectData) { |
85 |
| - classInjectData = new ClassInjectData(classPrototype); |
86 |
| - MVCInject.injectMapping.push(classInjectData); |
87 |
| - } |
88 |
| - |
89 |
| - let list = classInjectData.cmds[cmd]; |
90 |
| - if (!list) { |
91 |
| - list = new Array<(e: IStream) => void>(); |
92 |
| - classInjectData.cmds[cmd] = list; |
93 |
| - } |
94 |
| - if (list.indexOf(handle) == -1) { |
95 |
| - list.push(handle); |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 | 35 | private facade: IFacade;
|
100 | 36 | public constructor(facade: IFacade) {
|
101 | 37 | this.facade = facade;
|
102 | 38 | }
|
103 | 39 |
|
104 |
| - private getInjectClassByDef(classInjectData: ClassInjectData, property: string): new () => any { |
| 40 | + private getInjectClassByDef(classInjectData: InjectClassData, property: string): new () => any { |
105 | 41 | let fullClassName = classInjectData.getFullClassName();
|
106 | 42 | let dic = MVCInject.injectDefMapping[fullClassName];
|
107 | 43 | if (!dic) {
|
@@ -132,19 +68,16 @@ module mvc {
|
132 | 68 | public inject(target: IInjectable): IInjectable {
|
133 | 69 | let type = Object.getPrototypeOf(target);
|
134 | 70 | while (type) {
|
135 |
| - for (let item of MVCInject.injectMapping) { |
136 |
| - if (item.classPrototype == type) { |
137 |
| - this.doInject(item, target); |
138 |
| - break; |
139 |
| - } |
140 |
| - } |
| 71 | + let item=InjectAttribute.Get(type); |
| 72 | + if(item)this.doInject(item, target); |
| 73 | + |
141 | 74 | //循环找基类注入
|
142 | 75 | type = Object.getPrototypeOf(type);
|
143 | 76 | }
|
144 | 77 | return target;
|
145 | 78 | }
|
146 | 79 |
|
147 |
| - protected doInject(classInjectData: ClassInjectData, target: IInjectable) { |
| 80 | + protected doInject(classInjectData: InjectClassData, target: IInjectable) { |
148 | 81 | for (let key in classInjectData.propertys) {
|
149 | 82 | let cls = this.getInjectClassByDef(classInjectData, key);
|
150 | 83 | if (cls == null) {
|
@@ -199,15 +132,17 @@ module mvc {
|
199 | 132 | let ins = this.facade.getInjectLock(aliasName);
|
200 | 133 | if (!ins) {
|
201 | 134 | ins = Singleton.__GetOrCreateOneInstance(aliasName);
|
| 135 | + ins["_name"] = aliasName; |
| 136 | + |
| 137 | + //可注入 必须加锁 (先调用也是想让onRegister的时候 可以访问注入对像) |
202 | 138 | if (ins[MVCInject.INJECTABLE_FULLNAME]) {
|
203 |
| - ins = this.inject(ins); |
| 139 | + this.facade.__unSafeInjectInstance(ins); |
204 | 140 | }
|
| 141 | + |
205 | 142 | if (isMediator) {
|
206 |
| - ins["_name"] = aliasName; |
207 | 143 | this.facade.registerMediator(<IMediator>ins);
|
208 | 144 | }
|
209 | 145 | else if (isProxy) {
|
210 |
| - ins["_name"] = aliasName; |
211 | 146 | this.facade.registerProxy(<IProxy>ins);
|
212 | 147 | }
|
213 | 148 | }
|
|
0 commit comments