Skip to content

Commit db8ce22

Browse files
author
Diogo Aires
committed
This commit being to create unregister on Container and Registrator classes.
The reason this it to be for use in unitary test, where has much cenaries of validations through of mocks.
1 parent 69727a1 commit db8ce22

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Quick.IOC.pas

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ TIocRegistration<T> = record
100100
function GetKey(aPInfo : PTypeInfo; const aName : string = ''): string;
101101
function RegisterType(aTypeInfo : PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration;
102102
function RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration;
103+
procedure Unregister(aTypeInfo : PTypeInfo; const aName : string = '');
103104
end;
104105

105106
TIocRegistrator = class(TInterfacedObject,IIocRegistrator)
@@ -120,13 +121,16 @@ TIocRegistrator = class(TInterfacedObject,IIocRegistrator)
120121
function RegisterInstance<T : class>(const aName : string = '') : TIocRegistration<T>; overload;
121122
function RegisterInstance<TInterface : IInterface>(aInstance : TInterface; const aName : string = '') : TIocRegistration; overload;
122123
function RegisterOptions<T : TOptions>(aOptions : T) : TIocRegistration<T>;
124+
procedure Unregister<TInterface: IInterface>(const aName : string = ''); overload;
125+
procedure Unregister(aTypeInfo : PTypeInfo; const aName : string = ''); overload;
123126
end;
124127

125128
IIocContainer = interface
126129
['{6A486E3C-C5E8-4BE5-8382-7B9BCCFC1BC3}']
127130
function RegisterType(aInterface: PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration;
128131
function RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration;
129132
function Resolve(aServiceType: PTypeInfo; const aName : string = ''): TValue;
133+
procedure Unregister(aTypeInfo : PTypeInfo; const aName : string = '');
130134
procedure Build;
131135
end;
132136

@@ -214,6 +218,8 @@ TIocContainer = class(TInterfacedObject,IIocContainer)
214218
function AbstractFactory<T : class, constructor> : T; overload;
215219
function RegisterTypedFactory<TFactoryInterface : IInterface; TFactoryType : class, constructor>(const aName : string = '') : TIocRegistration<TTypedFactory<TFactoryType>>;
216220
function RegisterSimpleFactory<TInterface : IInterface; TImplementation : class, constructor>(const aName : string = '') : TIocRegistration;
221+
procedure Unregister<TInterface: IInterface>(const aName : string = ''); overload;
222+
procedure Unregister(aInterface: PTypeInfo; const aName : string = ''); overload;
217223
procedure Build;
218224
end;
219225

@@ -375,6 +381,17 @@ function TIocContainer.RegisterType(aInterface: PTypeInfo; aImplementation: TCla
375381
Result := fRegistrator.RegisterType(aInterface,aImplementation,aName);
376382
end;
377383

384+
procedure TIocContainer.Unregister<TInterface>(const aName : string = '');
385+
begin
386+
fRegistrator.Unregister<TInterface>(aName);
387+
end;
388+
389+
procedure TIocContainer.Unregister(aInterface: PTypeInfo; const aName : string = '');
390+
begin
391+
fRegistrator.Unregister(aInterface, aName);
392+
end;
393+
394+
378395
function TIocContainer.RegisterInstance<T>(const aName: string): TIocRegistration<T>;
379396
begin
380397
Result := fRegistrator.RegisterInstance<T>(aName);
@@ -601,6 +618,28 @@ function TIocRegistrator.RegisterType(aTypeInfo : PTypeInfo; aImplementation : T
601618
fDependencyOrder.Add(Result);
602619
end;
603620

621+
procedure TIocRegistrator.Unregister<TInterface>(const aName : string);
622+
begin
623+
Unregister(TypeInfo(TInterface), aName);
624+
end;
625+
626+
procedure TIocRegistrator.Unregister(aTypeInfo : PTypeInfo; const aName : string);
627+
var
628+
key: string;
629+
vValue: TIocRegistration;
630+
begin
631+
key := GetKey(aTypeInfo, aName);
632+
633+
if fDependencies.TryGetValue(key,vValue) then
634+
begin
635+
if (vValue.IntfInfo = aTypeInfo) and (vValue.Name = aName) then
636+
begin
637+
fDependencies.Remove(key);
638+
vValue.Free;
639+
end;
640+
end;
641+
end;
642+
604643
{ TIocResolver }
605644

606645
constructor TIocResolver.Create(aRegistrator : TIocRegistrator; aInjector : TIocInjector);

0 commit comments

Comments
 (0)