-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathMVCBr.IoC.pas
814 lines (711 loc) · 22.6 KB
/
MVCBr.IoC.pas
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
{ *************************************************************************** }
{ }
{ Simple.IoC }
{ }
{ Copyright (C) 2013 Vincent Parrett }
{ }
{ http://www.finalbuilder.com }
{ }
{ }
{ *************************************************************************** }
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{ *************************************************************************** }
unit MVCBr.IoC;
/// A Simple IoC container. Does not do Dependency Injects.
interface
uses
Generics.Collections,
TypInfo,
Rtti,
MVCBr.Interf,
SysUtils;
{ .$I 'SimpleIoC.inc' }
{$DEFINE DELPHI_XE_UP }
type
TResolveResult = (Unknown, Success, InterfaceNotRegistered, ImplNotRegistered,
DeletegateFailedCreate);
TActivatorDelegate<TInterface: IMVCBrIOC> = reference to function: TInterface;
TMVCBrIoC = class
private
FRaiseIfNotFound: boolean;
type
TIoCRegistration<T: IMVCBrIOC> = class
private
FGuid: TGuid;
FName: string;
FIInterface: PTypeInfo;
FImplClass: TClass;
FActivatorDelegate: TActivatorDelegate<T>;
FIsSingleton: boolean;
[unsafe]
FInstance: IMVCBrIOC;
public
Destructor Destroy; override;
function ImplementClass(AImplements: TInterfacedClass)
: TIoCRegistration<T>;
function DelegateTo(const ADelegate: TActivatorDelegate<T>)
: TIoCRegistration<T>;
function Singleton(const AValue: boolean): TIoCRegistration<T>;
function isSingleton: boolean;
function asGuid: TGuid;
function asName: String;
function Name(AName: String): TIoCRegistration<T>;
function Guid(AGuid: TGuid): TIoCRegistration<T>;
end;
private
FContainerInfo: TDictionary<string, TObject>;
class var FDefault: TMVCBrIoC;
class var FReleased: boolean;
class procedure ClassDestroy;
protected
function GetInterfaceKey<TInterface>(const AName: string = ''): string;
function InternalResolve<TInterface: IMVCBrIOC>(out AInterface: TInterface;
const AName: string = ''): TResolveResult;
function InternalRegisterType<TInterface: IMVCBrIOC>(const Singleton
: boolean; const AImplementation: TClass;
const Delegate: TActivatorDelegate<TInterface>; const Name: string = '')
: TIoCRegistration<TInterface>;
public
constructor Create;
destructor Destroy; override;
procedure Release; virtual;
// Default Container
class function DefaultContainer: TMVCBrIoC;
{$IFDEF DELPHI_XE_UP}
// Exe's compiled with D2010 will crash when these are used.
// NOTES: The issue is due to the two generics included in the functions. The constaints also seem to be an issue.
procedure RegisterType<TInterface: IMVCBrIOC; TImplementation: class>
(const Name: string = ''); overload;
procedure RegisterType<TInterface: IMVCBrIOC; TImplementation: class>
(const Singleton: boolean; const Name: string = ''); overload;
{$ENDIF}
procedure RegisterType<TInterface: IMVCBrIOC>(const Delegate
: TActivatorDelegate<TInterface>; const Name: string = ''); overload;
procedure RegisterType<TInterface: IMVCBrIOC>(const Singleton: boolean;
const Delegate: TActivatorDelegate<TInterface>;
const Name: string = ''); overload;
function RegisterType<TInterface: IMVCBrIOC>(const AName: String)
: TIoCRegistration<TInterface>; overload;
procedure AttachInstance(AInstance: IMVCBrIOC);
procedure RevokeInstance(AInstance: IInterface);
procedure Revoke(AGuid: TGuid; ACount: Integer = 1);
// Register an instance as a signleton. If there is more than one instance that implements the interface
// then use the name parameter
procedure RegisterSingleton<TInterface: IMVCBrIOC>(const Instance
: TInterface; const Name: string = '');
// Resolution
function Resolve<TInterface: IMVCBrIOC>(const Name: string = '')
: TInterface;
function GetName(AGuid: TGuid): string;
function GetGuid(AName: string): TGuid;
function RegisterInterfaced<TInterface: IMVCBrIOC>(AII: TGuid;
AClass: TInterfacedClass; AName: String; bSingleton: boolean)
: TIoCRegistration<IMVCBrIOC>; overload;
// Returns true if we have such a service.
function HasService<T: IMVCBrIOC>: boolean;
// Empty the Container.. usefull for testing only!
procedure Clear;
property RaiseIfNotFound: boolean read FRaiseIfNotFound
write FRaiseIfNotFound;
property ContainerInfo: TDictionary<string, TObject> read FContainerInfo;
end;
EIoCException = class(Exception);
EIoCRegistrationException = class(EIoCException);
EIoCResolutionException = class(EIoCException);
// Makes sure virtual constructors are called correctly. Just using a class reference will not call the overriden constructor!
// See http://stackoverflow.com/questions/791069/how-can-i-create-an-delphi-object-from-a-class-reference-and-ensure-constructor
TClassActivator = class
private
class var FRttiCtx: TRttiContext;
public
constructor Create;
class function CreateInstance(const AClass: TClass): IInterface;
class procedure CallReleaseMethod(FInstance: TObject);
end;
function GetInterfaceIID(const I: IInterface; var IID: TGuid): boolean;
implementation
{ TActivator }
constructor TClassActivator.Create;
begin
TClassActivator.FRttiCtx := TRttiContext.Create;
end;
class function TClassActivator.CreateInstance(const AClass: TClass): IInterface;
var
rType: TRttiType;
method: TRttiMethod;
begin
result := nil;
rType := FRttiCtx.GetType(AClass);
if not(rType is TRttiInstanceType) then
exit;
for method in TRttiInstanceType(rType).GetMethods do
begin
if method.IsConstructor and (Length(method.GetParameters) = 0) then
begin
result := method.Invoke(TRttiInstanceType(rType).MetaclassType, [])
.AsInterface;
Break;
end;
end;
end;
class procedure TClassActivator.CallReleaseMethod(FInstance: TObject);
var
rType: TRttiType;
method: TRttiMethod;
II: IMVCBrIOC;
begin
if supports(FInstance, IMVCBrIOC, II) then
begin
II.Release;
II := nil;
exit;
end;
rType := FRttiCtx.GetType(FInstance.ClassType);
if not(rType is TRttiInstanceType) then
exit;
method := TRttiInstanceType(rType).GetMethod('release');
if assigned(method) and rType.IsInstance then
if (method.Visibility in [mvPublic, mvPublished]) and
(Length(method.GetParameters) = 0) then
begin
method.Invoke(FInstance, []);
end;
end;
function TMVCBrIoC.HasService<T>: boolean;
begin
result := Resolve<T> <> nil;
end;
{$IFDEF DELPHI_XE_UP}
procedure TMVCBrIoC.RegisterType<TInterface, TImplementation>
(const Name: string);
begin
InternalRegisterType<TInterface>(false, TImplementation, nil, name);
end;
procedure TMVCBrIoC.RegisterType<TInterface, TImplementation>(const Singleton
: boolean; const Name: string);
begin
InternalRegisterType<TInterface>(Singleton, TImplementation, nil, name);
end;
{$ENDIF}
procedure TMVCBrIoC.RegisterType<TInterface>(const Delegate
: TActivatorDelegate<TInterface>; const Name: string);
begin
InternalRegisterType<TInterface>(false, nil, Delegate, name);
end;
function TMVCBrIoC.InternalRegisterType<TInterface>(const Singleton: boolean;
const AImplementation: TClass; const Delegate: TActivatorDelegate<TInterface>;
const Name: string = ''): TIoCRegistration<TInterface>;
var
key: string;
pInfo: PTypeInfo;
rego: TIoCRegistration<TInterface>;
o: TObject;
newName: string;
newSingleton: boolean;
begin
result := nil;
newSingleton := Singleton;
newName := name;
pInfo := TypeInfo(TInterface);
{$IFDEF ANDROID}
if newName = '' then
key := pInfo.Name.ToString
else
key := pInfo.Name.ToString + '_' + newName;
{$ELSE}
if newName = '' then
key := string(pInfo.Name{$IFDEF LINUX}.ToString{$ENDIF})
else
key := string(pInfo.Name{$IFDEF LINUX}.ToString{$ENDIF}) + '_' + newName;
{$ENDIF}
key := LowerCase(key);
if not FContainerInfo.TryGetValue(key, o) then
begin
rego := TIoCRegistration<TInterface>.Create;
rego.FIInterface := pInfo;
rego.FActivatorDelegate := Delegate;
rego.FImplClass := AImplementation;
rego.FIsSingleton := newSingleton;
FContainerInfo.Add(key, rego);
end
else
begin
rego := TIoCRegistration<TInterface>(o);
// cannot replace a singleton that has already been instanciated.
if rego.FIsSingleton and (rego.FInstance <> nil) then
raise EIoCException.Create
(Format('An implementation for type %s with name %s is already registered with IoC',
[pInfo.Name, newName]));
rego.FIInterface := pInfo;
rego.FActivatorDelegate := Delegate;
rego.FImplClass := AImplementation;
rego.FIsSingleton := newSingleton;
FContainerInfo.AddOrSetValue(key, rego);
end;
result := rego;
end;
class procedure TMVCBrIoC.ClassDestroy;
begin
if FDefault <> nil then
FDefault.Free;
FDefault := nil;
end;
procedure TMVCBrIoC.Clear;
begin
FContainerInfo.Clear;
end;
constructor TMVCBrIoC.Create;
begin
FReleased := false;
FContainerInfo := TDictionary<string, TObject>.Create;
FRaiseIfNotFound := false;
end;
class function TMVCBrIoC.DefaultContainer: TMVCBrIoC;
begin
result := nil;
if FReleased then
exit;
if FDefault = nil then
FDefault := TMVCBrIoC.Create;
result := FDefault;
end;
destructor TMVCBrIoC.Destroy;
var
o: TObject;
rego: TIoCRegistration<IMVCBrIOC>;
begin
if FContainerInfo <> nil then
begin
for o in FContainerInfo.Values do
if o <> nil then
begin
rego := TIoCRegistration<IMVCBrIOC>(o);
if assigned(o) then
o.Free;
end;
FContainerInfo.Free;
FContainerInfo := nil;
end;
FReleased := true;
inherited;
end;
function TMVCBrIoC.GetInterfaceKey<TInterface>(const AName: string): string;
var
pInfo: PTypeInfo;
begin
// By default the key is the interface name unless otherwise found.
pInfo := TypeInfo(TInterface);
{$IFDEF ANDROID}
result := pInfo.Name.ToString;
{$ELSE}
result := string(pInfo.Name{$IFDEF LINUX}.ToString{$ENDIF});
{$ENDIF}
if (AName <> '') then
result := result + '_' + AName;
// All keys are stored in lower case form.
result := LowerCase(result);
end;
function TMVCBrIoC.GetGuid(AName: string): TGuid;
var
LName: string;
rogo: TIoCRegistration<IMVCBrIOC>;
begin
for LName in FContainerInfo.keys do
if LName.ToUpper.Equals(AName.ToUpper) then
begin
rogo := TIoCRegistration<IMVCBrIOC>(FContainerInfo.items[LName]);
result := rogo.FGuid;
end;
end;
function TMVCBrIoC.GetName(AGuid: TGuid): string;
var
LName: string;
rogo: TIoCRegistration<IMVCBrIOC>;
achei: string;
begin
achei := '';
for LName in FContainerInfo.keys do
begin
rogo := TIoCRegistration<IMVCBrIOC>(FContainerInfo.items[LName]);
if rogo.FGuid = AGuid then
begin
achei := rogo.FName;
Break;
end;
end;
result := achei;
end;
function TMVCBrIoC.InternalResolve<TInterface>(out AInterface: TInterface;
const AName: string): TResolveResult;
var
key: string;
errorMsg: string;
// container: TDictionary<string, TObject>;
registrationObj: TObject;
registration: TIoCRegistration<TInterface>;
resolvedInf: IInterface;
resolvedObj: TInterface;
bIsSingleton: boolean;
bInstanciate: boolean;
begin
AInterface := Default (TInterface);
result := TResolveResult.Unknown;
// Get the key for the interace we are resolving and locate the container for that key.
key := GetInterfaceKey<TInterface>(AName);
// container := FContainerInfo;
if not FContainerInfo.TryGetValue(key, registrationObj) then
begin
result := TResolveResult.InterfaceNotRegistered;
exit;
end;
// Get the interface registration class correctly.
registration := TIoCRegistration<TInterface>(registrationObj);
bIsSingleton := registration.FIsSingleton;
bInstanciate := true;
if bIsSingleton then
begin
// If a singleton was registered with this interface then check if it's already been instanciated.
if registration.FInstance <> nil then
begin
// Get AInterface as TInterface
if registration.FInstance.QueryInterface(GetTypeData(TypeInfo(TInterface))
.Guid, AInterface) <> 0 then
begin
result := TResolveResult.ImplNotRegistered;
exit;
end;
bInstanciate := false;
end;
end;
if bInstanciate then
begin
// If the instance hasn't been instanciated then we need to lock and instanciate
MonitorEnter(FContainerInfo);
try
// If we have a implementing class then used this to activate.
if registration.FImplClass <> nil then
resolvedInf := TClassActivator.CreateInstance(registration.FImplClass)
// Otherwise if there is a activate delegate use this to activate.
else if registration.FActivatorDelegate <> nil then
begin
resolvedInf := registration.FActivatorDelegate();
if resolvedInf = nil then
begin
result := TResolveResult.DeletegateFailedCreate;
exit;
end;
end;
// Get AInterface as TInterface
if resolvedInf.QueryInterface(GetTypeData(TypeInfo(TInterface)).Guid,
resolvedObj) <> 0 then
begin
result := TResolveResult.ImplNotRegistered;
exit;
end;
AInterface := resolvedObj;
if bIsSingleton then
begin
registration.FInstance := resolvedObj;
// Reset the registration to show the instance which was created.
FContainerInfo.AddOrSetValue(key, registration);
end;
finally
MonitorExit(FContainerInfo);
end;
end;
end;
procedure TMVCBrIoC.RegisterSingleton<TInterface>(const Instance: TInterface;
const Name: string);
var
key: string;
pInfo: PTypeInfo;
rego: TIoCRegistration<TInterface>;
o: TObject;
begin
pInfo := TypeInfo(TInterface);
key := GetInterfaceKey<TInterface>(name);
if not FContainerInfo.TryGetValue(key, o) then
begin
rego := TIoCRegistration<TInterface>.Create;
rego.FIInterface := pInfo;
rego.FActivatorDelegate := nil;
rego.FImplClass := nil;
rego.FIsSingleton := true;
rego.FInstance := Instance;
FContainerInfo.Add(key, rego);
end
else
raise EIoCException.Create
(Format('An implementation for type %s with name %s is already registered with IoC',
[pInfo.Name, name]));
end;
function TMVCBrIoC.RegisterInterfaced<TInterface>(AII: TGuid;
AClass: TInterfacedClass; AName: String; bSingleton: boolean)
: TIoCRegistration<IMVCBrIOC>;
var
Interf: PInterfaceEntry;
rego: TIoCRegistration<IMVCBrIOC>;
key: string;
begin
Interf := GetInterfaceEntry(AII);
key := GetInterfaceKey<TInterface>(AName);
rego := TIoCRegistration<IMVCBrIOC>.Create;
rego.FGuid := AII;
rego.FName := AName;
rego.FIInterface := nil;
rego.FImplClass := AClass;
rego.FIsSingleton := bSingleton;
rego.FInstance := nil;
rego.FActivatorDelegate := function: IMVCBrIOC
var
obj: TInterfacedObject;
begin
obj := AClass.Create;
supports(obj, AII, result);
end;
FContainerInfo.Add(key, rego);
result := rego;
end;
procedure TMVCBrIoC.RegisterType<TInterface>(const Singleton: boolean;
const Delegate: TActivatorDelegate<TInterface>; const Name: string);
begin
InternalRegisterType<TInterface>(Singleton, nil, Delegate, name);
end;
procedure TMVCBrIoC.Revoke(AGuid: TGuid; ACount: Integer = 1);
var
rogo: TIoCRegistration<IMVCBrIOC>;
n: Integer;
LName:string;
begin
n := 0;
for LName in FContainerInfo.keys do
begin
rogo := TIoCRegistration<IMVCBrIOC>(FContainerInfo.items[LName]);
if rogo.FGuid.ToString = AGuid.ToString then
begin
inc(n);
rogo.FInstance := nil;
if (ACount > 0) and (n >= ACount) then
exit;
end;
end;
end;
procedure TMVCBrIoC.RevokeInstance(AInstance: IInterface);
var
LName: string;
rogo: TIoCRegistration<IMVCBrIOC>;
achei: string;
AOrigem, ALocal: TObject;
begin
if FReleased then
exit;
achei := '';
if not assigned(AInstance) then
exit;
AOrigem := AInstance as TObject;
for LName in FContainerInfo.keys do
begin
rogo := TIoCRegistration<IMVCBrIOC>(FContainerInfo.items[LName]);
if assigned(rogo) and assigned(rogo.FInstance) and (rogo.FIsSingleton) then
begin
ALocal := rogo.FInstance as TObject;
if AOrigem.ClassName = ALocal.ClassName then
begin
rogo.FInstance := nil;
Break;
end;
end;
end;
end;
function TMVCBrIoC.Resolve<TInterface>(const Name: string = ''): TInterface;
var
resolveResult: TResolveResult;
errorMsg: string;
pInfo: PTypeInfo;
begin
pInfo := TypeInfo(TInterface);
resolveResult := InternalResolve<TInterface>(result, name);
// If we don't have a resolution and the caller wants an exception then throw one.
if (result = nil) and (FRaiseIfNotFound) then
begin
case resolveResult of
TResolveResult.Success:
;
TResolveResult.InterfaceNotRegistered:
errorMsg := Format('No implementation registered for type %s',
[pInfo.Name]);
TResolveResult.ImplNotRegistered:
errorMsg :=
Format('The Implementation registered for type %s does not actually implement %s',
[pInfo.Name, pInfo.Name]);
TResolveResult.DeletegateFailedCreate:
errorMsg :=
Format('The Implementation registered for type %s does not actually implement %s',
[pInfo.Name, pInfo.Name]);
else
// All other error types are treated as unknown until defined here.
errorMsg :=
Format('An Unknown Error has occurred for the resolution of the interface %s %s. This is either because a new error type isn''t being handled, '
+ 'or it''s an bug.', [pInfo.Name, name]);
end;
raise EIoCResolutionException.Create(errorMsg);
end;
end;
function GetInterfaceEntry2(const I: IInterface): PInterfaceEntry;
var
Instance: TObject;
InterfaceTable: PInterfaceTable;
j: Integer;
CurrentClass: TClass;
begin
Instance := I as TObject;
if assigned(Instance) then
begin
CurrentClass := Instance.ClassType;
while assigned(CurrentClass) do
begin
InterfaceTable := CurrentClass.GetInterfaceTable;
if assigned(InterfaceTable) then
for j := 0 to InterfaceTable.EntryCount - 1 do
begin
result := @InterfaceTable.Entries[j];
if result.IOffset <> 0 then
begin
if Pointer(NativeInt(Instance) + result^.IOffset) = Pointer(I) then
exit;
end;
// TODO: implement checking interface implemented via implements delegation
// see System.TObject.GetInterface/System.InvokeImplGetter
end;
CurrentClass := CurrentClass.ClassParent
end;
end;
result := nil;
end;
function GetInterfaceIID(const I: IInterface; var IID: TGuid): boolean;
var
InterfaceEntry: PInterfaceEntry;
begin
InterfaceEntry := GetInterfaceEntry2(I);
result := assigned(InterfaceEntry);
if result then
IID := InterfaceEntry.IID;
end;
function TMVCBrIoC.RegisterType<TInterface>(const AName: String)
: TIoCRegistration<TInterface>;
var
Interf: PInterfaceEntry;
key: string;
begin
key := GetInterfaceKey<TInterface>(AName);
result := TIoCRegistration<TInterface>.Create;
result.FName := AName;
result.FIInterface := nil;
result.FIsSingleton := true;
result.FInstance := nil;
FContainerInfo.Add(key, result);
end;
procedure TMVCBrIoC.Release;
var
o: TObject;
rogo: TIoCRegistration<IMVCBrIOC>;
resolvedObj: TObject;
begin
for o in FContainerInfo.Values do
begin
rogo := TIoCRegistration<IMVCBrIOC>(o);
if assigned(rogo.FInstance) then
rogo.FInstance.Release;
rogo.FInstance := nil;
end;
end;
procedure TMVCBrIoC.AttachInstance(AInstance: IMVCBrIOC);
var
o: TObject;
rogo: TIoCRegistration<IMVCBrIOC>;
resolvedObj: TObject;
begin
for o in FContainerInfo.Values do
begin
rogo := TIoCRegistration<IMVCBrIOC>(o);
resolvedObj := AInstance as TObject;
if (not assigned(rogo.FInstance)) and assigned(rogo.FImplClass) and
(sametext(rogo.FImplClass.ClassName, resolvedObj.ClassName)) and
(rogo.isSingleton) then
begin
rogo.FInstance := AInstance;
Break;
end;
end;
end;
{ TMVCBrIoC.TIoCRegistration<T> }
function TMVCBrIoC.TIoCRegistration<T>.asGuid: TGuid;
begin
result := FGuid;
end;
function TMVCBrIoC.TIoCRegistration<T>.asName: String;
begin
result := FName;
end;
function TMVCBrIoC.TIoCRegistration<T>.DelegateTo(const ADelegate
: TActivatorDelegate<T>): TIoCRegistration<T>;
begin
result := self;
FActivatorDelegate := ADelegate;
end;
destructor TMVCBrIoC.TIoCRegistration<T>.Destroy;
begin
if assigned(FInstance) then
FInstance.Release;
FInstance := nil;
inherited;
end;
function TMVCBrIoC.TIoCRegistration<T>.Guid(AGuid: TGuid): TIoCRegistration<T>;
begin
result := self;
FGuid := AGuid;
end;
function TMVCBrIoC.TIoCRegistration<T>.ImplementClass
(AImplements: TInterfacedClass): TIoCRegistration<T>;
begin
result := self;
FActivatorDelegate := function: T
var
obj: TInterfacedObject;
begin
obj := AImplements.Create;
supports(obj, FGuid, result);
end;
FImplClass := AImplements;
end;
function TMVCBrIoC.TIoCRegistration<T>.isSingleton: boolean;
begin
result := FIsSingleton;
end;
function TMVCBrIoC.TIoCRegistration<T>.Name(AName: String): TIoCRegistration<T>;
begin
result := self;
FName := AName;
end;
function TMVCBrIoC.TIoCRegistration<T>.Singleton(const AValue: boolean)
: TIoCRegistration<T>;
begin
FIsSingleton := AValue;
result := self;
end;
initialization
finalization
TMVCBrIoC.ClassDestroy;
end.