zuloosurfing.blogg.se

Rails services
Rails services






Give your services "verby" names, e.g.Both approaches are fine, pick one and stick to it. Not using a namespace saves you from writing Services:: everytime you want to reference a service in your app. you can have a Services::Maintenance service, yet also a Maintenance module in lib). Namespacing your service allows you to use a name for them that some other class or module in your app has (e.g. Decide if you want to use a Services namespace or not.Let your query objects inherit from Services::Query.Let your services inherit from Services::Base.Conventionsįollow these conventions when using Services in your Rails app, and you'll be fine:

#Rails services free

  • does not care whether certain parameters are objects or object IDsĪpart from these basic principles, you are free to implement the actual logic in a service any way you want.
  • has its own exception class(es) which all exceptions that might be raised inherit from.
  • logs all the things (start time, end time, duration, caller, exceptions etc.).
  • can be configured as "unique", meaning only one instance of it should be run at any time (including or ignoring parameters).
  • blocking/in the foreground) or asynchronously (i.e.
  • does only one thing and does it well (Unix philosophy).
  • Services is based on a couple of basic principles around what a service should be and do in your app: If you use Sidekiq, make sure to load the Services gem after the Sidekiq gem. When you then try to enqueue a service for background processing, an exception will be raised. If you don't need background processing, you can still use Services without Sidekiq. To process services in the background, Services uses Sidekiq. If you're not using Postgres, you can still use all other parts of Services, just don't use Services::Query or, even better, submit a pull request that fixes it to work with your database! Sidekiq (optional) It might work with other databases but it's not guaranteed. The SQL that Services::Query (discussed further down) generates is optimized for Postgres. make sure no more than one instance of such a service is executed simultaneously. to store information about the currently running services, so you can enforce uniqueness for specific services, i.e.

    rails services

    Requirements Ruby >= 2.7 Rails >= 6.0 Redis >= 3.0 Usageįor disambiguation: in this README, when you read "Services" with a uppercase "S", this gem is meant, whereas with "services", well, the plural of service is meant.

    rails services

    The biggest benefit you get when using a service layer, in my opinion, is that it gets so much easier to reason about your application, find a bug, or implement new features, when all your business logic is in services, not scattered in models, controllers, helpers etc. There are of course advantages and disadvantages, but after using Services since 2013 in several Rails apps, I must say that in my opinion the advantages far outweigh the disadvantages. MotivationĪ lot has been written about service layers (service objects, SOA, etc.) for Rails. Services is a collection of modules and base classes that let you simply add a service layer to your Rails app.






    Rails services