Browse Source

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.

Diogo Aires 9 months ago
parent
commit
db8ce22b0d
1 changed files with 39 additions and 0 deletions
  1. 39 0
      Quick.IOC.pas

+ 39 - 0
Quick.IOC.pas

@@ -100,6 +100,7 @@ type
     function GetKey(aPInfo : PTypeInfo; const aName : string = ''): string;
     function GetKey(aPInfo : PTypeInfo; const aName : string = ''): string;
     function RegisterType(aTypeInfo : PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration;
     function RegisterType(aTypeInfo : PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration;
     function RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration;
     function RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration;
+    procedure Unregister(aTypeInfo : PTypeInfo; const aName : string = '');
   end;
   end;
 
 
   TIocRegistrator = class(TInterfacedObject,IIocRegistrator)
   TIocRegistrator = class(TInterfacedObject,IIocRegistrator)
@@ -120,6 +121,8 @@ type
     function RegisterInstance<T : class>(const aName : string = '') : TIocRegistration<T>; overload;
     function RegisterInstance<T : class>(const aName : string = '') : TIocRegistration<T>; overload;
     function RegisterInstance<TInterface : IInterface>(aInstance : TInterface; const aName : string = '') : TIocRegistration; overload;
     function RegisterInstance<TInterface : IInterface>(aInstance : TInterface; const aName : string = '') : TIocRegistration; overload;
     function RegisterOptions<T : TOptions>(aOptions : T) : TIocRegistration<T>;
     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;
   end;
 
 
   IIocContainer = interface
   IIocContainer = interface
@@ -127,6 +130,7 @@ type
     function RegisterType(aInterface: PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration;
     function RegisterType(aInterface: PTypeInfo; aImplementation : TClass; const aName : string = '') : TIocRegistration;
     function RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration;
     function RegisterInstance(aTypeInfo : PTypeInfo; const aName : string = '') : TIocRegistration;
     function Resolve(aServiceType: PTypeInfo; const aName : string = ''): TValue;
     function Resolve(aServiceType: PTypeInfo; const aName : string = ''): TValue;
+    procedure Unregister(aTypeInfo : PTypeInfo; const aName : string = '');
     procedure Build;
     procedure Build;
   end;
   end;
 
 
@@ -214,6 +218,8 @@ type
     function AbstractFactory<T : class, constructor> : T; overload;
     function AbstractFactory<T : class, constructor> : T; overload;
     function RegisterTypedFactory<TFactoryInterface : IInterface; TFactoryType : class, constructor>(const aName : string = '') : TIocRegistration<TTypedFactory<TFactoryType>>;
     function RegisterTypedFactory<TFactoryInterface : IInterface; TFactoryType : class, constructor>(const aName : string = '') : TIocRegistration<TTypedFactory<TFactoryType>>;
     function RegisterSimpleFactory<TInterface : IInterface; TImplementation : class, constructor>(const aName : string = '') : TIocRegistration;
     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;
     procedure Build;
   end;
   end;
 
 
@@ -375,6 +381,17 @@ begin
   Result := fRegistrator.RegisterType(aInterface,aImplementation,aName);
   Result := fRegistrator.RegisterType(aInterface,aImplementation,aName);
 end;
 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>;
 function TIocContainer.RegisterInstance<T>(const aName: string): TIocRegistration<T>;
 begin
 begin
   Result := fRegistrator.RegisterInstance<T>(aName);
   Result := fRegistrator.RegisterInstance<T>(aName);
@@ -601,6 +618,28 @@ begin
   fDependencyOrder.Add(Result);
   fDependencyOrder.Add(Result);
 end;
 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
+      fDependencies.Remove(key);
+      vValue.Free;
+    end;
+  end;
+end;
+
 { TIocResolver }
 { TIocResolver }
 
 
 constructor TIocResolver.Create(aRegistrator : TIocRegistrator; aInjector : TIocInjector);
 constructor TIocResolver.Create(aRegistrator : TIocRegistrator; aInjector : TIocInjector);