Why should the recipient of C # properties not be retrieved from the database?

Some answers and comments on this question: Simple C # code for polling a property? , imply that retrieving data from a database in a property receiver is usually a bad idea.
Why is it so bad?

(If you have sources for your information, list them.)


I will usually store the information in a variable after the first โ€œgetโ€ for reuse if this affects your answer.

+6
source share
7 answers

Because retrieving data from the database can cause any number of exceptions, and getters properties should generally never throw exceptions.

The expected behavior of the getter property is simply to return a value; if he really does much more, this should be a method.

I know that there is documentation from Microsoft that gives reasons not to throw exceptions in more detail, but I canโ€™t find it in my hand. If I do, I would replace it with a link to it. Perhaps this is so, but I'm not sure: http://msdn.microsoft.com/en-us/library/bzwdh01d%28VS.71%29.aspx#cpconpropertyusageguidelinesanchor1

+9
source

This is bad because (incidentally) he violates the principle of least surprise .

Programmers usually expect properties to do simple get / sets. Encapsulating data access in a property that can throw exceptions, cause side effects, and change the state of data in the database is not what is normally expected.

I am not saying that there is no case for complex properties - sometimes this can be a good solution. But this is not the expected way to do something.

+5
source

Short version: Creating a getter property, directly accessing the database, violates the principle of separation of problems ./p>

More: In general, a property is intended to represent data associated with an object, such as the FirstName property of a Person object. Property values โ€‹โ€‹can be set both internally and externally, but the action of changing and receiving this data at the facility should be separate from the action of retrieving or transferring data to the persistent storage.

+5
source

Each time you contact the recipient, you make another call to your database.

+3
source

Apart from the exceptions, the database query is probably a slooooow operation. In both aspects, using getters properties as database accessories violates the least surprise principle for client code. If I see a library class with a property, I donโ€™t expect it to do a lot of work, but it will also get access to a value that is easy to get.

The least surprising option here is to provide the old-fashioned simple Get function.

+2
source

A getter, by definition, should simply encapsulate data, not functionality.

In addition, you are likely to redefine functionality and make many trips to the database if you had more than one recipient that needed to be made round-trip to the database. Why not handle it in a centralized place, and not split it into multiple properties?

Methods are intended for modification and functionality of data at the class level, properties are intended only for individual modification and data extraction.

+2
source

If you really only retrieve a value from the database and not write it back, for example a read-only property, there is nothing wrong. Especially if a property cannot exist without its parent. However, when implemented, this can cause maintainability problems. You associate the process of retrieving stored information with access to this information. If you continue to follow this template for other properties and change the settings of your storage system, this change can spread throughout your code base. For example, changing the type of a table or column. This is why it is bad that you are calling the database at your destination.

On the side of the note: if the database throws an exception when trying to get the value, then it is obvious that there is an error in your code (or the calling client code), and the exception will still be displayed regardless of where you put the data access code. Multiple data is supported by some kind of collection inside the class that can throw exceptions, and standard practice is to store property values โ€‹โ€‹this way (see EventHandlerList ).

Properties were designed specifically because programmers must perform additional logic when retrieving and setting values, such as validation.

For all that has been said, review your code and ask yourself: โ€œHow easy will it change later?โ€ from there you should be on your way to a more convenient solution.

+1
source

Source: https://habr.com/ru/post/892698/


All Articles