I'm trying to create a class to use as a field in other objects that can hold a temporary unsaved value until a SaveChanges method is called on the object, so that I can pass the value to a stored proc, which will update any non-null fields with the new value, and leave fields with null as their original values.
I want to be able to do this:
private Field<int> MyInt;
private Field<int?> MyNullableInt;
private Field<string> MyString;
and then be able to use the Field objects like this:
MyInt = new Field<int>(1); // initialise with a value
MyInt.Value = 2; // assign a new value internally marking it as having a new value
int x = MyInt.Value; // read the current value
int? newInt = MyInt.NewValue; // read the newly set value, or null if there is no new value
I want to be able to create fields that are:
- value types, such as int, bool, etc that have a NOT NULL constraint in the db.
- value types, such as int?, bool?, etc that allow NULLs in the db.
- reference types, such as string, that are always nullable.
I'm still learning about Generics, but I'm starting to understand them, however, I'm a bit stuck on this because I can create a Field object as needed above that works with a non-nullable type, or a nullable type, but not both.
This is what I've come up with so far:
protected class Field<T> where T : struct {
private T _Field;
private bool _IsNew;
public Field(T InitialValue) {
_Field = InitialValue;
_IsNew = false;
}
public T Value {
get { return _Field; }
set {
if (!_Field.Equals(value)) {
_Field = value;
_IsNew = true;
}
}
}
public Nullable<T> NewValue {
get {
if (_IsNew) {
_IsNew = false;
return _Field;
}
return null;
}
}
public bool IsNew { get { return _IsNew; } }
}
This works well. I can also change the type constraint to where T : class
, and change Nullable<T>
to just T
on the NewValue method, which makes it work with nullable types, but how do I get it working with all data types? Do I really need to create two different classes to handle the two different scenarios?
Thanks, Ben