Invokable Controllers

Action class usage in my Laravel Apps

For several years I have been a fan of invokable classes, typically to act as action classes for forms and to create jobs for queues etc.

Simple Forms

With regards to simple forms, this works well. The validation and creation code are all contained in one class keeping the code out of the controller. If all you need is validation and a little logic before saving/updating, this model works well. The model breaks down when the complexity of the request goes up.

Invokable Controllers

On a recent freelance project, I needed to validate the request, convert the data and then generate lots of additional values. This Apps calculates the viability of pumps based on the request so there are a lot of fluid dynamic calculations. Once the calculations have been processed, I can again calculate the viability. Only once I have calculated the viability do I save the request.

Naming

My action classes have simple names – createGame, saveUser, – you get the idea. When you read the name of the action, you know what it does and when you look inside, you have an idea about what you will see. This doesn’t work when there are five or six steps that must happen before the “quote” is created.

We get this benefit with invokable controllers as well but rather than the class names telling you what is happening, the method names in the controller describe what is happening. We get all the benefits of invokable action classes without the negative of a giant action class which is doing more than its name suggests.

I rarely need to use invokable controllers but when I do, they are a life saver.

I’ve a detailed post on how I use action classes and how the specific use changes on the structure of the App. If you are a fan of invokable action classes, please give it a read.

Supporting multiple measurement units

Chalk sketch of two puzzles with words ingress and egress

I’m working on a freelance project in which I need to support multiple measurement units. In the App customers can provide their required flow rate in litres per second, litres per minute, litres per hour, cubic meters per hour, gallons a minute or imperial gallons per minute.

Warning: This gets really complicated, fast!

Imperial and Metric

You might have noticed we need to support imperial and metric measurement units. Well, that means we need to support mm, meters, degrees Celsius and the relevant inferior imperial versions some people still use. You know who you are.

Ingress and Egress

To keep business code simple, I have ingress object. These ingress objects take all the given values and convert them to a ‘known’ unit of measurement for each type. I then process all my calculations and do not need to be concerned with converting anything whilst I’m calculating. Once everything is calculated I pass everything into the egress objects, these convert all the data to the original unit and format.

Customer data is sacred

I store everything in the given format. I’ve tended to find over that it is better to store the original customer data and convert behind the scenes. Customers do not like it when their entered values of three gets returned as the 2.99997 the next time it appears on the screen.

I’m only a little way into the project but so far this is working well. Having a single interface to deal with is making my business code much simpler. I only need to care about the units for a value at the start and the end.

Hopefully, all goes well, only time will tell.

This project is the gift that keeps on giving, here is another post on integrating with Business Central.

Blog posts on freelance projects are deliberately a little vague as I can’t really divulge too much, I can talk about general stuff but obviously I’m never going to mention specifics.