Alex Rivera | Logout

Is it ok to use C# Property like this

Asked 2010-07-27T08:27:57.357
9

One of my fellow developer has a code similar to the following snippet

class Data
{
     public string Prop1
     {
           get
           {
                // return the value stored in the database via a query
           }
           set
           {
                // Save the data to local variable
           }
     }

     public void SaveData()
     {
          // Write all the properties to a file
     }

}

class Program
{
    public void SaveData()
    {
         Data d = new Data();
         // Fetch the information from database and fill the local variable
         d.Prop1 = d.Prop1; 
         d.SaveData();
    }
}

Here the Data class properties fetch the information from DB dynamically. When there is a need to save the Data to a file the developer creates an instance and fills the property using self assignment. Then finally calls a save. I tried arguing that the usage of property is not correct. But he is not convinced.

This are his points

  1. There are nearly 20 such properties.
  2. Fetching all the information is not required except for saving.
  3. Instead of self assignment writing an utility method to fetch all will have same duplicate code in the properties.

Is this usage correct?

Edit
Report

1 Answer

0

In my opinion, that's an awful design. Using a property getter to do some "magic" stuff makes the system awkward to maintain. If I would join your team, how should I know that magic behind those properties?

Create a separate method that is called as it behaves.

answered 2010-07-27T08:35:18.057

Your Answer