4

我正在使用 RemObjects DataAbstract 和 Spring4d。RemObjects 生成一个SchemaServer_Intf.pas文件,该文件包含其模式中存在的每种表的接口。它允许“强类型”数据集,允许使用

(aDataSet as IMyDataSet).MyField := aValue

这是 DataAbstract 生成的接口之一的快照

IEntiteType = interface(IDAStronglyTypedDataTable)
  ['{96B82FF7-D087-403C-821A-0323034B4B99}']
    { Property getters and setters }
    function GetEntiteIdValue: String;
    procedure SetEntiteIdValue(const aValue: String);
    function GetEntiteIdIsNull: Boolean;
    procedure SetEntiteIdIsNull(const aValue: Boolean);
    function GetNameValue: WideString;
    procedure SetNameValue(const aValue: WideString);
    function GetNameIsNull: Boolean;
    procedure SetNameIsNull(const aValue: Boolean);
    function GetIsSystemValue: SmallInt;
    procedure SetIsSystemValue(const aValue: SmallInt);
    function GetIsSystemIsNull: Boolean;
    procedure SetIsSystemIsNull(const aValue: Boolean);


    { Properties }
    property EntiteId: String read GetEntiteIdValue write SetEntiteIdValue;
    property EntiteIdIsNull: Boolean read GetEntiteIdIsNull write SetEntiteIdIsNull;
    property Name: WideString read GetNameValue write SetNameValue;
    property NameIsNull: Boolean read GetNameIsNull write SetNameIsNull;
    property IsSystem: SmallInt read GetIsSystemValue write SetIsSystemValue;
    property IsSystemIsNull: Boolean read GetIsSystemIsNull write SetIsSystemIsNull;
  end;

不过,有一个问题。如果您像这样投射数据表:

aDataTable := IEntiteType(TDAMemDataTable.Create(nil));

您将收到“不支持接口错误

但是,一旦你这样做:

aDataTable.LogicalName := 'EntiteType';
aDataTable.BusinessRulesId := MyBusinessRuleID;

你可以放心地写

aDataTable := IEntiteType(TDAMemDataTable.Create(nil));

而且您不会收到任何错误。

所以,对于 Spring4d,我想在我的注册单元中写这个:

aContainer.RegisterType<TDAMemDataTable>.Implements<IEntiteType>.DelegateTo(
   function : TDAMemDataTable
   var aDataTable : TDAMemDataTable; 
   begin
      Result:= TDAMemDataTable.Create(nil);
      Result.LogicalName := 'EntiteType';
      Result.BusinessRulesId := MyBusinessRuleId;         
   end
) 

但是,Spring4d 抛出(有原因)错误:

Exception 'first chance' à $762D5B68. Classe d'exception ERegistrationException avec un message 'Component type "uDAMemDataTable.TDAMemDataTable" incompatible with service type "SchemaClient_Intf.IEntiteType".'. Processus EntiteREM2.exe (3088) 

有没有办法覆盖这个检查?

4

1 回答 1

4

Ok I've found a way to do that. Super simple actually :

  aContainer.RegisterType<IAddress>.DelegateTo(
    function : IAddress
    var aTable : TDAMemDataTable;
    begin
      aTable := TDAMemDataTable.Create(nil);
      aTable.LogicalName := nme_Address;
      aTable.BusinessRulesID := RID_Address;
      Result := aTable as IAddress;
    end
  );

Also, for people interested in registering many tables in an elegant fashion :

aContainer.RegisterType<IAddress>.DelegateTo(TableConfigurator.GetTableDelegate<IAddress>(nme_Address, RID_Address));
// Registering other tables here...

Just create some "Helper" class with this method :

class function TableConfigurator.GetTableDelegate<T>(aLogicalName, aBusinessRulesId: string): TActivatorDelegate<T>;
begin
  Result := (function: T
    var
      aTable: TDAMemDataTable;
    begin
      aTable := TDAMemDataTable.Create(nil);
      aTable.LogicalName := aLogicalName;
      aTable.BusinessRulesID := aBusinessRulesId;
      Result := T(TValue.From(aTable).AsInterface);
    end);
end;
于 2015-12-09T23:22:14.817 回答