Lightman
Lightman

Reputation: 1265

Parameter validation vs property validation

Most (almost all?) validation frameworks are based on reading object's property value and checking if it obeys validation rules.

  1. Do we really need it?

If we pass valid parameters into object's constructor, property setters and other methods, object seems to be perfectly valid, and property value checks are not needed!

  1. Isn't it better to validate parameters instead of properties?

  2. What validation frameworks can be used to validate parameters before passing them into an object?

Update

I'm considering situation where client invokes service method and passes some data. Service method must check data, create / load domain objects, do business logic and persist changes.

It seems that most of the time data is passed by means of data transfer objects. And property validation is used because DTO can be validated only after it has been created by network infrastructure.

Upvotes: 0

Views: 307

Answers (2)

Fendy
Fendy

Reputation: 4643

This question can spread out into wider topic. First, let's see what Martin Fowler has said:

One copy of data lies in the database itself. This copy is the lasting record of the data, so I call it the record state.

A further copy lies inside in-memory Record Sets within the application. This data was only relevant for one particular session between the application and the database, so I call it session state.

The final copy lies inside the GUI components themselves. This, strictly, is the data they see on the screen, hence I call it the screen state.

Now I assume you are talking about validation at session state, whether it is better to validate the object property or validate the parameter. It depends. First, it depends on whether you use Anemic or Rich Domain Model. If you use anemic domain model, it will clear that the validation logic will reside at other class.

Second, it depends on what type of object do you build. An Framework / operation /utility object need to have validation against object property. e.g: C#'s FileStream object, in which the stream class need to have valid property of either file path, memory pointer, file access mode, etc. You wouldn't want every developer that use the utility to validate the input beforehand or it will crash in one operation, and giving wrong error message instead of fail fast.

Third, you need to consider that "parameter can come in many sources / forms", while "class / object property only has 1 definition". You need to place the parameter validation at every input source, while object property validation only need to be defined once. But you also need to understand the object's state. An object can be valid in some state (draft mode) and not in other state (submission mode).

Of course you can also add validation into other state level as well, such as database (record state) or UI (screen state), but it also have different pros/cons.

What validation frameworks can be used to validate parameters before passing them into an object?

C#'s ASP.Net MVC can do one kind of parameter validation (for data type) before constructing into an object, at controller level.

Conclusion

It depends entirely on what architecture and kind of object you want to make.

Upvotes: 1

ekostadinov
ekostadinov

Reputation: 6940

In my experience such validations were done when dealing with complex validation rules and Parameter object. Since we need to keep the Separation of concerns - the validation logic is not in the object itself. That's why - yes we

we really need it

What is more interesting - why construct expensive objects and later validate them.

Upvotes: 0

Related Questions