4

Hello I am a newcomer to Ruby and I am starting a new project in Ruby On Rails. I have a PHP background and used to working with bigger frameworks like Symfony2.

My question is that does Ruby on Rails use any kind of Service Container where I store all of my custom objects and manages my dependencies? From the tutorials which I watched nowhere is such principle mentioned.

Is this considered a bad practice in ROR because of how ruby's OOP is different?

Do I have to require each object and make a new sample when I ever need one?

Does Ruby not need Dependency Injection ? Is calling new

.new

on every object required a good practice in ruby ?

Csak Zoli
  • 408
  • 1
  • 4
  • 11

4 Answers4

1

I think it's important to not mix up Ruby (the language) and Ruby on Rails (the framework) - I'm not saying that you're doing this, just bear with me.

Ruby has some 'best practices' in terms of how your code is laid out and solutions to common problems, but you're free to use any design patterns or architectural styles you choose. You can say it's not opinionated.

Rails, however, has very strong opinions about how it is used. Here again there are best practices regarding common problems and their solutions, code formatting, file placement, etc, but unlike Ruby, you are in for an uphill battle if you choose design patterns or architectural styles that are not the 'norm' for Rails.

The easy-to-throw-together scaffolding of Rails is great in many cases, but not so much in others (projects that need to scale, for example). Even when you have to fight against the structure of Rails, it still has its benefits - the asset pipeline being one of them. If you want to add dependency injection, you might be better off working from a Domain Driven approach and sending your data from a 'smart' backend into a 'dumb' Rails-frontend.

dax
  • 10,779
  • 8
  • 51
  • 86
  • Good advice I saw how rails restricts many actions and at some degree its becoming annoying, would you perhaps know any library in ruby that might do this? – Csak Zoli Jul 25 '14 at 05:35
  • i'm not sure there is a library to do this, and if there were it would probably not be so good because it's so project specific. I think the best thing is to build separate small domains and require them in the Rails gemfile, so that when the rails project boots, it has access to all your dependencies. Jó szerencse! – dax Jul 25 '14 at 10:05
1

After a long think through and dax -s great advice I remember how uncle bob once said that a good application is designed in a way that we could remove the code that makes the applications logic place it into another framework and it should work perfectly. So I decided that all the code that holds my applications logic is going to be in Rails lib folder and I am going to use service container for that.

After a long search I found out that Jim Weirich made a service container and since he is a common and big name in Ruby it gave me even more confidence on going with this approach here is the git-hub library https://github.com/jimweirich/dim .

It has some amazingly simple and good examples on how to use it.

I decided that in my lib folder I will make a folder for every module in my application and in there make a config folder and add a services.rb file where I register each new service.

Thank You for all the great advices!

Csak Zoli
  • 408
  • 1
  • 4
  • 11
0

If you are asking about Dependency Injection, the general opinion is that is unnecessary with Ruby. You can check this question for further information.

Community
  • 1
  • 1
rabusmar
  • 4,084
  • 1
  • 25
  • 29
  • So then if I have a object that depends on lets say 5 other object in Ruby I just call .new on them and thats a best practice ? – Csak Zoli Jul 24 '14 at 19:23
  • 2
    Well, that doesn't happen too often with rails. The gems you declare in your Gemfile are usually avaliable from anywhere in your project, and if you need to change the behaviour of that gem you would probably do it in an initializer. Also, even if you need to inject dependencies, ruby has dynamic clases, blocks, named parameters and several other features that makes DI frameworks mostly unnecessary. You can read it in more detail here: http://weblog.jamisbuck.org/2008/11/9/legos-play-doh-and-programming – rabusmar Jul 24 '14 at 19:36
0

Maybe you could take a look in http://www.poodr.com/ Sandi Metz Practical Object-Oriented Design in Ruby (POODR) describes SOLID principles in Ruby and the D is for dependency inversion principle which has some tips on how ruby handles dependencies. Great book all in all.

Csak Zoli
  • 408
  • 1
  • 4
  • 11