Initial conditions:

There is a bike model that has a number of fixed parameters. In addition to the initial model parameters, it should be possible to manually add new model parameters.

Situation:

Each parameter in the database is represented by the following characteristics:

  • Parameter name
  • Units
  • Parameter value

There are several Lookup Tables in the database. Each of these tables shows the available units.

Other reference tables provide parameter names.

For each model, the name of the parameter, its value and units of measurement should be displayed on the screen.

Questions:

How best to implement a similar approach based on DDD? I do not want to complicate the domain model by dragging into it the approach used in the Database, which should not affect the model at all.

Notes:

Initially, before familiarizing myself with the database, I thought that the parameters in the model should be represented by a set of properties, for example:

// МодСль вСлосипСда public class BikeModel { public BikeModel(string name, double height, double width) { ChangeName(name); ChangeHeight(height); ChangeWidth(width); } // НазваниС ΠΌΠΎΠ΄Π΅Π»ΠΈ вСлосипСда public string Name { get; private set; } // Π”Π»ΠΈΠ½Π° вСлосипСда public double Height { get; private set; } // Π¨ΠΈΡ€ΠΈΠ½Π° вСлосипСда public double Width { get; private } public void ChangeName(string name) { Name = name; } public void ChangeHeight(double height) { Height = height; } public void ChangeWidth(double width) { Width = width; } } 

However, reading the database, I realized that under the value "Parameter" hides a number of characteristics presented in the section "Situation". It turns out that the parameter can be represented by the Generic-class.

 public class ParameterOfModel<T> { public ParameterOfModel(string name, T value, string units) { SetName(name); SetValue(value); SetUnits(units); } // НазваниС ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° public string Name { get; private set; } // Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° public T Value { get; private set; } // Π•Π΄ΠΈΠ½ΠΈΡ†Ρ‹ измСрСния ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° public string Units { get; private set; } public void SetName(string name) { Name = name; } public void SetValue(T value) { Value = value; } public void SetUnits(string units) { Units = units; } } 

In this case, the bike model will take the following form:

  // МодСль вСлосипСда public class BikeModel { public BikeModel(string name, double height, double width) { ChangeName(name); ChangeHeight(height); ChangeWidth(width); AdditionalNumericParams = new List<ParameterOfModel<double>>(); } // НазваниС ΠΌΠΎΠ΄Π΅Π»ΠΈ вСлосипСда public string Name { get; private set; } // Π”Π»ΠΈΠ½Π° вСлосипСда public ParameterOfModel<double> Height { get; private set; } // Π¨ΠΈΡ€ΠΈΠ½Π° вСлосипСда public ParameterOfModel<double> Width { get; private set; } // Π”ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ числовыС ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ public List<ParameterOfModel<double>> AdditionalNumericParams { get; private set; } public void ChangeName(string name) { Name = name; } public void ChangeHeight(double height) { Height = new ParameterOfModel<double>("Высота вСлосипСда", height, "Π‘Π°Π½Ρ‚ΠΈΠΌΠ΅Ρ‚Ρ€Ρ‹"); } public void ChangeWidth(double width) { Width = new ParameterOfModel<double>("Π¨ΠΈΡ€ΠΈΠ½Π° вСлосипСда", width, "Π‘Π°Π½Ρ‚ΠΈΠΌΠ΅Ρ‚Ρ€Ρ‹"); } // Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π² модСль Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ public void AddNewNumericParam(ParameterOfModel<double> additionalParameter) { AdditionalNumericParams.Add(additionalParameter); } } 

But how to collect such a parameter using reference tables?

  • There are 2 options: 1) - store the values ​​of the parameters in the form of dates, the types of these parameters - in a separate column. 2) - for each data type, make your own table of values ​​- DoubleValues, StringValues, IntValues ​​... etc - tym32167
  • Thanks for the answer. The problem is that I can not change the database. I am interested in the implementation in the program code based on the storage conditions in the database. - Eladei
  • The program code, in theory, should be based on use cases. If you just need to show the values ​​on the screen - that makes sense in typing, keep everything in rows. If you need the most flexible mechanism - there should not be any hard-wired lines like "Centimeters", and the data types and field names should be taken from external sources (DB, services, config files). You also need to think about how the user will enter data - how will you validate it? What parameter is required for the bike? Which must contain a value (can not be empty)? What parameters can not be repeated? - tym32167
  • The problem is that I do not understand: how can I configure the model of the bike. Validation of parameters is carried out in methods-installers, for example, in ChangeWidth. Just in this code they are not represented. If you use to create a model of the factory in which services will be used, then how can you clearly indicate which, for example, units of measurement to use, if their value is known in advance. I understand that the parameter type, units and value must be obtained from an external source. But according to the task condition, the set of parameters of the bicycle model is predetermined. - Eladei
  • one
    In this case, put a plus sign , can someone from the adepts tell :) - tym32167

0