Search code examples
arraysdelphidelphi-xe7

How to share records structure between projects, with slight variations?


I have 2 projects that work on same data, different processes. Main arrays are almost identical with slight variations, like:

// Project 1

TData1 = record
  A:string;
  B:integer;
  C:word;
  ...
end;

// Project 2

TData1 = record
  A:string;
  B:integer;
  C:word;
  ...
  XMLNode:TXMLNode; // Extra value needed in Project 2, not needed in Project 1
end;

I have numerous arrays that i want to share between projects. I would like to keep the same array structure so I can copy&paste any future changes that need to be implemented in both projects. Is there any way to keep records same with slight difference?

I was thinking of something like this:

    // in Project 1:

    TExtras = record
    end;

    // in Project 2:

    TExtras = record
      XMLNode:TXMLNode;
    end;

    // shared - in both projects

    TData1 = record
      A:string;
      B:integer;
      C:word;
      ...
      Extras:TExtras; // different extra fields based on project needs
    end;

And i can add additional fields into Extras in Project2 accessing fields with Data1.Extras.XMLNode. Not sure if this is future proof implementation.

The goal is to one day put all the shared structure into shared unit, one maintenance point and no more copy&paste. I need arrays to retain flexibility as simple arrays (array of TData1 or TArray<TData1>), so I can't go into complex implementation that will limit option to easily copy, sort, make distinct, manipulate data...

Is that correct approach? Any better ideas?


Edit:

Both projects work on same data, so they both read from the same 'source' files, but produce different end results. Right now I have lots of arrays and 99% of them are used for the same purpose in both projects, same functions. But now, when I work on one or the other project, adding new record fields, new functions that use new fields, and if I don't immediately synchronize the structure and new functions, it happens that in a few weeks I will need to to do the same in project 2 and I will create new fields with different names and different function names. So, when I finally copy some complex function between projects, I see they don't match only due to different naming.


Edit 2:

From all the comments and suggestions I have decided to go another route: to share common data structure and code in shared unit and create additional arrays with extra record fields in project 2. I would create these new arrays that link to main data arrays, to have:

// shared data 
TData1 = record
  A:string;
  B:integer;
  C:word;
  ...
end;

Data1:TArray<TData1>;

// additional in Project 2
TDataExtra = record
  DataIdx:integer;// link to TData1
  XMLNode:TXMLNode;
  ...
end; 

DataExtras:TArray<TDataExtra>;

to have simple access to XMLNode value for each Data1 record:

fGetXMLNode(i); // where i is index in Data1 array and function will return XMLNode

I believe with this I can keep shared units and add any extras to any array, with the minimal extra work, which is still lower cost than maintaining 2 data structure and code.

Would that be better solution?


Solution

  • Easy solution is to keep the data structure and related functions shared between both projects and add additional arrays that contain additional data, specific to each project. It will bring slightly more work to use extra arrays, but will keep shared code really shared.

    So, instead of new classes or IFDefs, as proposed in other anwers, a simple extra arrays that link to main data are best option for the problem:

    // shared main data through both projects
    TData1 = record
      A:string;
      B:integer;
      C:word;
      ...
    end;
    
    Data1:TArray<TData1>;
    
    // additional in Project 2
    TDataExtra = record
      DataIdx:integer;// link to TData1
      XMLNode:TXMLNode;
      ...
    end; 
    
    DataExtras:TArray<TDataExtra>;
    

    With this solution, any additional arrays, like DataExtras or any other extra fields for other arrays, are easy to add, expand, without needing to change shared code. Shared code will be easy to maintain, as it only holds main data and nothing specific to only one project