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 |
Agreed. But you avoid leaky abstractions.
The leakiness potential is worth not having an extra layer.
What if some code on the persistent layer wants to call delete and by that means "remove from the database" vs. some business logic code that calls delete and means "remove from the user interface"?
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 an archive
method then the delete
method.
This is all just experimentation for me. I might switch to composition if it gets painful - we'll see.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I mean composition vs inheritance.
Agreed. But you avoid leaky abstractions.
What if some code on the persistent layer wants to call
delete
and by that means "remove from the database" vs. some business logic code that callsdelete
and means "remove from the user interface"?