E
E
Evgeny Glebov2016-02-25 10:34:10
SQL
Evgeny Glebov, 2016-02-25 10:34:10

Where is the best place to set limits?

Hello. A question. Where it is better to put restriction for types?
For example, the mass should not exceed a certain size. This restriction can be set in several ways:
1) in the database itself
2) on the entity:

class User
{
   private int years = 0;
   public int Years { 
      get { return years; };
      set { 
              if(value > 200 || value < 0)
              {
                   value = 0;
                   Logger.Instance.Warning(тут в лог записываем stacktrace места, где пыталось записать неверное значение);
              }
              years = value; 
            }
   }
}

3) on the repository:
class UserRepository
{
   ...
   public void SetYears(User user, int years)
   {
      //код похожий как и во втором пункте.
   }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2016-02-25
@GLeBaTi

An entity must know about itself and its behavior.
For access methods can be different and you can migrate the base

D
Dmitry Kovalsky, 2016-02-25
@dmitryKovalskiy

It's too late at the base. A request that will be canceled a priori will crawl through all the circles of hell and also turn to the database. These are all resources. Use client-side validation with server validation. For example, at the stage of creating a model.
Well, a small PS - your code makes a little surprise for the client using it. Without access to the implementation, I won't know why I set the property to 9000, but it's still 0. Either throw an exception informing about invalid data, or at the client logic level, tell me that I entered crap.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question