I know title is very confusing. Below is my scenario
I have a contract object for my Service like the following.
class UpdateRequest
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public decimal Salary { get; set; }
}
Now the contract object of above class is passed to a method that updates a record in the database. Suppose I want to update an employee with an Id: 33 but I only want to change its name and I want to leave the Salary alone.
One way is to not worry about such details and update everything, but that would require the clients of my service to pass me the Salary value too so it does not get overwritten to a default(decimal) value.
Another way I could think of is to create the following type
public class TrackedValue<TInternal> : where TInternal:new
{
private TInternal value = default(TInternal);
public TInternal Value { get; }
private bool initialized;
public static implicit operator TrackedValue<TInternal>(TInternal v)
{
return new TrackedValue<TInternal> { value = v, initialized = true };
}
}
Is there any other way ? Does .NET not have anything like above already available ? I am assuming if they have things like Nullable they have got to have something for this problem too.
Nullable types wont help me. There are use-cases where I have to change an existing value of some table-column to null. That would throw it off.
UPDATE: Some people might say, My service should check property values against default for that type. Default value for 'bool' is false, if my methods are called with a bool property value to false, it will break my logic.