Created
January 9, 2012 22:38
-
-
Save justinko/1585371 to your computer and use it in GitHub Desktop.
The value of contexts in DCI
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 PostCreationContext | |
def initialize(post) | |
@post = post | |
@post.extend PostNotifier | |
@post.extend PostIndexer | |
@post.extend PostCacher | |
end | |
def execute | |
@post.save | |
@post.notify | |
@post.index | |
@post.cache | |
end | |
end |
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 PostsController < ApplicationController | |
def create | |
post = Post.new | |
post.extend PostNotifier | |
post.extend PostIndexer | |
post.extend PostCacher | |
post.save | |
post.notify | |
post.index | |
post.cache | |
end | |
end | |
# VS | |
class PostsController < ApplicationController | |
def create | |
context = PostCreationContext.new(Post.new) | |
context.execute | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The leakiness potential is worth not having an extra layer.
This is a scenario that I just don't think will come up. When would you ever need to call a
delete
method on the extended object and then actually need to delete it in the db? A more real life scenario would be calling anarchive
method then thedelete
method.This is all just experimentation for me. I might switch to composition if it gets painful - we'll see.