Cesar Romero

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Cesar Romero

This is how I do:

IConnectionFactory = interface(IInterface)
['{71E62C5F-8E39-4654-A595-A9083B5A064F}']
function GetConnection: TFDConnection; overload;
function GetConnection(const ConnectionParams: string):
TFDConnection; overload;
end;

TConnectionFactory = class(TInterfacedOwner, IConnectionFactory)


strict private
FConnection: TFDConnection;
FConnectionParams: string;
FDefaultTransaction: TFDTransaction;
protected
procedure CreateConnection;
function AddConnectionDef(const ConnectionDefName: string; Params:
TStringList): Boolean;
public
constructor Create(const ConnectionParams: string);
function GetConnection: TFDConnection; overload;
function GetConnection(const ConnectionParams: string):
TFDConnection; overload;
end;

class procedure TApplicationSetup.RegisterDatabase(Container:


TContainer; Configuration: TApplicationConfiguration);
begin
Container.RegisterType<IConnectionFactory,
TConnectionFactory>(ERP_CONNECTION_DEF_NAME)
.AsSingletonPerThread

.InjectConstructor(TValue.From<string>(TApplicationSetup.GetErpConnect
ion));

Container.RegisterType<IConnectionFactory,
TConnectionFactory>(DEFAULT_CONNECTION_DEF_NAME)
.AsDefault
.AsSingletonPerThread

.InjectConstructor(TValue.From<string>(TApplicationSetup.GetServiceCon
nection));
end;

TConnectionFactory creates the FireDAC TFDConnection and configure it with the


ConnectionParams, that is returned by the class
methods TApplicationSetup.GetServiceConnection, TApplication
Setup.GetErpConnection, since I have 2 database connections on this project,
this methods loads the database connection params from Windows Registry and build a
string like this:
'DriverID=PG;Server=10.0.2.2;Port=5432;Database=database_name;User_Nam
e=postgres;Password=postgres;Pooled=True;ExtendedMetadata=True;LoginTi
meout=30;"ApplicationName=Project1"'

This is how I create the connection and set the connectionstring:

procedure TConnectionFactory.CreateConnection;
begin
FConnection := TFDConnection.Create(Self.Owner);
FConnection.LoginPrompt := False;

FConnection.ResourceOptions.MacroCreate := False;
FConnection.ResourceOptions.MacroExpand := False;
FConnection.ResourceOptions.EscapeExpand := False;

FDefaultTransaction := TFDTransaction.Create(Self.Owner);
FDefaultTransaction.Connection := FConnection;
end;

function TConnectionFactory.GetConnection(const ConnectionParams:


string): TFDConnection;
const
CONNECTION_DEFAULT = 'DEFAULT';
CONNECTION_DEF_NAME = '_CONNECTION_DEF';
var
ConnectionDefName: string;
Params: TStringList;
begin
Result := FConnection;

Params := TStringList.Create;
Defer(Params.Free);
Params.Delimiter := ';';

if (ConnectionParams <> FConnectionParams) and (Pos(sLineBreak,


ConnectionParams) = 0) then
begin
ConnectionDefName := ConnectionParams.ToUpper +
CONNECTION_DEF_NAME;
Params.DelimitedText := FConnectionParams;
end
else
begin
ConnectionDefName := CONNECTION_DEFAULT + CONNECTION_DEF_NAME;
Params.DelimitedText := ConnectionParams;
end;

AddConnectionDef(ConnectionDefName, Params);
Result.ConnectionDefName := ConnectionDefName;
Result.Connected := True;
end;

I'm using this for more than one year already in my server Applications with several
simultaneous threads and no problems so far.

Regards,
Cesar Romero

You might also like