6 ms·
> the simple controller is forbidden from talking directly to Active Record [..] This is not better. It is. The controller layer should be as dumb as possible,
by adamors 13y ago
> the simple controller is forbidden from talking directly to Active Record [..] This is not better.
It is. The controller layer should be as dumb as possible, it shouldn't contain your (entire) application logic. It's a matter of single responsibility if anything.
Also, I find it very sad that we're still discussing the usefulness of the active record pattern. Other than convenience, it has none. It's a pain to maintain an application that uses it once it reaches a certain level of complexity.
And not just because of testability, it's a pain in the ass to replace/fine tune certain queries if you're calling active record methods in your controller.
- sanderjd 13y agoHonest question: what pattern for database access is better, and what are the best tools for said pattern? I don't necessarily mean just in Rails, but everywhere. ActiveRecord-the-ruby-library is incredibly mature and convenient to use, and for better or worse, encourages active-record-the-pattern. The repository pattern seems nicer in theory to me, but in practice, the best tool (in ruby) to implement it with still seems to be ActiveRecord, and then I find that I'm mostly delegating to the underlying AR object (because it already does everything!) and wondering what I've really gained. I was hoping DataMapper2/ROM[0] may have been a more straightforward but high-functioning replacement for AR, but it seems there has been no progress on it for quite some time. tldr; I'm wondering how you actually do this. Firstly, in Rails, but other acceptable answers are "other technologies do it in this other way, which is better than how Rails does it for these reasons". [0]: http://rom-rb.org/ http://rom-rb.org/
- adamors 13y agoI'm not using Rails (nor Ruby for that matter) so I can't comment on that part, but I found the repository pattern to be really useful. Using it with an active record is something I've seen other people do, and at least it gets the active record calls out of the controllers. It's not an optimal solution of course, but IMO it still beats plain active record.
- sanderjd 13y agoAre you willing to say what language you're using the repository pattern in and what tooling you've found useful in supporting it? I think my use of DAOs in Java struts projects was somewhat repository-pattern-esque, but regardless of the frustrations I have with ActiveRecord, it is way better than that was, so I'm really curious what, if anything, is better than either of them.
- karmajunkie 13y agoI've been using cqrs as a pattern for a couple of years, and event-sourced data as well now. On Rails. Life is significantly better.
- sanderjd 12y agoAnything open-source or articles about this approach that I could go take a look at?
- karmajunkie 12y agosorry for the delayed response—email is in my profile, feel free to reach out with more questions. I've got a post or two on my blog[0] (haven't updated it in awhile, but working on it!) and I just released a gem[1] to facilitate event-sourcing in ruby. Its young, but i've refined the api a bit from where I started a couple years ago (when it was just an experiment), and it has a much cleaner implementation now. For more info about CQRS/ES and DDD in general, I recommend starting here[3] and lurking on the DDD mailing list[4] [0] http://karmajunkie.com http://karmajunkie.com [1] http://github.com/karmajunkie/replay http://github.com/karmajunkie/replay [3] http://martinfowler.com/bliki/CQRS.html http://martinfowler.com/bliki/CQRS.html [4] https://groups.google.com/forum/#!forum/dddcqrs https://groups.google.com/forum/#!forum/dddcqrs
- ajmurmann 13y agoIn addition I find the controller to be one of the worst parts of Rails. The input comes in form of some magical methods that are available (params, request) and the output happens partially by setting instance variables and partially by calling render. How could anyone ever think that's a good idea?
- adamors 13y agoI guess it helps if you're developing an MVP and you need to show something in a matter of days/weeks. But beyond that, I never saw the appeal of Rails.
- ajmurmann 13y agoDon't get me wrong, I am pretty happy with Rails in general. I just think that the way controllers are implemented seems just totally random. Another, possibly worth, part are lifecycle callbacks on AR models. Those are just a huge invitation for confusion and trouble down the road.
- codereflection 13y agoI generally agree, but it's an important distinction to make that controllers should not be "dumb". If that's the case, then we need to stop calling them controllers. Instead we should insist that controllers be "lean".
- adamors 13y agoI specifically use dumb to mean that controllers should know as little as possible about the business logic. They should just mediate between requests and responses. EDIT: dumb not dump.