![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Domain Object Validation
I am curious how you guys handle domain object validation. Say for example I have an employee class. Currently I perform the validation inside the class itself. So for instance if someone sets an employee.FirstName property to an invalid length, an exception is thrown. Is it good practice to perform validation in the domain object, or should it be in the business layer?
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
As a general rule,I would perform validation in the domain object, as I would essentially make each object responsible for ensuring it is in a valid and usable state. That might mean throwing exceptions, or it might mean simply ignoring invalid changes. Exceptions to this general rule might include cases where one object is responsible for managing the state of another (eg X manages two other objects A and B, and enforces some relationship between them) but, even then, I tend to prefer having the individual objects responsible for their own state (eg A and B manage their own state, and X only manages the interrelationship between the pair).
Each object needs to have an interface definition (eg a set of arguments, preconditions, postconditions for each operation), and the business layer needs to respect the rules of each object's interface. It is certainly appropriate for the objects to ignore or complain bitterly (e.g. throw an exception) if the business layer doesn't obey the rules. But the "business layer" can have responsibility for validation (eg if the business layer rather than another object manages relationships between objects). |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Objects should, in my opinion, be responsible for the data they receive. Objects should degrade gracefully on invalid input, throwing the appropriate exceptions and allowing the next layer up to worry about what to do about it.
However, for any non-trivial system, there should also be some kind of shared validation system. For instance: from attributes import *
class Person(Attributes):
name = String(blank = False)
age = Integer(minimum = 0)
phonenumber = PhoneNumber(max_length = 10) |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
All right this makes sense. One other thing, do you guys think it is overkill to validate the length of every field to match the length specified in the database schema?
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Hm. Well, presumably your database will be perfectly capable of handling inputs that are too long, though most databases seem to cut off the end, rather than raise any sort of exception. If the length of your field is some large number that you hope most users will never even get near, then you could skip length validation, so long as it's not a security risk. If the length is small, then it does make sense to validate.
Really though, length validation should take up very little code, especially if done right, so I see no reason not to do it. |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
In addition to what Arevos said, it depends on where you're getting data from to feed into the database and what you're doing with the data you get back from it. For example, if you're getting data from a GUI, it is arguably better to validate data (and, if necessary) pester the user to get it right before inserting it into the database. That will avoid future surprises when the data is retrieved (and you may be able to avoid any need to validate data on retrieval). However, if the database is being fed by some other system, it may be appropriate to validate the data as you retrieve it. If you really care about security and redundancy, you will always validate data you get from another system (whether a GUI or database) or send to another system. That helps ensure your component systems respect each others interfaces, and can avoid situations were corruption of data propagates.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|