Good day to all. There was a question about C # events.
We have an Engine (Engine).
Engine has ModelParams model parameters. For example, the parameters include the countOfClapans valve count. In ModelParams, the paramsChanged event is defined, signaling a change in model parameters.
During the design process, I had a question: how to protect the parameters of the model from changing through sender from the paramsChanged event handler? At the same time, I do not want to make the properties of the parameters of the model of the type {get; private set {}} to disable the ability to edit properties outside the class.
As I understand it, you can make separate events to change each parameter of the model. But what if there are many of these parameters? Do not do the same number of event handlers for events.
In short, I try to isolate a ModelParams object from other objects (in this case, Cylinder class objects) that will subscribe to a model change event.
Below is the code in a very simplified form:
public class Engine { public ModelParams modelParams; public List<Cylinder> cylinders = new List<Cylinder>(); public Engine( ModelParams modelParams ) { this.modelParams = modelParams; } } public class ModelParams { private int countOfClapans; private string engineType; public int ClapansCount { get { return countOfClapans; } set { countOfClapans = value; } }; public int TypeOfEngine { get { return engineType; } set { engineType= value; } }; public event EventArgs<ModelArgs> ModelChanged; public void OnModelChange() { if(modelChanged != null) { ModelArgs args = new ModelArgs(); args.ClapansCount = this.ClapansCount; ModelChanged(this, args); } } } public class ModelArgs{ private int countOfClapans; private string engineType; public int ClapansCount { get { return countOfClapans; } set { countOfClapans= value; } }; public int TypeOfEngine { get { return engineType; } set { engineType= value; } }; }