Ver Fonte

Merge pull request #136 from DiogoAiresCardoso/created_unregister

Created Unregister.
Exilon há 9 meses atrás
pai
commit
92e8f5d9fb
1 ficheiros alterados com 42 adições e 0 exclusões
  1. 42 0
      Quick.IOC.pas

+ 42 - 0
Quick.IOC.pas

@@ -100,6 +100,7 @@ type
     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 @@ type
     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
@@ -127,6 +130,7 @@ type
     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 @@ type
     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;
 
@@ -375,6 +381,17 @@ 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);
@@ -601,6 +618,31 @@ begin
   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);