Question

I've been looking for good practices for Rusy Sinatra and I found this question here on stackoverflow:

Using Sinatra for larger projects via multiple files

However, I am wondering what to do in case the business logic behind some route is big. Wouldn't be better to wrap it in a class (helper/utility) and just delegate to it from the route ? I my opinion this will keep the controller clean and easy to follow. What would be the best folder to place such a utility class ?

Was it helpful?

Solution

The pattern we use where I work is:

Sinatra Web Handler -> 'Processor' class (encapsulates business logic in a reusable route, sometimes behind facades). The processor does any ORM or cache operations which might be necessary, and knows when to delegate to further downstream processors (or, even, other internal / external services).

This decouples the sinatra routes from the application logic, and means that we can plug those processor classes. We try to keep the processor classes related to one business process, for e.g. User creation, and write them in such a way that we can plug them onto other endpoints as and when we desire. We're in effect using Sinatra as an HTTP request router into our main application.

It seems to work pretty well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top