S
S
Sergey Ilichev2021-04-09 18:15:21
PHP
Sergey Ilichev, 2021-04-09 18:15:21

What can be used from a framework in DDD?

Hello colleagues!

Another question came up during the design process.

I am currently refactoring a project based on the yii2 framework. In fact, it could be rewritten in Symphony and simultaneously study this new framework for me, but I decided to try to slowly learn how to apply the DDD approach, which is new to me, and do all this with Yii2 under the hood. It is clear that this framework is not conducive to such an approach, but as far as I understand, with the right skill, DDD should not be tied to a framework at all.

Now I constantly encounter problems that when I write some component, one way or another, it is tied to the framework. And I honestly do not understand how to write without being tied up.

Let's say I'm writing a factory, a factory that returns me a data model. In Yii2, a model is either just a Model class not tied to a database, which has load methods for loading data into the model, validate, rules with validation rules, save, and so on, or there is an ActiveRecord model with the same set of properties and methods, since in depth it is also inherited from Model, only it is tied to the database, that is, when an ActiveRecord instance is received, it is filled with data from the database.

This factory, in fact, should not be tied to a framework, since, in fact, it should be somewhere in the domain. But in this factory, in theory, I should create instances of Model or AR models, do, for example, $ model instanceof Model or AR, in order to understand that we generally work with what we need and actually call their load method in the same place to load the necessary data there . And therefore there is a binding to these models and the framework.

The option that I see is to completely abandon the use of Model and ActiveRecord, and make your own custom model, which will do the same thing and it will have its own interface, which we will rely on in the factory by making $model instanceof SomeModelInterface.

But here you need to understand that this means simply rewriting the logic of the framework manually. And the question arises why is it needed at all then, if I rewrite all its conveniences in the form of models with the logic of saving, validating, loading data and other things? That is, in fact, I will have to create an interface for these models that describes everything that is needed, such as the same methods load, save, validate, getErrors and many others, then implement all this in the base class of the model and inherit all other models from it, instances of which to create through the factory. You can probably just copy everything that is in the Model framework, then, like, call it your model and use it)))

Yes, after that I can take, cut this case out of the project and shove it into Symphony, for example, but again the question is - why? if nothing is used from the framework anyway. Maximum routing and controllers?

What is ultimately left of the framework, and what is the point of writing something based on it, if in fact it will slow down development at times? That is, we once wrote on cms, then it became sloppy (we all understand why), we switched to frameworks, now we are moving away from them almost towards pure php?)

Or is it not happiness at all in DDD?)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
k2lhu, 2021-04-09
@k2lhu

When working with Yii2, you should initially put any components into wrappers and use them according to the project, so you can easily get rid of the implementation using the wrapper interface.
As for DDD when working with Yii2 and models, it would be ideal to divide the application into different layers, completely separating any Yii2 dependencies from the middle layers, but I would not endow models with an interface, in this regard, creating your own repository will help you, in which you can hide any methods on selections with the help of smart models, and to give out already independently mapped Entities and already endow them with interfaces and use them further, but this is just for selections of specific records. If you need to select multiple records, use Entity almost the same way, create your own custom collection that implements the built-in Iterator and Countable interfaces. To separate them, you can use the base class as a parent, and then create the desired collection class for the mapped Entities.
But your whole ideal idea of ​​DDD with Yii2 will easily fall apart when using ActiveRecord and basic models - they immediately shove validation, and cast, and processing behaviors for saving / updating / deleting, so even in old projects they also hang triggers often. Maybe you should think about whether you need DDD here at all? If this is done in order to get rid of the framework at any time, then use Entity, Collection for models and take everything to the repository at once, you can easily migrate to the same symphony. There is no painless transition and one way or another you will have to rewrite something, but you can do it all by simply changing the code of your repositories and controllers with requests.

D
dmitriy, 2021-04-15
@dmitriylanets

your direction and thoughts completely coincide with mine, so I will add what k2lhu already wrote
DDD is more about aggregates and context, maybe you need a hexagonal architecture and everything related to clean architecture. The principle is simple, your business logic should not depend on implementation details, try writing code without a framework, for example, saving, displaying simple entities, users. You will have repositories that do not work with the base, but simply Mock repositories, but that implement interfaces. This will give you a Domain layer and a very thin Infrastructure Layer. Next, try to implement business logic and scripts for working with your entities, for example, user registration, you will have an Application Layer. Next, you need to organize controllers or modules that will display interface elements, you will create controllers, views, modules (widgets), and so on. e.g. user registration form, so you have a Presentation Layer. Next, you will move your repositories, adapters to dynamics and implement saving your users to the database using Activerecord or DataMapper. This is how the Infrastructure Layer appears.
Pluses, the business logic does not depend on the framework, at each stage of the layer you can connect the framework at the level of at least the Infrastructure Layer, Presentation Layer. When changing the framework, change only them.
Tests can be implemented without problems, especially at the domain and business logic levels.
Golden words of Uncle Bob:

When you write a framework application for a customer, you guarantee the development of the application and its support throughout the life cycle, but what guarantee does the framework developer give you?

A
Anton Shamanov, 2021-04-09
@SilenceOfWinter

DDD is more about teamwork than programming. yii2 is outdated, modules for 3ki are already available and not even buggy)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question