samdark / yii2-cookbook

Yii 2.0 Community Cookbook
1.45k stars 296 forks source link

MVC: Any example snippets or external links? #187

Closed robsch closed 6 years ago

robsch commented 6 years ago

ActiveRecord classes should not contain any significant business logic. It deserves to be in separate classes which are built according to SOLID and Dependency Inversion. Don't be afraid to create your own classes which are not inherited from anything from the framework.

How should the ActiveRecord classes be 'connected' to the actual model? Currently, I'm not having non-Yii classes, since I'm not sure how to do that.

One reason is also because it is easy to configure the model objects with the means of Yii's DI.

The other is that I don't know how to combine the ActiveRecord object with the model objects: Starting from a controller action, how should I get e.g. an Customer model object? Currently I call [Customer::findOne(...)](http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#findOne()-detail). Should this function get overridden so that it returns an model object? And then, if the name of the customer should be set, should I have a method CustomerModel::setName($name) which sets the name at the ActiveRecord object and then saves it? Wouldn't that create lots of boilerplate code, repeating any property that may get set or read? Or is this quite normal?

I would love to design my projects as good as possible, but I don't have an idea how to do it when I should separate the model from the framework. Maybe it is not so Yii related, but then it would be nice if you could link to some appropiate documentation if possible.

This might also an inspiration for improving the Yii framefork, so that the separation can be done more easily - if there is anything that could help developers.

samdark commented 6 years ago

How should the ActiveRecord classes be 'connected' to the actual model?

Depends on how your model is implemented. The thing is that "model" in MVC isn't a single class but a whole layer implementing application domain logic. Since you don't have any and your app is basicall CRUD, you don't have anything to connect to and basically your ARs are your model. In this case it's fine to use it as is, no need to introduce unneeded complexity. If you'll add extra layer without a need for it, it will clearly be just boilerplate that you don't need.

If you need M from MVC and it's complex, you usually want to have Entities that aren't DB dependent. These are obtained and saved through repositories.

I would love to design my projects as good as possible, but I don't have an idea how to do it when I should separate the model from the framework.

When it's complicated enough for it to make sense.

This might also an inspiration for improving the Yii framefork, so that the separation can be done more easily - if there is anything that could help developers.

Framework isn't providing you complete application architecture. Some minor elements of it and infrastructure - yes. But not a complete architecture. That's you who decides on it and builds it.

robsch commented 6 years ago

Thanks for your reply. Repositories are the key - for my personal understanding. Should read about that... Understanding of 'Model' was clear to me, i.e. it is a model layer with the actual domain logic.

I agree: to avoid over-engineering - if you have a real live too :-D.

In my opinion all is about organizing things (code) so that it is easy to develop and to maintain. One (and others) should understand the code quickly. From this point of view and as a totally different approach, I find it also quite appealing to use standalone Action classes . Although I haven't done this yet, I think to put related stuff into one class/file as far as it is applicable is another good way to structure an application.

Thanks again.