Posts tagged with design patterns

What you'll need to build projections

dev.to

A great post by Barry O Sullivan on what, in my mind, is one of the biggest advantages of event sourcing: the ability to create projections.

Projections are a necessary part of any event sourced or CQRS system. These systems don't rely on a single generic data source such as a normalised MySQL database. Instead you build up your data sets by playing through the events, i.e the “film”, "projecting" them into the shape you want. This allows lot of flexibility as you're no longer bound by a single data model on which you have to run increasingly monstrous SQL queries (12+ joins anyone?). With projections you can build a data model specifically for the problem/question at hand.

Read more [dev.to]

Keeping your Laravel applications DRY with single action classes

medium.com

Rémi Collin shares a cool approach on where to place code that doesn't really belong in a controller. He creates small, reusable, testable, decoratable classes, called Actions.

Using this approach can seems a lot of classes at first. And, of course the user registration is a simple example aimed to keep the reading short and clear. Real value starts to become clear once the complexity starts growing, because you know your code is in one place, and the boundaries are clearly defined.

Read more [medium.com]

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.

Event sourcing made simple

The team at Kickstarter made a simple, synchronous event sourcing library implemented in Ruby.

We’ll go over a high level introduction to Event Sourcing where we will highlight the four components that make a (minimal) Event Sourcing system: Events, Calculators, Aggregates and Reactors. We will then talk about how we implemented a (minimal) Event Sourcing Framework at Kickstarter for d.rip. And finally we’ll reflect a bit on the ah-ha moments and the challenges that we’re going through with this approach — 9 months after having started to work on d.rip and 4 months after launch.

https://kickstarter.engineering/event-sourcing-made-simple-4a2625113224

Read more

Registering macro's in Laravel using a mixin

Rachid Laasri explains how to easily register multiple macros at once using the mixin function present on the Macroable trait.

Laravel Macros are a clean way to add pieces of functionality to classes you don’t own (core Laravel components) and re-use them in your projects. It was first introduced in the 4.2 version but it was only recently that I discovered the ability to define class-based macros. So, this is what this article is going to be about.

http://rachidlaasri.com/php/laravel/macro/2018/04/28/class-based-macros.html

Read more

FP vs. OO

In a new post Uncle Bob explains that you shouldn't have to choose between functional programming and object orientation.

In this blog I will make the case that while OO and FP are orthogonal, they are not mutually exclusive. That a good functional program can (and should) be object oriented. And that a good object oriented program can (and should) be functional. But to accomplish this goal we are going to have to define our terms very carefully.

http://blog.cleancoder.com/uncle-bob/2018/04/13/FPvsOO.html

Read more

Renderless Components in Vue.js

At this year's Laracon Online Adam Wathan gave a fantastic talk on creating reusable view components. The excellent new post on Adam's blog is basically the written down version of talk. Amazing stuff!

Splitting a component into a presentational component and a renderless component is an extremely useful pattern to master and can make code reuse a lot easier, but it's not always worth it. Use this approach if:

  • You're building a library and you want to make it easy for users to customize how your component looks
  • You have multiple components in your project with very similar behavior but different layouts

https://adamwathan.me/renderless-components-in-vuejs/

Read more

Nothing is Something

Here's a video of a great talk by Sandi Metz she gave at RailConf 2015.

Our code is full of hidden assumptions, things that seem like nothing, secrets that we did not name and thus cannot see. These secrets represent missing concepts and this talk shows you how to expose those concepts with code that is easy to understand, change and extend. Being explicit about hidden ideas makes your code simpler, your apps clearer and your life better. Even very small ideas matter. Everything, even nothing, is something.

Read more

Value objects like a pro

On the Hackernoon site Nicolò Pignatelli wrote a good guide on how to write Value objects in a good way.

This is the list you must always check it against:

  • it is immutable and no setters defined;
  • it reflects the semantics of the domain;
  • it shows how information flows and is transformed during runtime;
  • it hasn’t default or useless getter methods;
  • it can be compared to other Value Objects of the - - same class by reading private properties directly

https://hackernoon.com/value-objects-like-a-pro-f1bfc1548c72

Read more

Enabling PHP method chaining with a makeshift pipe operator

Sebastiaan Luca, a freelance Laravel developer from Antwerp, coded up a couple of functions that mimic a pipe operator.

An interesting RFC proposal by Sara Golemon submitted in April 2016 suggested the use of a pipe operator to enable method chaining for any value and method. Yet as of today, it's still being discussed and there's no saying if it will ever make its way into PHP. So in the meantime, here's a solution!

https://blog.sebastiaanluca.com/enabling-php-method-chaining-with-a-makeshift-pipe-operator

Let's hope a real pipe operator will land someday in PHP.

Read more

Organizing code into domain modules

In a new post to his site Mathieu Napoli makes the case for organising your code based on it's function rather than it's type.

We recently discussed 2 topics seemingly unrelated with my colleagues at Wizaplace: how to organize code? How to organize teams? ... Organizing code into domain modules is not a silver bullet but it forces to better understand the problem we are solving and better structure our code.

http://mnapoli.fr/organizing-code-into-domain-modules/

Read more

Goodbye controllers, hello request handlers

Jens Segers, developer at Teamleader and author of the popular Optimus and laravel-mongodb packages, wrote about an alternative to controllers.

Let me introduce you to request handlers. The concept is very simple, yet very unknown to a lot of PHP developers. A request handler is basically a controller, but limited to one single action. This concept is very similar to the Action-Domain-Responder pattern which was proposed by Paul M. Jones, an alternative for the MVC pattern that focuses on a more clear request to response flow for web applications.

https://jenssegers.com/85/goodbye-controllers-hello-request-handlers

Read more

Event Sourcing: what it is and why it's awesome

If you haven't heard about event sourcing or are unsure about what it is, Barry O Sullivan has got you covered. Recently he wrote up a blogpost that explains it very clearly.

Event Sourcing (ES) is opposite of this. Instead of focussing on current state, you focus on the changes that have occurred over time. It is the practice of modelling your system as a sequence of events. ... It solves all the big problems our team has faced when building large scale distributed business software. It allows us to talk to the business in their language, and it gives us the freedom to change and adapt the system with ease.

https://dev.to/barryosull/event-sourcing-what-it-is-and-why-its-awesome

Read more

Extending models in Eloquent

Caleb Porzio, co-presenter of the Twenty Percent Time podcast, published a new article on the Tightenco blog. This time he guides us through a nice use case for extending Eloquent models.

Let’s explore another alternative that can be used as a stand-in for repetitive where statements and local scopes. This technique involves creating new Eloquent models that extend other models. By extending another model, you inherit the full functionality of the parent model, while retaining the ability to add custom methods, scopes, event listeners, etc. This is commonly referred to as “Single Table Inheritance,” but I prefer to just call it “Model Inheritance”.

https://tighten.co/blog/extending-models-in-eloquent

Read more

Precision Through Imprecision: Improving Time Objects

Ross Tuck is probably one of my favourite bloggers. He doesn't publish something often (his previous post is from 2015), but when he does it's very much worth your time.

The important takeaway here isn’t “value objects, yay, inline juggling, boo!” It’s that we were able to remove several classes of errors by reducing the precision of the DateTime we were handling. If we hadn’t done that, the value object would still be handling all of these edges cases and probably failing at some of them too.

Reducing the quality of data to get a correct answer might seem counter-intuitive but it’s actually a more realistic view of the system we’re trying to model. Our computers might run in picoseconds but our business (probably) doesn’t. Plus, the computer is probably lying anyways.

http://rosstuck.com/precision-through-imprecision-improving-time-objects

Read more