Skip to content

Commit

Permalink
Changed param order for RegisterType
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Apr 17, 2024
1 parent 563f184 commit d231ebf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
4 changes: 2 additions & 2 deletions samples/services_injection/MainControllerU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ TMyController = class(TMVCController)

[MVCPath('/people')]
[MVCHTTPMethod([httpPOST])]
function CreatePerson([MVCInject] OtherPeopleService: IPeopleService; [MVCFromBody] Person: TPerson): IMVCResponse;
function CreatePerson([MVCInject] PeopleService: IPeopleService; [MVCFromBody] Person: TPerson): IMVCResponse;

[MVCPath('/people/($ID)')]
[MVCHTTPMethod([httpPUT])]
Expand Down Expand Up @@ -86,7 +86,7 @@ constructor TMyController.Create(const PeopleService: IPeopleService);
LogI('PeopleService in constructor: ' + IntToHex(NativeUInt(Pointer(PeopleService))));
end;

function TMyController.CreatePerson(OtherPeopleService: IPeopleService; Person: TPerson): IMVCResponse;
function TMyController.CreatePerson(PeopleService: IPeopleService; Person: TPerson): IMVCResponse;
begin
LogI('Created ' + Person.FirstName + ' ' + Person.LastName);
Result := CreatedResponse('', 'Person created (' + Person.ToString + ')' );
Expand Down
2 changes: 1 addition & 1 deletion samples/services_injection/Services.RegistrationU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ implementation
procedure RegisterServices(Container: IMVCServiceContainer);
begin
Container.RegisterType(TPeopleService, IPeopleService);
Container.RegisterType(TConnectionService, IConnectionService, '', TRegistrationType.SingletonPerRequest)
Container.RegisterType(TConnectionService, IConnectionService, TRegistrationType.SingletonPerRequest)
end;

end.
26 changes: 21 additions & 5 deletions sources/MVCFramework.Container.pas
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
unit MVCFramework.Container;

{$I dmvcframework.inc}

interface

uses
Expand All @@ -19,7 +21,7 @@ TClassOfInterfacedObject = class of TInterfacedObject;

IMVCServiceContainer = interface
['{1BB3F4A8-DDA1-4526-981C-A0BF877CFFD5}']
function RegisterType(const aImplementation: TClassOfInterfacedObject; const aInterface: TGUID; const aName : string = ''; const aRegType: TRegistrationType = TRegistrationType.Transient): IMVCServiceContainer; overload;
function RegisterType(const aImplementation: TClassOfInterfacedObject; const aInterface: TGUID; const aRegType: TRegistrationType = TRegistrationType.Transient; const aName : string = ''): IMVCServiceContainer; overload;
procedure Build();
end;

