TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Introducing Mutations: Putting SOA on Rails for security and maintainability

71 pointsby cyprissabout 12 years ago

12 comments

gingerlimeabout 12 years ago
Having transitioned from Django to Rails nearly a year ago, this post reminded me of django forms[1]. When I used django, I didn't think much about them, but moving to Rails, I felt something was missing. Why do validations live only/primarily on the model? Doesn't it make more sense to do validation higher-up the chain to filter mistakes and potentially harmful input?<p>Also a good point about the `attr_accessible` concept. It always felt like a bit of a crippled way to perform authorization. That said, I'm not sure this comment is completely valid:<p><i>&#62; attr_accessible suffers from context blindness: you’re frequently going to have an end user UI and an admin UI. You want admins to have access to more fields.</i><p>Whilst it's not the most elegant, you can (and should) define `attr_accessible :x, :y, :z, :as =&#62; :admin`<p>but you have to remember to use something like `MyObj.create({x: 'a', z: 'b'}, :as =&#62; :admin)`<p>[1] <a href="https://docs.djangoproject.com/en/1.5/topics/forms/" rel="nofollow">https://docs.djangoproject.com/en/1.5/topics/forms/</a>
评论 #5296887 未加载
评论 #5295344 未加载
评论 #5295030 未加载
jhundabout 12 years ago
I really like this approach. I use something similar for my work, however this has a much nicer API.<p>Often these patterns are treated and discussed as an either/or decision, which I don't agree with. There are many places in an app where the default and simple Rails way is the right choice. However as apps get more complex, there is also a need for approaches like the one presented here.<p>My rule for choosing the right approach is this:<p>If it's just a simple CRUD controller where it receives input from a simple form and writes attributes to a single model, with no AR callbacks involved, then omakase is perfectly acceptable. In that case I don't want to have to jump through all kinds of hoops and overcome unnecessary friction added by enterprisey patterns.<p>However, as soon as I have to use AR callbacks, add more lines to my controller action, or touch more than one model, that's a clear indication that a service object makes sense.<p>Mutations looks like a great solution for the latter case. It brings in some DCI goodness as well. The key is that some operations require complex context (object graphs, permissions, dependent operations), and that context should be treated as a first class citizen.
snikchabout 12 years ago
I'm a big fan of this pattern. We use a smaller contextual form process that includes ActiveModel::Validations and provides some simple defining methods, and does a very similar thing. Since most services / mutations are generally bound to a form, it's just called BaseForm, and its children are LoginForm, TweetForm etc.
bdittmerabout 12 years ago
I'm glad to see rails devs are finally coming to their senses and exploring other patterns for building applications. SOA, command objects, etc. have been a pretty common pattern in "enterprise-y" frameworks for awhile. This is making rails look more like grails, which I think is a good thing.
评论 #5295019 未加载
jbverschoorabout 12 years ago
Is there a reason why it was not created with multiple commands / methods in mind? :<p><pre><code> class UserService service :signup do |config| config.required do end config.optional do end config.execute # signup stuff here end end service :delete_account do |config| config.required do end config.optional do end config.execute # signup stuff here end end end </code></pre> This would also make it possible to do service description and even expose a json or xml api using just this.<p>But I guess the controllers are created for this. DHH says controllers are responsible for mailing and stuff, which is more correct than in the models with callbacks.
评论 #5295335 未加载
sandstromabout 12 years ago
This seems like a great idea! I have a few thoughts that comes to mind, perhaps others can chip in:<p>- How can this be used in tandem with strong_parameters; should ensuring the structure of params be a responsibility of the controller or the mutation (I'm leaning towards the former)? [1]<p>- How can validation duplication between models and mutations be avoided? In the example 'string :email, matches: EMAIL_REGEX' was used, but presumably one will still validate the email in the model. I'm guessing there are solutions to this, perhaps the existing model validation can somehow be used?<p>[1]: <a href="https://github.com/rails/strong_parameters" rel="nofollow">https://github.com/rails/strong_parameters</a>
评论 #5295309 未加载
评论 #5295189 未加载
nchuhoaiabout 12 years ago
How would Mutations compare with Concerns, which seems to be DHH's preferred method?
评论 #5296834 未加载
评论 #5295201 未加载
nchuhoaiabout 12 years ago
What are other approaches/patterns to more SOA to manage more complex code bases with Rails?
zpeabout 12 years ago
Looks like typed function to me.
评论 #5295070 未加载
timrosenblattabout 12 years ago
I heard DHH hates SOA, but the only reference I can find to this is an old interview from 2006. Anyone have a link to anything recent?
评论 #5294688 未加载
alxndrabout 12 years ago
Web site breaks page zoom. Argh.
vaprem911about 12 years ago
I almost fell off my chair laughing when I saw "Rails" and "Security" and "Maintainability" in the same sentence.<p>A quick search here itself on HN will show you how immature and security-hole ridden Rails is. Built for, and by, HIPSTERS!
评论 #5294742 未加载
评论 #5294759 未加载
评论 #5295018 未加载