I have a model which stores Id
(Int64
; for SQLite id column), Description
(string
), DueDate
(DateTime?
; null if not specified), and IsFinished
(bool
), such as
TodoItem.cs
public record TodoItem(Int64 Id, string Description, DateTime? DueDate = null, bool IsFinished = false);
and a ViewModel for the model.
TodoItemViewModel.cs
public class TodoItemViewModel
{
Int64 _id;
string _description;
DateTime? _dueDate;
bool _isFinished;
public Int64 Id => _id;
public string Description
{
get => _description;
set
{
this.RaiseAndSetIfChanged(ref _description, value);
}
}
// omits for DueDate, IsFinished properties
}
I want call the Database's update command when each property's setter executed. I am looking for some fancy way to pass 'which property to update'. Is there a feature for to pass property of the generic type T
like an enum? Code below for the example.
// The Type PropertyField<T> is my virtual thought
void Update(Int64 id, PropertyField<TodoItem> property, object value)
{
string sql = $"UPDATE tablename SET {property.PropertyName} = {value} WHERE id = {id}";
// execute sql command...
}
And call it by
TodoItemViewModel.cs
// ...
public string Description
{
get => _description;
set
{
this.RaiseAndSetIfChanged(ref _description, value);
Update(Id, TodoItem.Description, value); // update the description as new value for current todo item
}
}
// ...
It would be great if I can specify the corresponding type of the value (not object
, but string
for the above example).