I'm using Dapper for handling database connection in a .NET project. The automatic column mapping works well and I can also map columns to different property names in the model. However, how can I map computed properties? For example, my model is
class User
{
public int Id {get; set;}
public string Name {get; set;}
public bool IsPremiumUser {get; set;}
}
And the table is
Id | Name | CreationDate | IsPremiumUser
Now, IsPremiumUser can be null in db but not in the model. I'd like it to be mapped by the following logic:
if (row.IsPremiumUser != null)
{
model.IsPremiumUser = row.IsPremiumUser;
}
else
{
model.IsPremiumUser = row.CreationDate < 1.1.2000;
}
In other words, its value depends on 2 columns. Also there are multiple cases where I'd like to set a boolean property based on if a certain relationship exists. How to handle these more complex mapping cases?
IsPremiumUserwas null (when you read that), should it be updated and has a value?