0

Ignoring the fact that this uses the Aurelius Framework, this question is more about how I need to re-tweak the code to make the generic constructor injection work for both type:
< string >
and
< TCustomConnection >
Also ignore the fact that the child objects are in the same unit, I would generally put them in their own, but this just makes it easier to post in a question.

I'm trying use the Factory Method pattern to determine what type of connection it should be making at runtime depending on what object I instantiate. At the moment it is hardcoding the type of link it is expecting on the create.

In the example I want to pass in a TModelDatabaseLink, but want to decide at run time what type of database connection it could be, or whether the database connection comes from a file. Yes I know I could uncomment FFilename and just use this to hold the file name version, but I am always interested in learning a bit more.

unit Model.Database.Connection;

interface

uses
  System.Classes,
  Data.DB,
  Aurelius.Drivers.Interfaces,
  Aurelius.Engine.DatabaseManager;

type
  TModelDatabaseLink<T> = class
  private
    //FFilename: string;
    FConnection: T;
    FOwnsConnection: boolean;
    OwnedComponent: TComponent;
  end;

  TModelDatabaseConnection = class abstract
  private
    FDatabaseLink: TModelDatabaseLink<TCustomConnection>;
    FDatabaseManager: TDatabaseManager;
    FConnection: IDBConnection;
    function CreateConnection: IDBConnection; virtual; abstract;
    procedure CreateDatabaseManager;
  public
    constructor Create(ADatabaseLink: TModelDatabaseLink<TCustomConnection>);
    destructor Destroy; override;

    property Connection: IDBConnection read FConnection;
  end;

  TSQLLiteConnection = class(TModelDatabaseConnection)
  private
    function CreateConnection: IDBConnection; override;
  end;

  TFireDacConnection = class(TModelDatabaseConnection)
  private
    function CreateConnection: IDBConnection; override;
  end;

implementation

uses
  System.SysUtils,

  Aurelius.Drivers.Base,
  Aurelius.Drivers.SQLite,
  Aurelius.Drivers.FireDac,

  FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.VCLUI.Wait,
  FireDAC.Comp.Client;

{ TModelDatabaseConnection }

constructor TModelDatabaseConnection.Create(ADatabaseLink: TModelDatabaseLink<TCustomConnection>);
begin
  FDatabaseLink := ADatabaseLink;
  FConnection := CreateConnection;
  if Assigned(FConnection) then
    CreateDatabaseManager
  else
    raise Exception.Create('Failed to open database');
end;

procedure TModelDatabaseConnection.CreateDatabaseManager;
begin
  FDatabaseManager := TDatabaseManager.Create(FConnection);
end;

destructor TModelDatabaseConnection.Destroy;
begin
  FDatabaseManager.Free;
  FDatabaseLink.Free;
  inherited Destroy;
end;

{ TSQLLiteConnection }

function TSQLLiteConnection.CreateConnection: IDBConnection;
var
  LFilename: String;
  LAdapter: TSQLiteNativeConnectionAdapter;
begin
  //LFileName := FDatabaseLink.FConnection;                     << needs to be type string
  LAdapter := TSQLiteNativeConnectionAdapter.Create(LFilename);
  LAdapter.DisableForeignKeys;
  Result := LAdapter;
end;

{ TFireDacConnection }

function TFireDacConnection.CreateConnection: IDBConnection;
var
  LAdapter: TFireDacConnectionAdapter;
begin
  if Assigned(FDatabaseLink.OwnedComponent) then
    LAdapter := TFireDacConnectionAdapter.Create(FDatabaseLink.FConnection as TFDConnection, FDatabaseLink.OwnedComponent)
  else
    LAdapter := TFireDacConnectionAdapter.Create(FDatabaseLink.FConnection as TFDConnection, FDatabaseLink.FOwnsConnection);

  Result := LAdapter;
end;

end.

One other thing I would like to do if possible, is to change two creations:

LAdapter := TSQLiteNativeConnectionAdapter.Create(LFilename)

LAdapter := TFireDacConnectionAdapter.Create(FDatabaseLink.FConnection as TFDConnection, FDatabaseLink.OwnedComponent)

to use an abstract "GetAdapterClass" type function in the parent TModelDatabaseConnection and just declare the class of adapter in the child to do something like:

LAdapter := GetAdapterClass.Create...

An example of an adapter declaration is

