Reputation: 28856
I'm working on a modular project consisting of multiple sensors, each inheriting from an abstract Sensor
class. To configure each sensor, I have started to add a user control panel for each sensor (inherits from UserControl
), which is loaded at runtime and displayed in a panel by the calling application.
But then I realised that to change sensor properties from the control, the Control
class needs to have access to those properties or the sensor has to subscribe to configuration setting changes in the user control. This doesn't feel right, however, the control should be controlling the sensor.
What is the most elegant solution to control my sensors without creating a delegate-hell?
Should I continue on the path of:
class TestSensor : Sensor
{
public UserControl ControlPanel { get; } // returns a TestSensorControl
// ...
}
or should my sensors be created inside my controls:
class TestSensorControl : UserControl
{
private TestSensor _sensor;
public TestSensor Sensor {
get {
return _sensor;
}
}
// ...
}
Sensors are responsible for checking for state changes and communicating it to the calling application by signalling an OnSensorEvent(...)
event.
Alternatively, my base Sensor
class could inherit from UserControl, and my SensorControl can inherit from that instead, eliminating the need for an intermediate control class.
Upvotes: 2
Views: 1496
Reputation: 156
It feels like you should keep your non-ui Sensor class which can be used by ui and non-ui controllers, and no need to create a base UserControl for every initialization of Sensor class.
I would suggest using inherited controllers(Sensors are created in Controllers).
You can create a generic class SensorControl : UserControl
ui and inherit from that class class TestSensorControl : SensorControl
. That can save time and code dublicates.
You can define an abstract Sensor type in SensorControl and assign events in the constructor to be overridden.
Good luck
Upvotes: 1
Reputation: 4836
Id pass the sensor into control and have a subscription process.
Upvotes: 2