DEV Community

Discussion on: MVC Tutorials Are Broken

Collapse
bbrtj profile image
bbrtj

Model is usually (in most frameworks) connected to database with some ORM. There were some frameworks which also used models for different stuff like form models. I actually liked that.
However, if your model is connected to the database, I wouldn't put any business logic into it, other than getting even more data from the database. ORM objects are rather hard to instantiate in tests and unstable to pass around in your application. I don't ever feel safe when my object has the ->save method.

  • you don't ever know what is going to save it and where
  • as mentioned earlier, creating a new instance of that model (not to get it into the database, just to have something to work on) gets an additional mental overhead
  • often harder to debug, as these objects contain much more than just the data you need
  • and they are actually coupled with the framework

So what I've been trying to do lately instead is to create DTOs (Data Transfer Objects) for pretty much everything and repositories to save and fetch DTOs. Models are just used by the repositories. It is much easier in Perl because with Moose MOP you can skip tedious creation of DTOs and methods to translate DTO to model (usually two per model).

DTOs are easy to create, safe to pass around and in my case, they already contain all the right type constraints (with Type::Tiny). I also created a common DTO method named ->dummy which creates a new offspring class for the DTO with all the same type constraints but with no required fields, so that I can create DTOs even if I don't technically have all the data it needs. I admit that it's not a tested solution but works pretty well where I use it, and when I talk with my coworkers about it (in PHP context) they often agree.

Let me know if this is interesting to you, I can extend that to a full dev.to post with some Perl examples and such

Collapse
ovid profile image
Ovid Author

I think seeing a dev.to article about that would be great!