Added 'Renderer' in TMVCController

This commit is contained in:
Daniele Teti 2017-02-08 11:42:05 +01:00
parent 9ef9ede924
commit 862f31a1ce
7 changed files with 98 additions and 30 deletions

View File

@ -45,8 +45,10 @@ uses
type type
TJSONValueHelper = class helper for TJSONValue TJSONValueHelper = class helper for TJSONValue
public public
function GetItem(const Index: Integer): TJSONValue;
function ToJSON: String; function ToJSON: String;
function Count: Integer; function Count: Integer;
property Items[const Index: Integer]: TJSONValue read GetItem;
end; end;
{$ENDIF} {$ENDIF}
@ -62,6 +64,11 @@ begin
Result := Size; Result := Size;
end; end;
function TJSONValueHelper.GetItem(const Index: Integer): TJSONValue;
begin
Result := Get(Index);
end;
function TJSONValueHelper.ToJSON: String; function TJSONValueHelper.ToJSON: String;
begin begin
Result := Self.ToString; Result := Self.ToString;

View File

@ -3,7 +3,7 @@ unit MVCFramework.Serializer.Commons;
interface interface
uses uses
System.Rtti, System.Classes, System.SysUtils; System.Rtti, System.Classes, System.SysUtils, System.Generics.Collections, MVCFramework.Serializer.Intf;
type type
TSerializerHelpers = class sealed TSerializerHelpers = class sealed
@ -33,6 +33,17 @@ type
end; end;
TMVCSerUnSerRegistry = class sealed
strict private
class var SStorage: TDictionary<string, IMVCSerUnSer>;
public
class function GetSerUnSer(aContentType: String): IMVCSerUnSer;
class procedure RegisterSerializer(aContentType: string; aMVCSerUnSer: IMVCSerUnSer);
class procedure UnRegisterSerializer(aContentType: string);
class constructor Create;
class destructor Destroy;
end;
implementation implementation
uses uses
@ -193,4 +204,34 @@ begin
end; end;
end; end;
{ TMVCSerUnSerRegistry }
class constructor TMVCSerUnSerRegistry.Create;
begin
SStorage := TDictionary<String, IMVCSerUnSer>.Create;
end;
class destructor TMVCSerUnSerRegistry.Destroy;
begin
SStorage.Free;
end;
class function TMVCSerUnSerRegistry.GetSerUnSer(
aContentType: String): IMVCSerUnSer;
begin
if not SStorage.TryGetValue(aContentType, Result) then
raise EMVCSerializationException.CreateFmt('Cannot find a suitable serializer for %s', [aContentType]);
end;
class procedure TMVCSerUnSerRegistry.RegisterSerializer(aContentType: string;
aMVCSerUnSer: IMVCSerUnSer);
begin
TMVCSerUnSerRegistry.SStorage.Add(aContentType, aMVCSerUnSer);
end;
class procedure TMVCSerUnSerRegistry.UnRegisterSerializer(aContentType: string);
begin
TMVCSerUnSerRegistry.SStorage.Remove(aContentType);
end;
end. end.

View File

@ -233,7 +233,7 @@ begin
for I := 0 to Arr.Count - 1 do for I := 0 to Arr.Count - 1 do
begin begin
list.Add(Mapper.JSONObjectToObject(cref, list.Add(Mapper.JSONObjectToObject(cref,
Arr.Get(I) as TJSONObject)); Arr.Items[I] as TJSONObject));
end; end;
end end
else // Ezequiel J. Müller convert regular list else // Ezequiel J. Müller convert regular list
@ -248,11 +248,11 @@ begin
for ListParam in ListMethod.GetParameters do for ListParam in ListMethod.GetParameters do
case ListParam.ParamType.TypeKind of case ListParam.ParamType.TypeKind of
tkInteger, tkInt64: tkInteger, tkInt64:
ListItem := StrToIntDef(Arr.Get(I).Value, 0); ListItem := StrToIntDef(Arr.Items[I].Value, 0);
tkFloat: tkFloat:
ListItem := TJSONNumber(Arr.Get(I).Value).AsDouble; ListItem := TJSONNumber(Arr.Items[I].Value).AsDouble;
tkString, tkLString, tkWString, tkUString: tkString, tkLString, tkWString, tkUString:
ListItem := Arr.Get(I).Value; ListItem := Arr.Items[I].Value;
end; end;
if not ListItem.IsEmpty then if not ListItem.IsEmpty then
@ -597,7 +597,7 @@ begin
lJArr := TJSONArray(lJValue); lJArr := TJSONArray(lJValue);
for I := 0 to lJArr.Count - 1 do for I := 0 to lJArr.Count - 1 do
begin begin
AList.Add(JSONObjectToObject(AClazz, lJArr.Get(I) as TJSONObject)); AList.Add(JSONObjectToObject(AClazz, lJArr.Items[I] as TJSONObject));
end; end;
finally finally
lJValue.Free; lJValue.Free;
@ -637,4 +637,12 @@ begin
end; end;
end; end;
initialization
TMVCSerUnSerRegistry.RegisterSerializer('application/json', TMVCJSONSerUnSer.Create);
finalization
TMVCSerUnSerRegistry.UnRegisterSerializer('application/json');
end. end.

