Skip to content

Commit

Permalink
Merge pull request #136 from DiogoAiresCardoso/created_unregister
Browse files Browse the repository at this point in the history
Created Unregister.
  • Loading branch information
exilon authored Oct 15, 2024
2 parents 69727a1 + f1c2799 commit 92e8f5d
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Quick.IOC.pas
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ TIocRegistration<T> = 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)
Expand All @@ -120,13 +121,16 @@ TIocRegistrator = class(TInterfacedObject,IIocRegistrator)
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
['{6A486E3C-C5E8-4BE5-8382-7B9BCCFC1BC3}']
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;

Expand Down Expand Up @@ -214,6 +218,8 @@ TIocContainer = class(TInterfacedObject,IIocContainer)
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;

Expand Down Expand Up @@ -375,6 +381,17 @@ function TIocContainer.RegisterType(aInterface: PTypeInfo; aImplementation: TCla
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);
Expand Down Expand Up @@ -601,6 +618,31 @@ function TIocRegistrator.RegisterType(aTypeInfo : PTypeInfo; aImplementation : T
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);
Expand Down

0 comments on commit 92e8f5d

Please sign in to comment.