I have a product that stores non-volatile values in an EEPROM during the production process. I currently model the EEPROM as a struct so I can easily Marshal to/from a buffer.
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public class EEPROM
{
public const ushort MEMORY_VERSION = 0x01;
ushort memoryVersion;
byte value1 = 0;
byte value2 = 0;
public EEPROM() : this(MEMORY_VERSION) { }
protected EEPROM(ushort memoryVersion) { this.memoryVersion = memoryVersion; }
}
Some time after these units have been fielded, I may need to add a new EEPROM setting. I need to come up with a way to handle in-field updates to the EEPROM. Our upgrade procedure wouldn't support an external application well, so I need to handle the upgrade in system.
My first thought was to add a virtual Upgrade method.
public virtual void Update() {}
and then inherit the out-dated EEPROM structure in a new one
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public class EEPROM_V2 : EEPROM
{
public new const ushort MEMORY_VERSION = 0x02;
byte value3 = 0;
public EEPROM_V2() : base(MEMORY_VERSION) { }
protected TestEeprom(ushort memoryVersion) : base(MEMORY_VERSION) { }
public virtual void Upgrade() { base.Upgrade(); value3 = 0; }
}
But this would force clients of EEPROM to change to EEPROM_V2. I have to be overthinking this. If I simply append new values, I could easily end up with a nasty if statement chain.
if( memoryVersion == X) { do this }
if( memoryVersion == Y) { do this }
etc...
Aucun commentaire:
Enregistrer un commentaire