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
TJSONValueHelper = class helper for TJSONValue
public
function GetItem(const Index: Integer): TJSONValue;
function ToJSON: String;
function Count: Integer;
property Items[const Index: Integer]: TJSONValue read GetItem;
end;
{$ENDIF}
@ -62,6 +64,11 @@ begin
Result := Size;
end;
function TJSONValueHelper.GetItem(const Index: Integer): TJSONValue;
begin
Result := Get(Index);
end;
function TJSONValueHelper.ToJSON: String;
begin
Result := Self.ToString;

View File

@ -3,7 +3,7 @@ unit MVCFramework.Serializer.Commons;
interface
uses
System.Rtti, System.Classes, System.SysUtils;
System.Rtti, System.Classes, System.SysUtils, System.Generics.Collections, MVCFramework.Serializer.Intf;
type
TSerializerHelpers = class sealed
@ -33,6 +33,17 @@ type
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
uses
@ -193,4 +204,34 @@ begin
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.

View File

@ -233,7 +233,7 @@ begin
for I := 0 to Arr.Count - 1 do
begin
list.Add(Mapper.JSONObjectToObject(cref,
Arr.Get(I) as TJSONObject));
Arr.Items[I] as TJSONObject));
end;
end
else // Ezequiel J. Müller convert regular list
@ -248,11 +248,11 @@ begin
for ListParam in ListMethod.GetParameters do
case ListParam.ParamType.TypeKind of
tkInteger, tkInt64:
ListItem := StrToIntDef(Arr.Get(I).Value, 0);
ListItem := StrToIntDef(Arr.Items[I].Value, 0);
tkFloat:
ListItem := TJSONNumber(Arr.Get(I).Value).AsDouble;
ListItem := TJSONNumber(Arr.Items[I].Value).AsDouble;
tkString, tkLString, tkWString, tkUString:
ListItem := Arr.Get(I).Value;
ListItem := Arr.Items[I].Value;
end;
if not ListItem.IsEmpty then
@ -597,7 +597,7 @@ begin
lJArr := TJSONArray(lJValue);
for I := 0 to lJArr.Count - 1 do
begin
AList.Add(JSONObjectToObject(AClazz, lJArr.Get(I) as TJSONObject));
AList.Add(JSONObjectToObject(AClazz, lJArr.Items[I] as TJSONObject));
end;
finally
lJValue.Free;
@ -637,4 +637,12 @@ begin
end;
end;
initialization
TMVCSerUnSerRegistry.RegisterSerializer('application/json', TMVCJSONSerUnSer.Create);
finalization
TMVCSerUnSerRegistry.UnRegisterSerializer('application/json');
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}
, LoggerPro
, MVCFramework.DuckTyping
, MVCFramework.Patches;
, MVCFramework.Patches, MVCFramework.Serializer.Intf;
type
TDMVCSerializationType = TSerializationType;
@ -348,6 +348,7 @@ type
FContext: TWebContext;
FResponseStream: TStringBuilder;
FContentCharset: string;
FRenderer: IMVCSerUnSer;
procedure SetContext(const Value: TWebContext);
procedure SetWebSession(const Value: TWebSession);
procedure SetContentType(const Value: string);
@ -355,11 +356,7 @@ type
function GetWebSession: TWebSession;
function GetContentCharset: string;
procedure SetContentCharset(const Value: string);
// procedure Render<T: class>(ACollection: TObjectList<T>; AInstanceOwner: boolean;
// AJSONObjectActionProc: TJSONObjectActionProc; ASerializationType: TSerializationType); overload;
// procedure Render<T: class>(ACollection: TObjectList<T>; AInstanceOwner: boolean;
// AJSONObjectActionProc: TJSONObjectActionProc; ASerializationType: TSerializationType);
function GetRenderer: IMVCSerUnSer;
protected const
CLIENTID_KEY = '__clientid';
protected
@ -463,7 +460,7 @@ type
property Config: TMVCConfig read GetMVCConfig;
property StatusCode: UInt16 read GetStatusCode write SetStatusCode;
property Renderer: IMVCSerUnSer read GetRenderer;
public
// property ViewCache: TViewCache read FViewCache write SetViewCache;
procedure PushJSONToView(const AModelName: string; AModel: TJSONValue);
@ -650,7 +647,7 @@ uses
IdHTTPWebBrokerBridge,
MVCFramework.MessagingController,
Web.WebReq,
MVCFramework.SysControllers;
MVCFramework.SysControllers, MVCFramework.Serializer.Commons;
const
ALLOWED_TYPED_ACTION_PARAMETERS_TYPES =
@ -1994,6 +1991,15 @@ begin
end;
end;
function TMVCController.GetRenderer: IMVCSerUnSer;
begin
if FRenderer = nil then
begin
FRenderer := TMVCSerUnSerRegistry.GetSerUnSer(ContentType);
end;
Result := FRenderer;
end;
function TMVCController.GetWebSession: TWebSession;
begin
Result := FContext.Session;

View File

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

View File

@ -36,7 +36,10 @@ uses
TestServerControllerPrivateU in 'TestServerControllerPrivateU.pas',
AuthHandlersU in 'AuthHandlersU.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}

View File

@ -132,6 +132,9 @@
<DCCReference Include="AuthHandlersU.pas"/>
<DCCReference Include="..\..\sources\MVCFramework.Patches.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\dmvcframeworkbuildconsts.inc"/>
<BuildConfiguration Include="Release">
@ -215,16 +218,7 @@
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<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>
<DeployClass Name="ProjectiOSDeviceResourceRules"/>
<DeployClass Name="ProjectOSXResource">
<Platform Name="OSX32">
<RemoteDir>Contents\Resources</RemoteDir>
@ -564,7 +558,16 @@
<Operation>1</Operation>
</Platform>
</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="Win64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/>