构造此通用对象创建的正确方法是什么

时间:2017-02-28 11:26:38

标签: delphi dependency-injection delphi-10-seattle delphi-10.1-berlin

忽略这个使用Aurelius框架的事实,这个问题更多地是关于我需要如何重新调整代码以使通用构造函数注入适用于这两种类型:
<字符串>

< TCustomConnection>
还要忽略子对象在同一个单元中的事实,我通常将它们放在自己的单元中,但这样可以更容易在问题中发布。

我正在尝试使用Factory Method模式来确定它应该在运行时建立什么类型的连接,具体取决于我实例化的对象。 目前它正在硬编码它在创建时所期望的链接类型。

在我想要传递TModelDatabaseLink的示例中,但是想要在运行时决定它可以是什么类型的数据库连接,或者数据库连接是否来自文件。 是的我知道我可以取消注释FFilename并使用它来保存文件名版本,但我总是对学习更多内容感兴趣。

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.

如果可能的话,我想做的另一件事是更改两个创作:

LAdapter := TSQLiteNativeConnectionAdapter.Create(LFilename)

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

在父TModelDatabaseConnection中使用抽象的“GetAdapterClass”类型函数,只需在子代中声明适配器类,即可执行以下操作:

LAdapter := GetAdapterClass.Create...

适配器声明的一个例子是

TFireDacConnectionAdapter = class(TDriverConnectionAdapter<TFDConnection>, IDBConnection)

1 个答案:

答案 0 :(得分:0)

在我编写一个抽象层之前,我已经完成了这个,以防我需要在我的应用程序中替换Aurelius。我认为处理你想要做的事情的最好方法是使用接口。

我正在复制我的代码的某些部分:

  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;

我将在这里跳过所有getXXX函数的代码。

构造函数是:

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;

现在,当您想要创建Aurelius数据库时,您可以使用以下功能:

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

...你只需这样称呼它:

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

处理数据库创建的更好方法是使用具有不同参数的重载函数或匿名方法来避免case-end分支。