开发者

Define type by having certain objects in a collection in C#

开发者 https://www.devze.com 2023-02-23 14:06 出处:网络
Let me explain my model, what I was going to do, and what I would like to do and get your opinions on how to implement it.

Let me explain my model, what I was going to do, and what I would like to do and get your opinions on how to implement it.

Background

So I have a class called a TelemetryStation that has a collection of another class called Sensor. Well maybe the code would be more clear:

public class TelemetryStation
{
    IList<Sensor> Sensors { get; set; }
}

I was going to add a property to it called TelemetryStationType so that we could add tags to the TelemetryStation do identify what type it is. i.e. WeatherStation, Chemigation, etc. I would allow multiple tags if it meets the criteria for each type of TelemetryStaion.

Well this is fine, but what I would really like to do is guarantee that a WeatherStation has a minimal amount of certain sensors like temperature, wind speed, etc. My thought is that on my web page or windows form page, I could then grab all of the TelemetryStations of a certain type and it would be guaranteed that they have the correct sensors.

Questions

So what would be the best way to code this? Can I model it in classes or would I need to just create queries that grabs all TelemetryStation with certain types of sensors?

Edit

Something I should have added that might be important is how we are getting the data. So we have a port listener made by another company that dumps the telemetry station readings into a csv text file. So the file is named after the station Id and each line has a datetime stamp followed by the comma separated readings. There is no identification as to what the readings are. So I am allowing the user to define the model for the telemetry station as far as what sensors are coming in through the port listener and their posistion in the line in t开发者_高级运维he text file. I then parse the text file using the user created model of the data. This way if the company changes models or the order that the sensor data is in the text file, we can define a new model and the new data stream will make sense.


How about you have subclasses for your stations like this:

class WeatherStation : TelemetryStation
{
    public Sensor Temperatur { get; set; }
    public Sensor Wind { get; set; }
}

and in your base class you reflect on the properties

class TelemetryStation
{
    private List<Sensor> _Sensors = null;
    public List<Sensor> Sensors 
    {
        get 
        {
            if (_Sensors == null)
            {
               _Sensors = typeof(this).GetProperties().Where(p => p.PropertyType is Sensor).Select(p => (Sensor)p.GetValue(this, null)).ToList();
            }
            return _Sensors;
        }
    }
}

This way you have access to all the sensors on a specific station and you can make sure specific station types have a specific set of sensors.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号