diff --git a/Quick.IOC.pas b/Quick.IOC.pas index 5c46a95..663e180 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,31 @@ 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 + if fDependencyOrder.Contains(vValue) then + fDependencyOrder.Remove(vValue); + fDependencies.Remove(key); + vValue.Free; + end; + end; + +end; + { TIocResolver } constructor TIocResolver.Create(aRegistrator : TIocRegistrator; aInjector : TIocInjector);