Answer the question
In order to leave comments, you need to log in
How to validate the model before saving when using the Repository pattern?
In the application, access to entity data is carried out through a repository, which in turn uses ActiveRecord as a database provider to work.
Task : check the validity of the data received from the client, for example, this is a comment.
How would I do without the repository, primitively:
$model = new Comment(); // Comment наследуется от ActiveRecord
$model->load(Yii::$app->request->post());
$model->validate(); // true/false
Answer the question
In order to leave comments, you need to log in
when using the repository pattern, you should stop thinking about the model that travels through the system as a place where input validation should happen. This approach has gained popularity thanks to RAD-frameworks (Yii and similar) - the simplification of which aims at fast development speed.
If you look at the architecture from the point of view of "purity", then:
A model that travels through the entire system must contain only domain logic, or not contain it at all (ie, in fact, be a Value-object).
The validation of input data is described by a separate model - the request model - and contains the logic of validation / mapping / etc.
those. a more or less standard approach from the side of extensibility, reliability, testability and application flexibility to subsequent changes is to have validation / parsing of input data separately for each case (for example, separately for web forms and separately for JSON / XML / etc API, of course, fragments can be reused, inherited or mixed in, etc.), models - separately, work with storage separately.
The last you have already done = the repository is just that.
So your assumption is correct.
The repository (storage) must contain the logic for saving / searching / restoring objects. Validation is, as it were, not his task. In enterprise frameworks, separate entities are created for this.
As for Yii, ActiveRecord has several responsibilities in it: it is an entity with business logic, a validator, a repository, a life cycle, etc. You moved the storage logic to the repository, good. Now you need to move the validation logic somewhere.
But, on the other hand, why not use the functionality of the framework and leave the responsibility of validation in the model? Some super complex validators can be moved to separate validator classes.
What I'm saying is that if you want to endure all this, and go against the architecture of the framework (which is not so easy), then why use Yii? Maybe it's better to immediately take a framework that is more suitable for your architecture?
In the case of a repository, you have an entity that contains the data that the repository will process. Usually the validation is done in the entity's setters, or constructor. You can also perform it in the repository, in which case you can process the entity by interface.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question