Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to use C# Property like this

Tags:

c#

.net

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?

like image 796
ferosekhanj Avatar asked Jul 27 '10 08:07

ferosekhanj


People also ask

Does anybody use C anymore?

Despite the prevalence of higher-level languages, C continues to empower the world. The following are some of the systems that are used by millions and are programmed in the C language.

Why is C so unsafe?

C and C++ are unsafe in a strong sense: executing an erroneous operation causes the entire program to be meaningless, as opposed to just the erroneous operation having an unpredictable result. In these languages erroneous operations are said to have undefined behavior.

Is C an outdated language?

No, C is not an outdated language. Whether a language is outdated depends on more than just whether you can easily find a programmer in it. You'll find it harder to find a Lisp, Haskell, or Forth programmer, but it doesn't mean those languages are outdated either.

Is C still used Quora?

Short Answer: Yes. It is still a significant computer language for professional programmers.


2 Answers

I don't think that another developer who will work with the same code will be happy to see :

d.Prop1 = d.Prop1; 

Personally I would never do that.

Also it is not the best idea to use property to load data from DB. I would have method which will load data from DB to local variable and then you can get that data using property. Also get/set logically must work with the same data. It is strange to use get for getting data from DB but to use set to work with local variable.

like image 181
Incognito Avatar answered Sep 17 '22 18:09

Incognito


Properties should really be as lightweight as possible.

When other developers are using properties, they expect them to be intrinsic parts of the object (that is, already loaded and in memory).

The real issue here is that of symmetry - the property get and set should mirror each other, and they don't. This is against what most developers would normally expect.

Having the property load up from database is not recommended - normally one would populate the class via a specific method.

like image 36
Oded Avatar answered Sep 18 '22 18:09

Oded