-
Notifications
You must be signed in to change notification settings - Fork 186
/
Quick.IOC.pas
888 lines (771 loc) · 28 KB
/
Quick.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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
{ ***************************************************************************
Copyright (c) 2016-2022 Kike Pérez
Unit : Quick.IoC
Description : IoC Dependency Injector
Author : Kike Pérez
Version : 1.0
Created : 19/10/2019
Modified : 19/01/2022
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
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 Quick.IoC;
{$i QuickLib.inc}
interface
uses
System.SysUtils,
RTTI,
{$IFDEF DEBUG_IOC}
Quick.Debug.Utils,
{$ENDIF}
System.TypInfo,
System.Generics.Collections,
Quick.Logger.Intf,
Quick.Options;
type
TActivatorDelegate<T> = reference to function: T;
TIocRegistration = class
type
TRegisterMode = (rmTransient, rmSingleton, rmScoped);
private
fName : string;
fRegisterMode : TRegisterMode;
fIntfInfo : PTypeInfo;
fImplementation : TClass;
fActivatorDelegate : TActivatorDelegate<TValue>;
public
constructor Create(const aName : string);
property Name : string read fName;
property IntfInfo : PTypeInfo read fIntfInfo write fIntfInfo;
property &Implementation : TClass read fImplementation write fImplementation;
function IsSingleton : Boolean;
function IsTransient : Boolean;
function IsScoped : Boolean;
function AsSingleton : TIocRegistration;
function AsTransient : TIocRegistration;
function AsScoped : TIocRegistration;
property ActivatorDelegate : TActivatorDelegate<TValue> read fActivatorDelegate write fActivatorDelegate;
end;
TIocRegistrationInterface = class(TIocRegistration)
private
fInstance : IInterface;
public
property Instance : IInterface read fInstance write fInstance;
end;
TIocRegistrationInstance = class(TIocRegistration)
private
fInstance : TObject;
public
property Instance : TObject read fInstance write fInstance;
end;
TIocRegistration<T> = record
private
fRegistration : TIocRegistration;
public
constructor Create(aRegistration : TIocRegistration);
function AsSingleton : TIocRegistration<T>;
function AsTransient : TIocRegistration<T>;
function AsScoped : TIocRegistration<T>;
function DelegateTo(aDelegate : TActivatorDelegate<T>) : TIocRegistration<T>;
end;
IIocRegistrator = interface
['{F3B79B15-2874-4B66-9B7F-06E2EBFED1AE}']
function GetKey(aPInfo : PTypeInfo; const aName : string = ''): string;
function RegisterType(aTypeInfo : PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration;
function RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration;
procedure Unregister(aTypeInfo : PTypeInfo; const aName : string = '');
end;
TIocRegistrator = class(TInterfacedObject,IIocRegistrator)
private
fDependencies : TDictionary<string,TIocRegistration>;
fDependencyOrder : TList<TIocRegistration>;
public
constructor Create;
destructor Destroy; override;
property Dependencies : TDictionary<string,TIocRegistration> read fDependencies write fDependencies;
property DependencyOrder : TList<TIocRegistration> read fDependencyOrder;
function IsRegistered<TInterface: IInterface; TImplementation: class>(const aName : string = '') : Boolean; overload;
function IsRegistered<T>(const aName : string = '') : Boolean; overload;
function GetKey(aPInfo : PTypeInfo; const aName : string = ''): string;
function RegisterType<TInterface: IInterface; TImplementation: class>(const aName : string = '') : TIocRegistration<TImplementation>; overload;
function RegisterType(aTypeInfo : PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration; overload;
function RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration; overload;
function RegisterInstance<T : class>(const aName : string = '') : TIocRegistration<T>; overload;
function RegisterInstance<TInterface : IInterface>(aInstance : TInterface; const aName : string = '') : TIocRegistration; overload;
function RegisterOptions<T : TOptions>(aOptions : T) : TIocRegistration<T>;
procedure Unregister<TInterface: IInterface>(const aName : string = ''); overload;
procedure Unregister(aTypeInfo : PTypeInfo; const aName : string = ''); overload;
end;
IIocContainer = interface
['{6A486E3C-C5E8-4BE5-8382-7B9BCCFC1BC3}']
function RegisterType(aInterface: PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration;
function RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration;
function Resolve(aServiceType: PTypeInfo; const aName : string = ''): TValue;
procedure Unregister(aTypeInfo : PTypeInfo; const aName : string = '');
procedure Build;
end;
IIocInjector = interface
['{F78E6BBC-2A95-41C9-B231-D05A586B4B49}']
end;
TIocInjector = class(TInterfacedObject,IIocInjector)
end;
IIocResolver = interface
['{B7C07604-B862-46B2-BF33-FF941BBE53CA}']
function Resolve(aServiceType: PTypeInfo; const aName : string = ''): TValue; overload;
end;
TIocResolver = class(TInterfacedObject,IIocResolver)
private
fRegistrator : TIocRegistrator;
fInjector : TIocInjector;
function CreateInstance(aClass : TClass) : TValue;
public
constructor Create(aRegistrator : TIocRegistrator; aInjector : TIocInjector);
function Resolve<T>(const aName : string = ''): T; overload;
function Resolve(aServiceType: PTypeInfo; const aName : string = ''): TValue; overload;
function ResolveAll<T>(const aName : string = '') : TList<T>;
end;
TTypedFactory<T : class, constructor> = class(TVirtualInterface)
private
fResolver : TIocResolver;
public
constructor Create(PIID: PTypeInfo; aResolver : TIocResolver);
procedure DoInvoke(Method: TRttiMethod; const Args: TArray<TValue>; out Result: TValue);
end;
IFactory<T> = interface
['{92D7AB4F-4C0A-4069-A821-B057E193DE65}']
function New : T;
end;
TSimpleFactory<T : class, constructor> = class(TInterfacedObject,IFactory<T>)
private
fResolver : TIocResolver;
public
constructor Create(aResolver : TIocResolver);
function New : T;
end;
TSimpleFactory<TInterface : IInterface; TImplementation : class, constructor> = class(TInterfacedObject,IFactory<TInterface>)
private
fResolver : TIocResolver;
public
constructor Create(aResolver : TIocResolver);
function New : TInterface;
end;
TIocContainer = class(TInterfacedObject,IIocContainer)
private
fRegistrator : TIocRegistrator;
fResolver : TIocResolver;
fInjector : TIocInjector;
fLogger : ILogger;
class var
GlobalInstance: TIocContainer;
protected
class constructor Create;
class destructor Destroy;
public
constructor Create;
destructor Destroy; override;
function IsRegistered<TInterface: IInterface; TImplementation: class>(const aName: string): Boolean; overload;
function IsRegistered<TInterface : IInterface>(const aName: string): Boolean; overload;
function RegisterType<TInterface: IInterface; TImplementation: class>(const aName : string = '') : TIocRegistration<TImplementation>; overload;
function RegisterType(aInterface: PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration; overload;
function RegisterInstance<T : class>(const aName: string = ''): TIocRegistration<T>; overload;
function RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration; overload;
function RegisterInstance<TInterface : IInterface>(aInstance : TInterface; const aName : string = '') : TIocRegistration; overload;
function RegisterOptions<T : TOptions>(aOptions : TOptions) : TIocRegistration<T>; overload;
function RegisterOptions<T : TOptions>(aOptions : TConfigureOptionsProc<T>) : TIocRegistration<T>; overload;
function Resolve<T>(const aName : string = ''): T; overload;
function Resolve(aServiceType: PTypeInfo; const aName : string = ''): TValue; overload;
function ResolveAll<T>(const aName : string = '') : TList<T>;
function AbstractFactory<T : class, constructor>(aClass : TClass) : T; overload;
function AbstractFactory<T : class, constructor> : T; overload;
function RegisterTypedFactory<TFactoryInterface : IInterface; TFactoryType : class, constructor>(const aName : string = '') : TIocRegistration<TTypedFactory<TFactoryType>>;
function RegisterSimpleFactory<TInterface : IInterface; TImplementation : class, constructor>(const aName : string = '') : TIocRegistration;
procedure Unregister<TInterface: IInterface>(const aName : string = ''); overload;
procedure Unregister(aInterface: PTypeInfo; const aName : string = ''); overload;
procedure Build;
end;
TIocServiceLocator = class
public
class function GetService<T> : T;
class function TryToGetService<T: IInterface>(aService : T) : Boolean;
end;
Name = class(TCustomAttribute)
private
fName: string;
public
constructor Create(aName: string);
property Name: String read fName;
end;
EIocRegisterError = class(Exception);
EIocResolverError = class(Exception);
EIocBuildError = class(Exception);
//singleton global instance
function GlobalContainer : TIocContainer;
function ServiceLocator : TIocServiceLocator;
implementation
function GlobalContainer: TIocContainer;
begin
Result := TIocContainer.GlobalInstance;
end;
function ServiceLocator : TIocServiceLocator;
begin
Result := TIocServiceLocator.Create;
end;
{ TIocRegistration }
constructor TIocRegistration.Create;
begin
fRegisterMode := TRegisterMode.rmTransient;
end;
function TIocRegistration.AsTransient: TIocRegistration;
begin
Result := Self;
fRegisterMode := TRegisterMode.rmTransient;
end;
function TIocRegistration.AsSingleton : TIocRegistration;
begin
Result := Self;
fRegisterMode := TRegisterMode.rmSingleton;
end;
function TIocRegistration.AsScoped: TIocRegistration;
begin
Result := Self;
fRegisterMode := TRegisterMode.rmScoped;
end;
function TIocRegistration.IsTransient: Boolean;
begin
Result := fRegisterMode = TRegisterMode.rmTransient;
end;
function TIocRegistration.IsSingleton: Boolean;
begin
Result := fRegisterMode = TRegisterMode.rmSingleton;
end;
function TIocRegistration.IsScoped: Boolean;
begin
Result := fRegisterMode = TRegisterMode.rmScoped;
end;
{ TIocContainer }
class constructor TIocContainer.Create;
begin
GlobalInstance := TIocContainer.Create;
end;
class destructor TIocContainer.Destroy;
begin
if GlobalInstance <> nil then GlobalInstance.Free;
inherited;
end;
function TIocContainer.AbstractFactory<T>(aClass: TClass): T;
begin
Result := fResolver.CreateInstance(aClass).AsType<T>;
end;
function TIocContainer.AbstractFactory<T> : T;
begin
Result := fResolver.CreateInstance(TClass(T)).AsType<T>;
end;
procedure TIocContainer.Build;
var
dependency : TIocRegistration;
begin
{$IFDEF DEBUG_IOC}
TDebugger.TimeIt(Self,'Build','Container dependencies building...');
{$ENDIF}
for dependency in fRegistrator.DependencyOrder do
begin
try
{$IFDEF DEBUG_IOC}
TDebugger.Trace(Self,'[Building container]: %s',[dependency.fIntfInfo.Name]);
{$ENDIF}
if dependency.IsSingleton then fResolver.Resolve(dependency.fIntfInfo,dependency.Name);
{$IFDEF DEBUG_IOC}
TDebugger.Trace(Self,'[Built container]: %s',[dependency.fIntfInfo.Name]);
{$ENDIF}
except
on E : Exception do raise EIocBuildError.CreateFmt('Build Error on "%s(%s)" dependency: %s!',[dependency.fImplementation.ClassName,dependency.Name,e.Message]);
end;
end;
end;
constructor TIocContainer.Create;
begin
fLogger := nil;
fRegistrator := TIocRegistrator.Create;
fInjector := TIocInjector.Create;
fResolver := TIocResolver.Create(fRegistrator,fInjector);
end;
destructor TIocContainer.Destroy;
begin
fInjector.Free;
fResolver.Free;
fRegistrator.Free;
fLogger := nil;
inherited;
end;
function TIocContainer.IsRegistered<TInterface, TImplementation>(const aName: string): Boolean;
begin
Result := fRegistrator.IsRegistered<TInterface,TImplementation>(aName);
end;
function TIocContainer.IsRegistered<TInterface>(const aName: string): Boolean;
begin
Result := fRegistrator.IsRegistered<TInterface>(aName);
end;
function TIocContainer.RegisterType<TInterface, TImplementation>(const aName: string): TIocRegistration<TImplementation>;
begin
Result := fRegistrator.RegisterType<TInterface, TImplementation>(aName);
end;
function TIocContainer.RegisterType(aInterface: PTypeInfo; aImplementation: TClass; const aName: string): TIocRegistration;
begin
Result := fRegistrator.RegisterType(aInterface,aImplementation,aName);
end;
procedure TIocContainer.Unregister<TInterface>(const aName : string = '');
begin
fRegistrator.Unregister<TInterface>(aName);
end;
procedure TIocContainer.Unregister(aInterface: PTypeInfo; const aName : string = '');
begin
fRegistrator.Unregister(aInterface, aName);
end;
function TIocContainer.RegisterInstance<T>(const aName: string): TIocRegistration<T>;
begin
Result := fRegistrator.RegisterInstance<T>(aName);
end;
function TIocContainer.RegisterTypedFactory<TFactoryInterface,TFactoryType>(const aName: string): TIocRegistration<TTypedFactory<TFactoryType>>;
begin
Result := fRegistrator.RegisterType<TFactoryInterface,TTypedFactory<TFactoryType>>(aName).DelegateTo(function : TTypedFactory<TFactoryType>
begin
Result := TTypedFactory<TFactoryType>.Create(TypeInfo(TFactoryInterface),fResolver);
end);
end;
function TIocContainer.RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration;
begin
Result := fRegistrator.RegisterInstance(aTypeInfo,aName);
end;
function TIocContainer.RegisterInstance<TInterface>(aInstance: TInterface; const aName: string): TIocRegistration;
begin
Result := fRegistrator.RegisterInstance<TInterface>(aInstance,aName);
end;
function TIocContainer.RegisterOptions<T>(aOptions: TOptions): TIocRegistration<T>;
begin
Result := fRegistrator.RegisterOptions<T>(aOptions).AsSingleton;
end;
function TIocContainer.RegisterOptions<T>(aOptions: TConfigureOptionsProc<T>): TIocRegistration<T>;
var
options : T;
begin
options := T.Create;
aOptions(options);
Result := Self.RegisterOptions<T>(options);
end;
function TIocContainer.RegisterSimpleFactory<TInterface, TImplementation>(const aName: string): TIocRegistration;
begin
Result := fRegistrator.RegisterInstance<IFactory<TInterface>>(TSimpleFactory<TInterface,TImplementation>.Create(fResolver),aName).AsSingleton;
end;
function TIocContainer.Resolve(aServiceType: PTypeInfo; const aName: string): TValue;
begin
Result := fResolver.Resolve(aServiceType,aName);
end;
function TIocContainer.Resolve<T>(const aName : string = ''): T;
begin
Result := fResolver.Resolve<T>(aName);
end;
function TIocContainer.ResolveAll<T>(const aName : string = ''): TList<T>;
begin
Result := fResolver.ResolveAll<T>(aName);
end;
{ TIocRegistrator }
constructor TIocRegistrator.Create;
begin
fDependencies := TDictionary<string,TIocRegistration>.Create;
fDependencyOrder := TList<TIocRegistration>.Create;
end;
destructor TIocRegistrator.Destroy;
var
i : Integer;
regs : TArray<TIocRegistration>;
begin
for i := fDependencyOrder.Count-1 downto 0 do
begin
if fDependencyOrder[i] <> nil then
begin
//free singleton instances not interfaced
if (fDependencyOrder[i] is TIocRegistrationInstance) and
(TIocRegistrationInstance(fDependencyOrder[i]).IsSingleton) then
TIocRegistrationInstance(fDependencyOrder[i]).Instance.Free;
fDependencyOrder[i].Free;
end;
end;
fDependencies.Free;
fDependencyOrder.Free;
inherited;
end;
function TIocRegistrator.GetKey(aPInfo : PTypeInfo; const aName : string = ''): string;
begin
{$IFDEF NEXTGEN}
{$IFDEF DELPHISYDNEY_UP}
Result := string(aPInfo.Name);
{$ELSE}
Result := aPInfo .Name.ToString;
{$ENDIF}
{$ELSE}
Result := string(aPInfo.Name);
{$ENDIF}
if not aName.IsEmpty then Result := Result + '.' + aName.ToLower;
end;
function TIocRegistrator.IsRegistered<TInterface, TImplementation>(const aName: string): Boolean;
var
key : string;
reg : TIocRegistration;
begin
Result := False;
key := GetKey(TypeInfo(TInterface),aName);
if fDependencies.TryGetValue(key,reg) then
begin
if reg.&Implementation = TImplementation then Result := True;
end
end;
function TIocRegistrator.IsRegistered<T>(const aName: string): Boolean;
var
key : string;
reg : TIocRegistration;
begin
Result := False;
key := GetKey(TypeInfo(T),aName);
if fDependencies.TryGetValue(key,reg) then
begin
if reg is TIocRegistrationInterface then Result := True
else if (reg is TIocRegistrationInstance) {and (TIocRegistrationInterface(reg).Instance <> nil)} then Result := True;
end
end;
function TIocRegistrator.RegisterInstance<T>(const aName: string): TIocRegistration<T>;
var
reg : TIocRegistration;
begin
reg := RegisterInstance(TypeInfo(T),aName);
Result := TIocRegistration<T>.Create(reg);
end;
function TIocRegistrator.RegisterInstance<TInterface>(aInstance: TInterface; const aName: string): TIocRegistration;
var
key : string;
tpinfo : PTypeInfo;
begin
tpinfo := TypeInfo(TInterface);
key := GetKey(tpinfo,aName);
if fDependencies.TryGetValue(key,Result) then
begin
if Result.&Implementation = tpinfo.TypeData.ClassType then raise EIocRegisterError.Create('Implementation is already registered!');
end
else
begin
Result := TIocRegistrationInterface.Create(aName);
Result.IntfInfo := tpinfo;
TIocRegistrationInterface(Result).Instance := aInstance;
//reg.Instance := T.Create;
fDependencies.Add(key,Result);
fDependencyOrder.Add(Result);
end;
end;
function TIocRegistrator.RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration;
var
key : string;
begin
key := GetKey(aTypeInfo,aName);
if fDependencies.TryGetValue(key,Result) then
begin
if Result.&Implementation = aTypeInfo.TypeData.ClassType then raise EIocRegisterError.Create('Implementation is already registered!');
end
else
begin
Result := TIocRegistrationInstance.Create(aName);
Result.IntfInfo := aTypeInfo;
Result.&Implementation := aTypeInfo.TypeData.ClassType;
//reg.Instance := T.Create;
fDependencies.Add(key,Result);
fDependencyOrder.Add(Result);
end;
end;
function TIocRegistrator.RegisterOptions<T>(aOptions: T): TIocRegistration<T>;
var
pInfo : PTypeInfo;
key : string;
reg : TIocRegistration;
begin
pInfo := TypeInfo(IOptions<T>);
key := GetKey(pInfo,'');
if fDependencies.TryGetValue(key,reg) then
begin
if reg.&Implementation = aOptions.ClassType then raise EIocRegisterError.Create('Implementation for this interface is already registered!');
end
else
begin
reg := TIocRegistrationInterface.Create('');
reg.IntfInfo := pInfo;
reg.&Implementation := aOptions.ClassType;
TIocRegistrationInterface(reg).Instance := TOptionValue<T>.Create(aOptions);
fDependencies.Add(key,reg);
fDependencyOrder.Add(reg);
end;
Result := TIocRegistration<T>.Create(reg);
end;
function TIocRegistrator.RegisterType<TInterface, TImplementation>(const aName: string): TIocRegistration<TImplementation>;
var
reg : TIocRegistration;
begin
reg := RegisterType(TypeInfo(TInterface),TImplementation,aName);
Result := TIocRegistration<TImplementation>.Create(reg);
end;
function TIocRegistrator.RegisterType(aTypeInfo : PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration;
var
key : string;
begin
key := GetKey(aTypeInfo,aName);
if fDependencies.TryGetValue(key,Result) then
begin
if Result.&Implementation = aImplementation then raise EIocRegisterError.Create('Implementation for this interface is already registered!')
else Key := key + '#' + TGUID.NewGuid.ToString;
end;
Result := TIocRegistrationInterface.Create(aName);
Result.IntfInfo := aTypeInfo;
Result.&Implementation := aImplementation;
fDependencies.Add(key,Result);
fDependencyOrder.Add(Result);
end;
procedure TIocRegistrator.Unregister<TInterface>(const aName : string);
begin
Unregister(TypeInfo(TInterface), aName);
end;
procedure TIocRegistrator.Unregister(aTypeInfo : PTypeInfo; const aName : string);
var
key: string;
vValue: TIocRegistration;
begin
key := GetKey(aTypeInfo, aName);
if fDependencies.TryGetValue(key,vValue) then
begin
if (vValue.IntfInfo = aTypeInfo) and (vValue.Name = aName) then
begin
if fDependencyOrder.Contains(vValue) then
fDependencyOrder.Remove(vValue);
fDependencies.Remove(key);
vValue.Free;
end;
end;
end;
{ TIocResolver }
constructor TIocResolver.Create(aRegistrator : TIocRegistrator; aInjector : TIocInjector);
begin
fRegistrator := aRegistrator;
fInjector := aInjector;
end;
function TIocResolver.CreateInstance(aClass: TClass): TValue;
var
ctx : TRttiContext;
rtype : TRttiType;
rmethod : TRttiMethod;
rParam : TRttiParameter;
value : TValue;
values : TArray<TValue>;
att: TCustomAttribute;
attname: string;
begin
Result := nil;
rtype := ctx.GetType(aClass);
if rtype = nil then Exit;
for rmethod in TRttiInstanceType(rtype).GetMethods do
begin
if rmethod.IsConstructor then
begin
//if create don't have parameters
if Length(rmethod.GetParameters) = 0 then
begin
Result := rmethod.Invoke(TRttiInstanceType(rtype).MetaclassType,[]);
Break;
end
else
begin
for rParam in rmethod.GetParameters do
begin
attname := EmptyStr;
for att in rParam.GetAttributes do
begin
if att is Name then
begin
attname := Name(att).Name;
Break;
end;
end;
value := Resolve(rParam.ParamType.Handle, attname);
values := values + [value];
end;
Result := rmethod.Invoke(TRttiInstanceType(rtype).MetaclassType,values);
Break;
end;
end;
end;
end;
function TIocResolver.Resolve(aServiceType: PTypeInfo; const aName : string = ''): TValue;
var
key : string;
reg : TIocRegistration;
intf : IInterface;
begin
Result := nil;
reg := nil;
key := fRegistrator.GetKey(aServiceType,aName);
{$IFDEF DEBUG_IOC}
TDebugger.Trace(Self,'Resolving dependency: %s',[key]);
{$ENDIF}
if not fRegistrator.Dependencies.TryGetValue(key,reg) then raise EIocResolverError.CreateFmt('Type "%s" not registered for IOC!',[aServiceType.Name]);
//if is singleton return already instance if exists
if reg.IsSingleton then
begin
if reg is TIocRegistrationInterface then
begin
if TIocRegistrationInterface(reg).Instance <> nil then
begin
if TIocRegistrationInterface(reg).Instance.QueryInterface(GetTypeData(aServiceType).Guid,intf) <> 0 then raise EIocResolverError.CreateFmt('Implementation for "%s" not registered!',[aServiceType.Name]);
TValue.Make(@intf,aServiceType,Result);
{$IFDEF DEBUG_IOC}
TDebugger.Trace(Self,'Resolved dependency: %s',[reg.fIntfInfo.Name]);
{$ENDIF}
Exit;
end;
end
else
begin
if TIocRegistrationInstance(reg).Instance <> nil then
begin
Result := TIocRegistrationInstance(reg).Instance;
{$IFDEF DEBUG_IOC}
TDebugger.Trace(Self,'Resolved dependency: %s',[reg.fIntfInfo.Name]);
{$ENDIF}
Exit;
end;
end;
end;
//instance not created yet
if reg.&Implementation = nil then raise EIocResolverError.CreateFmt('Implemention for "%s" not defined!',[aServiceType.Name]);
//use activator if assigned
if reg is TIocRegistrationInterface then
begin
{$IFDEF DEBUG_IOC}
TDebugger.Trace(Self,'Building dependency: %s',[reg.fIntfInfo.Name]);
{$ENDIF}
if Assigned(reg.ActivatorDelegate) then TIocRegistrationInterface(reg).Instance := reg.ActivatorDelegate().AsInterface
else TIocRegistrationInterface(reg).Instance := CreateInstance(reg.&Implementation).AsInterface;
if (TIocRegistrationInterface(reg).Instance = nil) or (TIocRegistrationInterface(reg).Instance.QueryInterface(GetTypeData(aServiceType).Guid,intf) <> 0) then raise EIocResolverError.CreateFmt('Implementation for "%s" not registered!',[aServiceType.Name]);
TValue.Make(@intf,aServiceType,Result);
end
else
begin
{$IFDEF DEBUG_IOC}
TDebugger.Trace(Self,'Building dependency: %s',[reg.fIntfInfo.Name]);
{$ENDIF}
if Assigned(reg.ActivatorDelegate) then TIocRegistrationInstance(reg).Instance := reg.ActivatorDelegate().AsObject
else
begin
TIocRegistrationInstance(reg).Instance := CreateInstance(reg.&Implementation).AsObject;
end;
Result := TIocRegistrationInstance(reg).Instance;
end;
{$IFDEF DEBUG_IOC}
TDebugger.Trace(Self,'Built dependency: %s',[reg.fIntfInfo.Name]);
{$ENDIF}
end;
function TIocResolver.Resolve<T>(const aName : string = ''): T;
var
pInfo : PTypeInfo;
begin
Result := Default(T);
pInfo := TypeInfo(T);
Result := Resolve(pInfo,aName).AsType<T>;
end;
function TIocResolver.ResolveAll<T>(const aName : string = '') : TList<T>;
var
pInfo : PTypeInfo;
reg : TIocRegistration;
begin
Result := TList<T>.Create;
pInfo := TypeInfo(T);
for reg in fRegistrator.fDependencyOrder do
begin
if reg.IntfInfo = pInfo then Self.Resolve(pInfo,aName);
end;
end;
{ TIocRegistration<T> }
function TIocRegistration<T>.AsScoped: TIocRegistration<T>;
begin
Result := Self;
fRegistration.AsScoped;
end;
function TIocRegistration<T>.AsSingleton: TIocRegistration<T>;
begin
Result := Self;
fRegistration.AsSingleton;
end;
function TIocRegistration<T>.AsTransient: TIocRegistration<T>;
begin
Result := Self;
fRegistration.AsTransient;
end;
constructor TIocRegistration<T>.Create(aRegistration: TIocRegistration);
begin
fRegistration := aRegistration;
end;
function TIocRegistration<T>.DelegateTo(aDelegate: TActivatorDelegate<T>): TIocRegistration<T>;
begin
Result := Self;
fRegistration.ActivatorDelegate := function: TValue
begin
Result := TValue.From<T>(aDelegate);
end;
end;
{ TTypedFactory<T> }
constructor TTypedFactory<T>.Create(PIID: PTypeInfo; aResolver : TIocResolver);
begin
inherited Create(PIID, DoInvoke);
fResolver := aResolver;
end;
procedure TTypedFactory<T>.DoInvoke(Method: TRttiMethod; const Args: TArray<TValue>; out Result: TValue);
begin
if CompareText(Method.Name,'New') <> 0 then raise Exception.Create('TTypedFactory needs a method "New"');
Result := fResolver.CreateInstance(TClass(T)).AsType<T>;
end;
{ TIocServiceLocator }
class function TIocServiceLocator.GetService<T> : T;
begin
Result := GlobalContainer.Resolve<T>;
end;
class function TIocServiceLocator.TryToGetService<T>(aService : T) : Boolean;
begin
Result := GlobalContainer.IsRegistered<T>('');
if Result then aService := GlobalContainer.Resolve<T>;
end;
{ TSimpleFactory<T> }
constructor TSimpleFactory<T>.Create(aResolver: TIocResolver);
begin
fResolver := aResolver;
end;
function TSimpleFactory<T>.New: T;
begin
Result := fResolver.CreateInstance(TClass(T)).AsType<T>;
end;
{ TSimpleFactory<TInterface, TImplementation> }
constructor TSimpleFactory<TInterface, TImplementation>.Create(aResolver: TIocResolver);
begin
fResolver := aResolver;
end;
function TSimpleFactory<TInterface, TImplementation>.New: TInterface;
begin
Result := fResolver.CreateInstance(TClass(TImplementation)).AsType<TInterface>;
end;
{ Name }
constructor Name.Create(aName: string);
begin
fName := aName;
end;
end.