lundi 26 mars 2018

C# Update Modified properties in database

I have a class having a lot of properties which can be modified from outside. At the end of setting all the properties I wanted to update only the modified fields in database, so what would be the efficient way?

Eg. Let say below is my class

public class SomeClass {
    private int field1;
    private string field2;
    private string field3;
    private byte field4;

    public int Field1 {
        get { return field1; }
        set { field1 = value; }
    }

    public string Field2 {
        get { return field2; }
        set { field2 = value; }
    }

    public byte Field4 {
        get { return field4; }
        set { field4 = value; }
    }

    public string Field3 {
        get { return field3; }
        set { field3 = value; }
    }

    public SomeClass() { }

    public bool FillClassVariablesWithCurrentDatabaseValue() {
        //Fill the class variables with current database values
    }

    public bool UpdateModifiedFields() {
        //Only update the modified fields in database
    }
}

Now to know whether the field has been updated or not, I will have to add that many boolean variables in the class as below

public class SomeClass {
    private int field1;
    private string field2;
    private string field3;
    private byte field4;

    private bool field1Updated;
    private bool field2Updated;
    private bool field3Updated;
    private bool field4Updated;

    public int Field1 {
        get { return field1; }
        set {
            if (field1 != value) {
                field1 = value;
                field1Updated = true;
            }
        }
    }

    public string Field2 {
        get { return field2; }
        set {
            if (field2 != value) {
                field2 = value;
                field2Updated = true;
            }
        }
    }

    public byte Field4 {
        get { return field4; }
        set {
            if (field4 != value) {
                field4 = value;
                field4Updated = true;
            }
        }
    }

    public string Field3 {
        get { return field3; }
        set {
            if (field3 != value) {
                field3 = value;
                field3Updated = true;
            }
        }
    }

    public SomeClass() { }

    public bool FillClassVariablesWithCurrentDatabaseValue() {
        //Fill the class variables with current database values
    }

    public bool UpdateModifiedFields() {
        //Only update the modified fields in database
        string parameters = string.Empty;
        if (field1Updated)
            parameters += "field1=@field1";
        if (field2Updated)
            parameters += "field2=@field2";
        if (field3Updated)
            parameters += "field3=@field3";
        if (field4Updated)
            parameters += "field4=@field4";

        //code to update these fields
    }
}

But since my class has a lot of variables, I don't think this approach is good. Any suggestion will helpful.

Aucun commentaire:

Enregistrer un commentaire