Hi, I have a bit of a design dilemma at present. I have an abstract class Firmware
which handles file transfer (updating the firmware) and a few other things such as version.
The trouble is that I want to update the file paths of all MyFirmware
s in all Device
s that I have over the place. One way I can do this is to have a static list of Device
s in Device
which I iterate over updating Device.FilePath
, whenever I set FirmwareFilePath
or get an event from Firmware
that it's FilePath
has changed, remember to clean them up an so on.
** Edit - made this example more complete*
public class Firmware
{
private string _path;
public string Path
{
get { return _path; }
set
{
if (_path == value)
return;
_path = value;
OnPropertyChanged("Path");
}
}
}
public class Device
{
private static readonly List<Device> _Bars = new List<Device>();
private readonly Firmware _myFirmware = new Firmware() ;
public Firmware MyFirmware
{
get { return _myFirmware; }
}
public Device()
{
_Bars.Add(this);
Firmware.PropertyChanged += NewPath;
}
private void NewPath (object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Path")
{
foreach (var dev in _Bars)
dev.MyFirmware.Path = MyFirmware.Path;
}
}
}
Or I could use a "pointer" , change my Getter, Setter and Ctor slightly.
public class Pointer<TField>
{
private TField _backingField;
public TField GetValue()
{
return _backingField;
}
public void SetValue(TField value)
{
_backingField = value;
}
}
public class Firmware
{
private Pointer<string> _pPath;
public string Path
{
get { return _pPath.GetValue(); }
set { _pPath.SetValue(value); }
}
public Firmware (Pointer<String> pPath)
{
_pPath = pPath;
}
}
public class Device
{
private static readonly Pointer<String> _PPath = new Pointer<string>();
public static string Path
{
get { return _PPath.GetValue(); }
set { _PPath.SetValue(value); }
}
private readonly Firmware _myFirmware = new Firmware(_PPath);
public Firmware MyFirmware
{
get { return _myDevice; }
}
}
Is there any agreed upon reason why this would be bad practice? Is there any GC trap I haven't noticed?