Posts tagged with design patterns

Using view models

stitcher.io

Brent explains what view models are and how you can use them. We use view models in almost every project at Spatie.

In essence, view models are simple classes that take some data, and transform it into something usable for the view. In this chapter I'll show you the basic principles of the pattern, we'll take a look at how they integrate in Laravel projects, and finally I'll show you how we use the pattern in one of our projects.

Read more [stitcher.io]

No, disabling a button is not app logic

dev.to

David Khourshid explains how you can use state machines to make your logic more resilient.

Disabling a button is not logic. Rather, it is a sign that logic is fragile and bug-prone. In my opinion, disabling a button should only be a visual cue to the user that clicking the button will have no effect.

Read more [dev.to]

Join thousands of developers

Every two weeks, I share practical tips, tutorials, and behind-the-scenes insights from maintaining 300+ open source packages.

No spam. Unsubscribe anytime. You can also follow me on X.

4 Ways The Laravel Service Container Helps Us Managing Our Dependencies

christoph-rumpel.com

Christoph Rumpel wrote a clear post on how you can use Laravel's service container.

The service container is a quite complex topic, and I see many struggling to understand what it does. It was the same for me, and the main reason is that many explanations concentrate on "how" to use the container. With this article, I want to give you my introduction to this topic by focusing on the "why" and "when" the container can help us with our dependencies.

Read more [christoph-rumpel.com]

Domain Events vs. Event Sourcing

www.innoq.com

Christan Stettler wrote an interesting post on why domain events and event sourcing should not be mixed up.

Event sourcing and domain events can of course be used both at the same time, but should not influence each other. The two concepts are used for different purposes and should therefore not be mixed.

Read more [www.innoq.com]

Refactoring to actions

by Freek Van der Herten – 6 minute read

In our recent projects at Spatie, we've started using a concept called "actions". It keeps our controllers and models skinny. It's a straightforward practice. In this blog post, I'd like to explain it to you.

Read more

SOLID, interfaces and final

stitcher.io

My colleague Brent started a new podcasts series called "Rant with Brent". In the first episode has explains why he likes the "final" keyword in PHP.

I'm giving podcasting a try. Let me know what you think of it via Twitter or e-mail. You can download the episode here. In this episode I talk about why I think final helps you write better maintainable code.

Read more [stitcher.io]

Hand-written service containers

matthiasnoback.nl

Matthias Noback makes the case for writting your own service containers.

Dependency injection is very important. Dependency injection containers are too. The trouble is with the tools, that let us define services in a meta-language, and rely on conventions to work well. This extra layer requires the "ambient information" Paul speaks about in his tweet, and easily lets us make mistakes that we wouldn't make if we'd just write out the code for instantiating our services.

Read more [matthiasnoback.nl]

Domain Events vs. Event Sourcing

www.innoq.com

Christian Stettler explains why domain events and event sourcing should not be mixed up.

What do domain events have in common with event sourcing? Certainly the word “event” in the name. But beyond that, when talking to architects and developers in projects, at conferences or trainings, I often hear that domain events go well with event sourcing and that event sourcing is an ideal source of domain events. In this blog post I would like to outline why I personally do not share this view.

Read more [www.innoq.com]

Q&A on the Book Refactoring - Second Edition

www.infoq.com

Refactoring is an excellent book written by Martin Fowler. He recently released a second edition. I'm reading it now and can recommend it to anyone interested in writing better code. Here's an interview with the author on the second edition of the book.

InfoQ interviewed Fowler about the major changes in the 2nd edition of Refactoring, how to recognize code smells and refactor code, how code reviews and refactoring support each other, what tech leads can do to encourage refactoring, the benefits refactoring brings, using tools for refactoring, and mob programming.

Read more [www.infoq.com]

Giving collections a voice

timacdonald.me

Tim MacDonald demonstrates how you can add logic to a custom collection.

Laravel collections have become an essential part of my codebases and I couldn't imagine working without them. I have found giving collections the voice of the problem domain makes for a much nicer API when compared to the generic collection methods.

Read more [timacdonald.me]

The Integration Operation Segregation Principle

frederickvanbrabant.com

In a new post on his blog Senior CEO Frederick Vanbrabant explains the Integration Operation Segregation Principle, which is programmerspeak for splitting your code into nice little testable bits.

As you can see the Integration Operation Segregation Principle is just a long and complicated term to describe something very simple. This all might seem like a lot of work, but it’s worth it. Your code and especially your tests will thank you later

Read more [frederickvanbrabant.com]

Introducing Laravel view models

stitcher.io

My colleague Brent released another awesome package called laravel-view-models. On his blog he shares some good examples and uses cases.

In essence, view models are simple classes that take some data, and transform it into something usable for the view. In this post I'll show you the basic principles of the pattern, we'll take a look at how they integrate in Laravel projects, and finally I'll show you how we use the pattern in one of Spatie's, our company, projects.

Read more [stitcher.io]

Reusing domain code

matthiasnoback.nl

Here's another excellent post by Matthias Noback.

Reuse-in-the-small is definitely possible. Reuse-in-the-large is deemed to be impossible, because no two problems/projects are alike, but practice proves otherwise. There are reusable components covering entire subdomains, which are nonetheless quite successful. The chance of success is bigger if such a reusable component is used to cover for a generic subdomain. Using an off-the-shelf solution in such a case helps you save development effort which can instead be redirected to the core domain.

Read more [matthiasnoback.nl]

Objects should be constructed in one go

matthiasnoback.nl

In another cool blogpost, Matthias Noback explains a few best practices around newing up objects, illustrated with some great examples.

Consider the following rule: "When you create an object, it should be complete, consistent and valid in one go." It is derived from the more general principle that it should not be possible for an object to exist in an inconsistent state. I think this is a very important rule, one that will gradually lead everyone from the swamps of those dreaded "anemic" domain models. However, the question still remains: what does all of this mean?

Read more [matthiasnoback.nl]

Using anonymous classes as private classes

markbakeruk.net

Mark Baker, serial conference speaker and creator of the PhpSpreadsheet package shares some thoughts on how to create private classes using anonymous classes.

I’ve written before about the benefits of using PHP’s Anonymous Classes for test doubles; but Anonymous Classes also have potential usecases within production code as well. In this article I’m going to describe one such usecase that can be particularly useful within libraries, and that is replicating the access of Package Private (in Java), or Protected Internal Classes (as per C#).

Read more [markbakeruk.net]