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 |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Right now this is the controller calling the processor. After the change, proposed above by Gregory, controller would call the validator which would call the processor, so at the end, the controller would:
But that would introduce a processing logic into controller (or would it not?). What if there are two more controllers using a processor right now? They would still be passing a comment directly to processor instead of validator... :-/
Is that not a problem that the controller is actually creating the processor instance? Making the validator implementing a processor contract won't help here. Shouldn't controllers ask someone (a comment processor factory?) for an instance of processor, so your solution with adding a validator into the processing chain would "kick in" everywhere?