Should the Trim method be at the data access level or domain level?

I am dealing with a database containing data with inconsistencies, such as a white start and back space.

In general, I see that many developers practice security coding, cutting off almost all the lines that come from the database, which may have been entered by the user at some point. In my oppinoin, it is better to do this formatting before the data is saved, so that it will be done only once, and then the data can be in a stable and reliable state. Unfortunately, this is not the case, but it leads me to the next best solution using the Trim method.

If I clip all the data as part of my data access level, then I don’t need to do security in the business objects of my domain. If instead I put responsibility for cropping in my business objects, for example, using a set of access elements to my C # properties, I should get the same results on the network, however cropping will work with all the values ​​assigned to the properties of my business objects , and not just those that come from an inconsistent database.

I think this is a somewhat philosophical question that can determine the answer that I can ask, “ Should the domain level be responsible for the protective / forced formatting of the data? ” It would be reasonable to have a set of accessories for the PhoneNumber property on the business object accepting an unformatted or formatted string , and then tries to format it as necessary, or will this responsibility be transferred to the presentation and data access layers, leaving a more stringent level of the domain in the type of data that it will accept? I think this may be a more fundamental question.

Update: The following are a few links that I thought I should share with this topic.

Information Service Templates, Part 3: Data Cleansing Template

LINQ to SQL - formatting a string before saving?

How to trim values ​​with Linq to Sql?

+4
source share
3 answers

I would suggest "clear" the data at the application level. The reason you want to do this here (yes, higher up the stack as suggested by Dev Art) is because your domain model should "model" the domain as close as possible. What if at some point all the data is "clean"? Well, then you may need to remove the helper method that does the “cleanup”. It’s easier to remove it from a location higher in the application stack.

Use a cool extension method that uses reflection (don’t start by thinking slowly until you know how it works ) or digging into all the “string” properties (for example) of your domain object graph. Here is an example that this method uses to set DateTime values to a fixed offset — note how it will “offset” all DateTime values ​​even deep in collections or other custom types. In your case, compensation will be your crop. This, of course, is easier than adding .Trim() throughout the show and can be easily separated.

Remember that bad data is an end-to-end task for your domain and therefore should not be bound directly to it (think AOP ).

+3
source

Data must be cleared before it is saved. Now that it is saved, you have unclean data that should probably be cleared in the database. Consider finding a customer by name. Can I find "John," "Dow," if what I kept was "John," "Dow."

Clearing data close to the user interface allows you to have much simpler code. Defensive code can vary from cleanup code to claims. (i.e. assert string = trimmed (string)). To get to this point, you will need to clear the database, as well as the user interface code.

+2
source

better to do this formatting before saving data

That's right.

Should the domain be later responsible for the security / enforcement of data formatting?

With the data currently saved, you will not find a suitable place for cropping. because the consistency of your storage is broken.

You can try self-healing. Read the data and crop it somewhere before displaying in the dialog. As soon as the user saves this dialog, the data in the database becomes "fixed".

As for the new input, I am inclined to believe that cropping data is a cleanup operation that does not belong to either the domain layer or the data layer. User input should be "cleared" somewhere near the user interface level before you start working with this data.

0
source

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


All Articles