mardi 17 mai 2016

What is the best approach to update a database field when a corresponding class property changes?

I have a class with several fields.

public readonly byte         Id;
public          bool         Active       { get; set; }
public          List<Group>  Groups       { get; set; }
public          string       FirstName    { get; set; }
public          string       LastName     { get; set; }
public          Gender       Gender       { get; set; }
public          string       IdCard       { get; set; }
public          List<string> PhoneNumbers { get; set; }
public          string       Address      { get; set; }
public          string       PicturePath  { get; set; }
public          string       Comments     { get; set; }

In the database, I have a table with corresponding fields. I want that whenever I change the value of this fields, the corresponding field in the table will be updated too.

My first instinct was to do so in the set accessor, but I have read that "property getters are expected to be fast and idempotent", and database querying is not always fast.

So I thought maybe to create a method UpdateDatabase() which should be invoked by the user of the object whenever he changes the value of one of those properties. Then the method updates the database with the new values. But it's very tedious...

Which approach is better? Would you suggest another approach completely?

Aucun commentaire:

Enregistrer un commentaire