I don't know that my title describes the code I want reviewed. Hopefully the code is explanatory.
I have an abstract parent class with two children. The parent listens to some hardware notifications and, when the appropriate hardware is connected/removed/fried in butter etc.., raises an event to its children who perform some processing with the data from the change.
The children then raise a public event on the parent with the processed data, which is what I'm unsure of. Here are the (stripped down) classes.
public abstract class DeviceDetector<TEventArgs>
{
public event Action<TEventArgs> DeviceArrived;
public event Action<TEventArgs> DeviceRemoved;
protected void OnDeviceChange(TEventArgs e)
{
if (e.Arrival && DeviceArrived != null)
DeviceArrived(e);
if (!e.Arrival && DeviceRemoved != null)
DeviceRemoved(e);
}
protected event Action<string> DeviceArrival;
protected event Action<string> DeviceRemoval;
}
Each of the two children is defined as follows:
public class SomeDescriptiveChildName: DeviceDetector<DeviceChild1EventArgs>
{
public SomeDescriptiveChildName()
{
this.DeviceArrival += new Action<string>(HandleProtectedEvent);
this.DeviceRemoval += new Action<string>(HandleProtectedEvent);
}
private void HandleProtectedEvent(string e)
{
//Class names have been changed to protect the innocent.
DeviceChild1EventArgs e = new DeviceChild1EventArgs();
base.OnDeviceChange(e);
}
}
public class AnotherDescriptiveChildName: DeviceDetector<DeviceChild2EventArgs>
{
public AnotherDescriptiveChildName()
{
this.DeviceArrival += new Action<string>(HandleProtectedEvent);
this.DeviceRemoval += new Action<string>(HandleProtectedEvent);
}
private void HandleProtectedEvent(string e)
{
DeviceChild2EventArgs e = new DeviceChild2EventArgs();
base.OnDeviceChange(e);
}
}
The DeviceChild1EventArgs
and DeviceChild2EventArgs
are classes inheriting from EventArgs to represent the data from each child.
My question is this: Is there a better way to represent the data passed to the children through DeviceArrival
and DeviceRemoval
(in this example I used string, it's not a string) without the use of generics etc...
To me this seems somewhat like overkill, but it's the only way I could think of doing this.
DeviceChild1
andDeviceChild2
actual class names? \$\endgroup\$