delphimvcframework/sources/MVCFramework.ApplicationSession.pas

206 lines
5.4 KiB
ObjectPascal
Raw Normal View History

// ***************************************************************************
//
// Delphi MVC Framework
//
2022-01-04 15:44:47 +01:00
// Copyright (c) 2010-2022 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// *************************************************************************** }
2015-12-22 12:38:17 +01:00
2013-10-30 00:48:23 +01:00
unit MVCFramework.ApplicationSession;
{$I dmvcframework.inc}
2013-10-30 00:48:23 +01:00
interface
2017-03-20 19:08:01 +01:00
uses
System.SysUtils,
System.DateUtils,
System.Generics.Collections;
2013-10-30 00:48:23 +01:00
type
2017-03-20 19:08:01 +01:00
TWebApplicationSession = class abstract
private
{ private declarations }
protected
function GetItems(const AKey: string): string; virtual; abstract;
procedure SetItems(const AKey, AValue: string); virtual; abstract;
2013-10-30 00:48:23 +01:00
public
constructor Create; virtual;
destructor Destroy; override;
2017-03-20 19:08:01 +01:00
2013-10-30 00:48:23 +01:00
function ToString: string; override;
2017-03-20 19:08:01 +01:00
property Items[const AKey: string]: string read GetItems write SetItems; default;
2013-10-30 00:48:23 +01:00
end;
TWebApplicationSessionClass = class of TWebApplicationSession;
TWebApplicationSessionMemory = class(TWebApplicationSession)
2017-03-20 19:08:01 +01:00
private
2013-10-30 00:48:23 +01:00
FData: TDictionary<string, string>;
2017-03-20 19:08:01 +01:00
protected
function GetItems(const AKey: String): String; override;
procedure SetItems(const AKey, AValue: String); override;
2013-10-30 00:48:23 +01:00
public
constructor Create; override;
destructor Destroy; override;
2017-03-20 19:08:01 +01:00
function ToString: String; override;
property Data: TDictionary<string, string> read FData;
2013-10-30 00:48:23 +01:00
end;
TMVCApplicationSessionFactory = class sealed
2017-03-20 19:08:01 +01:00
private
2013-10-30 00:48:23 +01:00
FRegisteredApplicationSessionTypes: TDictionary<String, TWebApplicationSessionClass>;
private
class var Instance: TMVCApplicationSessionFactory;
2017-03-20 19:08:01 +01:00
public
constructor Create;
2015-04-01 17:01:23 +02:00
destructor Destroy; override;
2017-03-20 19:08:01 +01:00
procedure RegisterSessionType(const AName: String; AWebApplicationSessionClass: TWebApplicationSessionClass);
function CreateNewByType(const AName: String): TWebApplicationSession;
class function GetInstance: TMVCApplicationSessionFactory; static;
class procedure DestroyInstance; static;
2013-10-30 00:48:23 +01:00
end;
implementation
{ TWebApplicationSession }
2013-10-30 00:48:23 +01:00
constructor TWebApplicationSession.Create;
begin
inherited Create;
end;
destructor TWebApplicationSession.Destroy;
begin
inherited Destroy;
2013-10-30 00:48:23 +01:00
end;
function TWebApplicationSession.ToString: string;
begin
Result := EmptyStr;
2013-10-30 00:48:23 +01:00
end;
{ TWebApplicationSessionMemory }
2013-10-30 00:48:23 +01:00
constructor TWebApplicationSessionMemory.Create;
begin
inherited Create;
2013-10-30 00:48:23 +01:00
FData := TDictionary<string, string>.Create;
end;
destructor TWebApplicationSessionMemory.Destroy;
begin
FData.Free;
inherited Destroy;
2013-10-30 00:48:23 +01:00
end;
2017-03-20 19:08:01 +01:00
function TWebApplicationSessionMemory.GetItems(const AKey: String): String;
2013-10-30 00:48:23 +01:00
begin
TMonitor.Enter(Self);
2013-10-30 00:48:23 +01:00
try
2017-03-20 19:08:01 +01:00
if not FData.TryGetValue(AKey, Result) then
Result := EmptyStr;
2013-10-30 00:48:23 +01:00
finally
TMonitor.Exit(Self);
2013-10-30 00:48:23 +01:00
end;
end;
2017-03-20 19:08:01 +01:00
procedure TWebApplicationSessionMemory.SetItems(const AKey, AValue: String);
2013-10-30 00:48:23 +01:00
begin
TMonitor.Enter(Self);
2013-10-30 00:48:23 +01:00
try
2017-03-20 19:08:01 +01:00
FData.AddOrSetValue(AKey, AValue);
2013-10-30 00:48:23 +01:00
finally
TMonitor.Exit(Self);
2013-10-30 00:48:23 +01:00
end;
end;
function TWebApplicationSessionMemory.ToString: String;
var
2017-03-20 19:08:01 +01:00
LKey: String;
2013-10-30 00:48:23 +01:00
begin
TMonitor.Enter(Self);
2013-10-30 00:48:23 +01:00
try
Result := EmptyStr;
2017-03-20 19:08:01 +01:00
for LKey in FData.Keys do
2013-10-30 00:48:23 +01:00
begin
2017-03-20 19:08:01 +01:00
Result := LKey + ' = ' + QuotedStr(FData.Items[LKey]) + sLineBreak;
2013-10-30 00:48:23 +01:00
end;
finally
TMonitor.Exit(Self);
2013-10-30 00:48:23 +01:00
end;
end;
{ TMVCApplicationSessionFactory }
2013-10-30 00:48:23 +01:00
constructor TMVCApplicationSessionFactory.Create;
begin
inherited Create;
2013-10-30 00:48:23 +01:00
FRegisteredApplicationSessionTypes := TDictionary<string, TWebApplicationSessionClass>.Create;
end;
function TMVCApplicationSessionFactory.CreateNewByType(const AName: String): TWebApplicationSession;
2013-10-30 00:48:23 +01:00
var
Clazz: TWebApplicationSessionClass;
2013-10-30 00:48:23 +01:00
begin
if not FRegisteredApplicationSessionTypes.TryGetValue(AName, Clazz) then
2013-10-30 00:48:23 +01:00
raise Exception.Create('Unknown application session type');
Result := Clazz.Create;
2013-10-30 00:48:23 +01:00
end;
destructor TMVCApplicationSessionFactory.Destroy;
begin
FRegisteredApplicationSessionTypes.Free;
inherited Destroy;
end;
class procedure TMVCApplicationSessionFactory.DestroyInstance;
begin
if Assigned(Instance) then
Instance.Free;
2013-10-30 00:48:23 +01:00
end;
class function TMVCApplicationSessionFactory.GetInstance: TMVCApplicationSessionFactory;
2013-10-30 00:48:23 +01:00
begin
if not Assigned(Instance) then
Instance := TMVCApplicationSessionFactory.Create;
Result := Instance;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCApplicationSessionFactory.RegisterSessionType(const AName: String; AWebApplicationSessionClass: TWebApplicationSessionClass);
2013-10-30 00:48:23 +01:00
begin
FRegisteredApplicationSessionTypes.AddOrSetValue(AName, AWebApplicationSessionClass);
end;
initialization
2017-03-20 19:08:01 +01:00
TMVCApplicationSessionFactory.GetInstance.RegisterSessionType('memory', TWebApplicationSessionMemory);
2013-10-30 00:48:23 +01:00
finalization
TMVCApplicationSessionFactory.DestroyInstance;
2013-10-30 00:48:23 +01:00
end.