TFireDacConnectionAdapter = class(TDriverConnectionAdapter<TFDConnection>, IDBConnection)
mikelittlewood
  • 223
  • 4
  • 15
  • Could it be that you are working with the Spring.Persistence adapters? Because this code looks very familiar to me. If so look into Spring.Persistence.Core.ConnectionFactory.pas how different ctor parameters for the adapted connection are injected. – Stefan Glienke Feb 28 '17 at 14:33
  • Hey Stefan. These are actually the ones from the TMS Aurelius framework to be honest. I will however take a look at the Spring unit too as I have them on my PC as well to see if it gives me any more clues. – mikelittlewood Feb 28 '17 at 14:51
  • Hi Stefan. In Spring.Persistence.Adapters.SQLite you specify that you have to pass in a TSQLiteDatabase rather than just a file name. The only one I can find in the Spring code is the one under ..\Marshmallow\External\SQLite3. Is this an "official" Spring one? – mikelittlewood Feb 28 '17 at 16:34
  • Adapters always get their adaptee being passed. But the ctor arguments of the adaptees can differ because of their implementation. That is being handled either by a DI container that knows how to resolve different parameters or the unit I mentioned in the previous comment. As for the SQLite3 units - they have a few bugs fixed but they are not official as we don't provide any support, maintenance or responsibility for them. – Stefan Glienke Feb 28 '17 at 17:02

1 Answers1

0

I have done this before when I wrote an abstract layer in case I need to replace Aurelius in my applications. I think the best way to deal with what you want to do is to use interfaces.

I am copying here some parts of my code with adjustments:

  TServerType = (stLocal, stFireDac);

  IDatabase = interface
    function getDatabaseType: TServerType;
    property DatabaseType: TServerType read getDatabaseType;
  end;

  IAurelius = interface (IDatabase)
    ['{990BB776-2E70-4140-B118-BEFF61FDBDAF}']
    function getDatabaseConnection: IDBConnection;
    function getDatabaseManager: TDatabaseManager;
    property DatabaseConnection: IDBConnection read getDatabaseConnection;
    property DatabaseManager: TDatabaseManager read getDatabaseManager;
  end;

  IAureliusLocal = interface (IAurelius)
    ['{9F705CC4-6E3B-4706-B54A-F0649CED3A8D}']
    function getDatabasePath: string;
    property DatabasePath: string read getDatabasePath;
  end;

  IAureliusFireDac = interface (IAurelius)
    // I use this for a memory database but the logic is the same for FireDAC. You need to add the required properties 
  end;

  TAurelius = class (TInterfacedObject, IAurelius)
  private
    fServerType: TServerType;
    fDatabaseConnection: IDBConnection;
    fDatabaseManager: TDatabaseManager;
    function getDatabaseConnection: IDBConnection;
    function getDatabaseManager: TDatabaseManager;
  public
    constructor Create (const serverType: TServerType);
  end;

  TAureliusLocal = class (TAurelius, IAureliusLocal)
  private
    fDatabasePath: string;
    function getDatabasePath: string;
  public
    constructor Create (const databasePath: string);
  end;

  TAureliusFireDac = class (TAurelius, IAureliusFireDac)
  public 
    constructor Create (const aConnection: TFDConenction); <-- or whatever parameters you need here to initalise the FireDac connection
  end;

I am going to skip the code for all the getXXX functions here.

The constructors are these:

constructor TAurelius.Create(const serverType: TServerType);
begin
  inherited Create;
  fServerType:=serverType;
end;

constructor TAureliusLocal.Create (const databasePath: string);
const
  databaseFilename = 'test.sqlite';
begin
  inherited Create(stLocal);
  fDatabasePath:=Trim(databasePath);
   try
    fDatabaseConnection:=
    TSQLiteNativeConnectionAdapter.Create(
      TPath.Combine(fDatabasePath, databaseFilename));
   except
    raise Exception.Create('stLocal database can''t be created');
   end;
end;

constructor TAureliusFireDac.Create (const aConnection: TFDConenction);
begin
  inherited Create(stFireDac);
  // <-- here you initialise the connection like before but for FireDac
end;

Now, when you want to create an Aurelius database you use the following functions:

function createAureliusDatabase (const serverType: TServerType): IAurelius;
begin
  case serverType of
    stLocal: result:=TAureliusLocal.Create(path);
    stFireDac: result:=TAureliusFireDac.Create(....);
  end;
end;

...and you simply call it like this:

var 
  currentDatabase: IAurelius;
begin
  currentDatabase:=createAureliusDatabase(stLocal,'c:\....');
end;

A better way to deal with the creation of the database is to use overloaded functions with different parameters or anonymous methods to avoid the case-end branch.

John Kouraklis
  • 686
  • 4
  • 12