0

Getting around to the thinking of OOP. Please correct me if this is horrible way of structuring code.

Imagine there is a class Thermometer. Inside the class I have got 3 structs config Limit Measurements. At different times during the day the code will keep a list of classes Thermometer and take measurements. Check if they are within a certain limits and save to a json.

Now imagine I've got a json containing the limits as such:

{
"Thermometer1" : {
    "sevenPM":
    {
        "Min" : 10,
        "Max" : 20
    },
    "twelvePM":
    {
        "Min" : 15,
        "Max" : 25
    },
    "threePM":
    {
        "Min" : 30,
        "Max" : 35
    }
},
"Thermometer2" : {
    "sevenPM":
    {
        "Min" : 30,
        "Max" : 40
    },
    "twelvePM":
    {
        "Min" : 45,
        "Max" : 60
    },
    "threePM":
    {
        "Min" : 22,
        "Max" : 30
    }
}       
}

Here is the class:

Class Thermometer
{       
    Thermometer()
    {
        Limits _limits = new Limits();
        CONFIG _config = new CONFIG();
        Measurements _measurements = new Measurements();
    }               
    
    Method1()
    {
        //does calculations
    }
    
    Method2()
    {
        //does calculation
    }   
}

The structs:

struct CONFIG
    {
        bool inShadeorInSun;
        string Thermometer_Model;
        string Location;
    }
    
    struct Limits
    {
        struct sevenPM
        {
            double Min;
            double Max;
        }
        struct twelvePM
        {
            double Min;
            double Max;
        }
        struct threePM
        {
            double Min;
            double Max;
        }
    }
    
    struct Measurements
    {
        double sevenPM;
        double twelvePM;
        double threePM;
    }

When I write the list of classes Thermometer to a json i will get an entry for each thermometer. Each thermometer will have config, limits and measurements header inside it. This is to record the config of course, the limit that was set at the time of the recording and the measurements.

On app startup I want the application to read the Limits json and when adding a new thermometer I can simply say: "Import Thermometer2 Limits" and do something like this:

string json = File.ReadAllText("Limits.json");
dynamic read_limits = JsonConvert.DeserializeObject<dynamic>(json);
read_limits = jsonobject["Thermometer2"];
Thermometer _thermometer2 = new Thermometer();
_thermometer2._limits = read_limits;

I think I have got everything right in terms of OOP?? but I am also having issues deserialising the json into the struct.

the last line _thermometer2._limits = read_limits; does not work

Is this a good handling of OOP?

Breakwin
  • 84
  • 6
  • 3
    Well your fields are private so they won't be serialized or deserialized by default. – dbc May 20 '21 at 15:49
  • 3
    *Is this a good handling of OOP?* - see [Why are mutable structs “evil”?](https://stackoverflow.com/q/441309/3744182). Either use immutable structs, or convert to classes. See also [When should I use a struct rather than a class in C#?](https://stackoverflow.com/q/521298/3744182). – dbc May 20 '21 at 15:50
  • *the last line `_thermometer2._limits = read_limits;` does not work* - what is your basic question here? Is it *Is this a good handling of OOP?* or is it *How can I deserialize the following JSON into the following classes & structs*? The rule on stack overflow is to ask [one question per post](https://meta.stackexchange.com/q/222735) so it would be better to clarify which you need answered. But if the former, it may be off-topic as being opinion based. If the latter, are you sure you included a complete [mcve]? – dbc May 20 '21 at 15:56
  • 1
    Consider a class with the *properties* of `{Min, Max, ReadingTime}` which might simplify a few things – Ňɏssa Pøngjǣrdenlarp May 20 '21 at 15:59

0 Answers0