View File

@ -65,7 +65,7 @@ uses
, ReqMulti {Delphi XE4 (all update) and XE5 (with no update) dont contains this unit. Look for the bug in QC} , ReqMulti {Delphi XE4 (all update) and XE5 (with no update) dont contains this unit. Look for the bug in QC}
, LoggerPro , LoggerPro
, MVCFramework.DuckTyping , MVCFramework.DuckTyping
, MVCFramework.Patches; , MVCFramework.Patches, MVCFramework.Serializer.Intf;
type type
TDMVCSerializationType = TSerializationType; TDMVCSerializationType = TSerializationType;
@ -348,6 +348,7 @@ type
FContext: TWebContext; FContext: TWebContext;
FResponseStream: TStringBuilder; FResponseStream: TStringBuilder;
FContentCharset: string; FContentCharset: string;
FRenderer: IMVCSerUnSer;
procedure SetContext(const Value: TWebContext); procedure SetContext(const Value: TWebContext);
procedure SetWebSession(const Value: TWebSession); procedure SetWebSession(const Value: TWebSession);
procedure SetContentType(const Value: string); procedure SetContentType(const Value: string);
@ -355,11 +356,7 @@ type
function GetWebSession: TWebSession; function GetWebSession: TWebSession;
function GetContentCharset: string; function GetContentCharset: string;
procedure SetContentCharset(const Value: string); procedure SetContentCharset(const Value: string);
// procedure Render<T: class>(ACollection: TObjectList<T>; AInstanceOwner: boolean; function GetRenderer: IMVCSerUnSer;
// AJSONObjectActionProc: TJSONObjectActionProc; ASerializationType: TSerializationType); overload;
// procedure Render<T: class>(ACollection: TObjectList<T>; AInstanceOwner: boolean;
// AJSONObjectActionProc: TJSONObjectActionProc; ASerializationType: TSerializationType);
protected const protected const
CLIENTID_KEY = '__clientid'; CLIENTID_KEY = '__clientid';
protected protected
@ -463,7 +460,7 @@ type
property Config: TMVCConfig read GetMVCConfig; property Config: TMVCConfig read GetMVCConfig;
property StatusCode: UInt16 read GetStatusCode write SetStatusCode; property StatusCode: UInt16 read GetStatusCode write SetStatusCode;
property Renderer: IMVCSerUnSer read GetRenderer;
public public
// property ViewCache: TViewCache read FViewCache write SetViewCache; // property ViewCache: TViewCache read FViewCache write SetViewCache;
procedure PushJSONToView(const AModelName: string; AModel: TJSONValue); procedure PushJSONToView(const AModelName: string; AModel: TJSONValue);
@ -650,7 +647,7 @@ uses
IdHTTPWebBrokerBridge, IdHTTPWebBrokerBridge,
MVCFramework.MessagingController, MVCFramework.MessagingController,
Web.WebReq, Web.WebReq,
MVCFramework.SysControllers; MVCFramework.SysControllers, MVCFramework.Serializer.Commons;
const const
ALLOWED_TYPED_ACTION_PARAMETERS_TYPES = ALLOWED_TYPED_ACTION_PARAMETERS_TYPES =
@ -1994,6 +1991,15 @@ begin
end; end;
end; end;
function TMVCController.GetRenderer: IMVCSerUnSer;
begin
if FRenderer = nil then
begin
FRenderer := TMVCSerUnSerRegistry.GetSerUnSer(ContentType);
end;
Result := FRenderer;
end;
function TMVCController.GetWebSession: TWebSession; function TMVCController.GetWebSession: TWebSession;
begin begin
Result := FContext.Session; Result := FContext.Session;