Expand Down Expand Up @@ -68,7 +70,7 @@ TMVCServiceContainer = class(TInterfacedObject, IMVCServiceContainer, IMVCServ
constructor Create; virtual;
destructor Destroy; override;
public
function RegisterType(const aImplementation: TClassOfInterfacedObject; const aInterface: TGUID; const aName : string = ''; const aRegType: TRegistrationType = TRegistrationType.Transient): IMVCServiceContainer; overload;
function RegisterType(const aImplementation: TClassOfInterfacedObject; const aInterface: TGUID; const aRegType: TRegistrationType = TRegistrationType.Transient; const aName : string = ''): IMVCServiceContainer; overload;
function Resolve(const ServiceContainerResolver: IMVCServiceContainerResolver; const aTypeInfo: PTypeInfo; const aName: string = ''): IInterface; overload;
function ResolveEx(const ServiceContainerResolver: IMVCServiceContainerResolver; const aTypeInfo: PTypeInfo; const aName: string; out ServiceKey: String; out RegType: TRegistrationType): IInterface; overload;
procedure Build();
Expand Down Expand Up @@ -153,9 +155,10 @@ class function TMVCServiceContainer.GetKey(const aGUID: TGUID; const aName: Stri
end;

function TMVCServiceContainer.RegisterType(const aImplementation: TClassOfInterfacedObject; const aInterface: TGUID;
const aName: string; const aRegType: TRegistrationType): IMVCServiceContainer;
const aRegType: TRegistrationType; const aName: string): IMVCServiceContainer;
var
lReg: TRegistration;
lKey: string;
begin
if fBuilt then
begin
Expand All @@ -168,10 +171,23 @@ function TMVCServiceContainer.RegisterType(const aImplementation: TClassOfInterf
lReg.Clazz := aImplementation;
lReg.RttiType := TRttiUtils.GlContext.GetType(lReg.Clazz);
lReg.RegistrationType := aRegType;
if not fRegistry.TryAdd(GetKey(aInterface, aName), lReg) then
lKey := GetKey(aInterface, aName);
{$IF Defined(RIOORBETTER)}
if not fRegistry.TryAdd(lKey, lReg) then
begin
raise EMVCContainerError.CreateFmt('Cannot register duplicated service "%s"',[GetKey(aInterface, aName)]);
raise EMVCContainerError.CreateFmt('Cannot register duplicated service "%s"',[lKey]);
end;
{$ELSE}
if not fRegistry.ContainsKey(lKey) then
begin
fRegistry.Add(lKey, lReg)
end
else
begin
raise EMVCContainerError.CreateFmt('Cannot register duplicated service "%s"',[lKey]);
end;
{$ENDIF}

end
else
begin
Expand Down
12 changes: 6 additions & 6 deletions unittests/general/Several/InjectorTestU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ procedure TTestContainer.TestCascadeConstructorInjection;
begin
var lCont := NewMVCServiceContainer;
lCont.RegisterType(TServiceA, IServiceA);
lCont.RegisterType(TServiceB, IServiceB, '', TRegistrationType.SingletonPerRequest);
lCont.RegisterType(TServiceB, IServiceB, TRegistrationType.SingletonPerRequest);
lCont.RegisterType(TServiceC, IServiceC);
lCont.Build;

Expand Down Expand Up @@ -143,8 +143,8 @@ procedure TTestContainer.TestNotBuiltContainer;
procedure TTestContainer.TestSingleton;
begin
var lCont := NewMVCServiceContainer;
lCont.RegisterType(TServiceA, IServiceA, '', TRegistrationType.Singleton);
lCont.RegisterType(TServiceA, IServiceA, 'Svc1', TRegistrationType.Singleton);
lCont.RegisterType(TServiceA, IServiceA, TRegistrationType.Singleton);
lCont.RegisterType(TServiceA, IServiceA, TRegistrationType.Singleton, 'Svc1');
lCont.Build;

// 1° Request
Expand All @@ -168,8 +168,8 @@ procedure TTestContainer.TestSingleton;
procedure TTestContainer.TestSingletonPerRequest;
begin
var lCont := NewMVCServiceContainer
.RegisterType(TServiceA, IServiceA, '', TRegistrationType.SingletonPerRequest)
.RegisterType(TServiceA, IServiceA, 'Svc1', TRegistrationType.SingletonPerRequest);
.RegisterType(TServiceA, IServiceA, TRegistrationType.SingletonPerRequest)
.RegisterType(TServiceA, IServiceA, TRegistrationType.SingletonPerRequest, 'Svc1');
lCont.Build;

// 1° "request"
Expand All @@ -195,7 +195,7 @@ procedure TTestContainer.TestTransient;
begin
var lCont := NewMVCServiceContainer;
lCont.RegisterType(TServiceA, IServiceA);
lCont.RegisterType(TServiceA, IServiceA, 'Svc1');
lCont.RegisterType(TServiceA, IServiceA, TRegistrationType.Transient, 'Svc1');
lCont.Build;
var lResolver := NewServiceContainerResolver(lCont);
var l0 := lResolver.Resolve(TypeInfo(IServiceA));
Expand Down

0 comments on commit d231ebf

Please sign in to comment.