Let's say I have the following class hierarchy...
Code:
public class Field
{
private int _id;
private sring _name;
public int ID
{
get {return _id;}
set {_id = value;}
}
public string Name
{
get {return _name;}
set {_name = value;}
}
public Field() {}
}
public class TextField : Field
{
private int _maxLength;
public int MaxLength
{
get {return _maxLength;}
set {_maxLength = value;}
}
public TextField() : base() {}
}
public class DropDownField : Field
{
private string _items;
public string Items
{
get {return _items;}
set {_items= value;}
}
public DropDownField() : base() {}
}
Basically, I have two classes (TextField and DropDownField) that inherit from Field. Let's also assume I am persisting these using a DB-generated ID and using a single table per class hierarchy mapping with a discriminator column (FieldType).
We have a GUI editor that allows a user to add, edit, and delete text fields or drop down list fields. But now, they want to be able to select a text field for example, and change the text field to a drop down list field. Well, the text field is used in many different places in the application (the ID is used to record data entered for a field for example), and we must keep the ID intact.
Is there a way to "convert" one field type to another field type, while maintaining the ID of the original field and making sure the changes are persisted to the same field?
Example...
Code:
TextField myField = Repository.GetByID(id);
if (typeHasChanged) // Maybe user chose different field type from a drop-down list.
// Convert myField, a TextField, to a DropDownField???
Repository.Save(myField);
The result would be that myField is now of the type DropDownField and the discriminator column for the field would now reflect that change.
I have shown a simplified version. We basically have about six different field types that all inherit from a base Field class. We need to be able to allow the users to change the field type of any one to any other one in this UI. Up to this point, I simply did not allow the changing of the type once the field was created. But, it seems like we should be able to do this.
I appreciate any help!
Kevin