View File

@ -37,7 +37,7 @@ uses
type type
TTestJSONSerializer = class(TMVCSerUnSerTestCase) TTestJSONSerializer = class(TMVCSerUnSerTestCase)
protected protected
procedure Setup; override; procedure SetUp; override;
published published
procedure TestSerUnSerObject; override; procedure TestSerUnSerObject; override;
procedure TestSerUnSerObjectList; override; procedure TestSerUnSerObjectList; override;
@ -52,7 +52,7 @@ uses BOs, MVCFramework.Serializer.JSON, MVCFramework.DuckTyping;
{ TTestJSONSerializer } { TTestJSONSerializer }
procedure TTestJSONSerializer.Setup; procedure TTestJSONSerializer.SetUp;
begin begin
SetSerUnSer(TMVCJSONSerUnSer.Create); SetSerUnSer(TMVCJSONSerUnSer.Create);
end; end;

View File

@ -36,7 +36,10 @@ uses
TestServerControllerPrivateU in 'TestServerControllerPrivateU.pas', TestServerControllerPrivateU in 'TestServerControllerPrivateU.pas',
AuthHandlersU in 'AuthHandlersU.pas', AuthHandlersU in 'AuthHandlersU.pas',
MVCFramework.Patches in '..\..\sources\MVCFramework.Patches.pas', MVCFramework.Patches in '..\..\sources\MVCFramework.Patches.pas',
ObjectsMappers in '..\..\sources\ObjectsMappers.pas'; ObjectsMappers in '..\..\sources\ObjectsMappers.pas',
MVCFramework.Serializer.Commons in '..\..\sources\MVCFramework.Serializer.Commons.pas',
MVCFramework.Serializer.Intf in '..\..\sources\MVCFramework.Serializer.Intf.pas',
MVCFramework.Serializer.JSON in '..\..\sources\MVCFramework.Serializer.JSON.pas';
{$R *.res} {$R *.res}

View File

@ -132,6 +132,9 @@
<DCCReference Include="AuthHandlersU.pas"/> <DCCReference Include="AuthHandlersU.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.Patches.pas"/> <DCCReference Include="..\..\sources\MVCFramework.Patches.pas"/>
<DCCReference Include="..\..\sources\ObjectsMappers.pas"/> <DCCReference Include="..\..\sources\ObjectsMappers.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.Serializer.Commons.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.Serializer.Intf.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.Serializer.JSON.pas"/>
<None Include="..\..\sources\dmvcframework.inc"/> <None Include="..\..\sources\dmvcframework.inc"/>
<None Include="..\..\sources\dmvcframeworkbuildconsts.inc"/> <None Include="..\..\sources\dmvcframeworkbuildconsts.inc"/>
<BuildConfiguration Include="Release"> <BuildConfiguration Include="Release">
@ -215,16 +218,7 @@
<Overwrite>true</Overwrite> <Overwrite>true</Overwrite>
</Platform> </Platform>
</DeployFile> </DeployFile>
<DeployClass Name="DependencyModule"> <DeployClass Name="ProjectiOSDeviceResourceRules"/>
<Platform Name="Win32">
<Operation>0</Operation>
<Extensions>.dll;.bpl</Extensions>
</Platform>
<Platform Name="OSX32">
<Operation>1</Operation>
<Extensions>.dylib</Extensions>
</Platform>
</DeployClass>
<DeployClass Name="ProjectOSXResource"> <DeployClass Name="ProjectOSXResource">
<Platform Name="OSX32"> <Platform Name="OSX32">
<RemoteDir>Contents\Resources</RemoteDir> <RemoteDir>Contents\Resources</RemoteDir>
@ -564,7 +558,16 @@
<Operation>1</Operation> <Operation>1</Operation>
</Platform> </Platform>
</DeployClass> </DeployClass>
<DeployClass Name="ProjectiOSDeviceResourceRules"/> <DeployClass Name="DependencyModule">
<Platform Name="Win32">
<Operation>0</Operation>
<Extensions>.dll;.bpl</Extensions>
</Platform>
<Platform Name="OSX32">
<Operation>1</Operation>
<Extensions>.dylib</Extensions>
</Platform>
</DeployClass>
<ProjectRoot Platform="iOSDevice64" Name="$(PROJECTNAME).app"/> <ProjectRoot Platform="iOSDevice64" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/> <ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/> <ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/>