Created
July 11, 2012 06:20
-
-
Save gmoeck/3088352 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommentsController < ApplicationController | |
def create | |
translator.translate params | |
end | |
private | |
def translator | |
CommentRequestTranslator.new(processor) | |
end | |
def processor | |
comment_processor = CommentProcessor.new | |
comment_processor.add_listener twitter_poster | |
comment_processor.add_listener facebook_poster | |
comment_processor.add_listener comment_persister | |
comment_processor | |
end | |
def twitter_poster | |
TwitterPoster.new | |
end | |
def facebook_poster | |
FacebookPoster.new | |
end | |
def comment_persister | |
SQLCommentPersister.new | |
end | |
end | |
#example class | |
class CommentProcessor | |
def initialize | |
@announcer = Announcer.new | |
end | |
def add_listener(listener) | |
@announcer.add_listener listener | |
end | |
def new_comment(comment) | |
new_comment = comment.with_language(language_for(comment.body)) | |
.with_spam(is_spam(comment.body)) | |
@announcer.announce('new_comment', new_comment) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would agree that it can be somewhat of an issue to have the controller creating the instances of the objects. I'd generally move the creation of the objects into an infrastructure layer and then just have the controllers call into the domain. I would often however still create a "translator" or "adapter" within the controller, and pass the domain into its constructor when translation from the network request into several messages to the domain is necessary.