From db8ce22b0df86e384fd4d435fbfae662637375d0 Mon Sep 17 00:00:00 2001 From: Diogo Aires Date: Fri, 4 Oct 2024 08:55:20 -0300 Subject: [PATCH 1/2] 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. --- Quick.IOC.pas | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Quick.IOC.pas b/Quick.IOC.pas index 5c46a95..2b5027a 100644 --- a/Quick.IOC.pas +++ b/Quick.IOC.pas @@ -100,6 +100,7 @@ TIocRegistration = record 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) @@ -120,6 +121,8 @@ TIocRegistrator = class(TInterfacedObject,IIocRegistrator) function RegisterInstance(const aName : string = '') : TIocRegistration; overload; function RegisterInstance(aInstance : TInterface; const aName : string = '') : TIocRegistration; overload; function RegisterOptions(aOptions : T) : TIocRegistration; + procedure Unregister(const aName : string = ''); overload; + procedure Unregister(aTypeInfo : PTypeInfo; const aName : string = ''); overload; end; IIocContainer = interface @@ -127,6 +130,7 @@ TIocRegistrator = class(TInterfacedObject,IIocRegistrator) 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; @@ -214,6 +218,8 @@ TIocContainer = class(TInterfacedObject,IIocContainer) function AbstractFactory : T; overload; function RegisterTypedFactory(const aName : string = '') : TIocRegistration>; function RegisterSimpleFactory(const aName : string = '') : TIocRegistration; + procedure Unregister(const aName : string = ''); overload; + procedure Unregister(aInterface: PTypeInfo; const aName : string = ''); overload; procedure Build; end; @@ -375,6 +381,17 @@ function TIocContainer.RegisterType(aInterface: PTypeInfo; aImplementation: TCla Result := fRegistrator.RegisterType(aInterface,aImplementation,aName); end; +procedure TIocContainer.Unregister(const aName : string = ''); +begin + fRegistrator.Unregister(aName); +end; + +procedure TIocContainer.Unregister(aInterface: PTypeInfo; const aName : string = ''); +begin + fRegistrator.Unregister(aInterface, aName); +end; + + function TIocContainer.RegisterInstance(const aName: string): TIocRegistration; begin Result := fRegistrator.RegisterInstance(aName); @@ -601,6 +618,28 @@ function TIocRegistrator.RegisterType(aTypeInfo : PTypeInfo; aImplementation : T fDependencyOrder.Add(Result); end; +procedure TIocRegistrator.Unregister(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 + fDependencies.Remove(key); + vValue.Free; + end; + end; +end; + { TIocResolver } constructor TIocResolver.Create(aRegistrator : TIocRegistrator; aInjector : TIocInjector); From f1c27995153d0e548c36aaf99a14296fad9b819a Mon Sep 17 00:00:00 2001 From: Diogo Aires Date: Fri, 4 Oct 2024 13:41:46 -0300 Subject: [PATCH 2/2] Reference removed also of fDependencyOrder, because had occured error of "invalid pointer operation" on destroy do IOC. --- Quick.IOC.pas | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Quick.IOC.pas b/Quick.IOC.pas index 2b5027a..663e180 100644 --- a/Quick.IOC.pas +++ b/Quick.IOC.pas @@ -634,10 +634,13 @@ procedure TIocRegistrator.Unregister(aTypeInfo : PTypeInfo; const aName : string 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 }