Last active
August 29, 2015 13:57
-
-
Save rewinfrey/9405245 to your computer and use it in GitHub Desktop.
Repositories, Contexts (Use Cases), Context Library
This file contains 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
# Establishing a top level namespace for a given module (Context Library), | |
# that serves as the internal API for a component of a system | |
# is a great way to begin the process of extracting that component | |
# from its containing environment. | |
# Benefits include: | |
# A well defined boundary of responsibilities | |
# More DRY (less duplicated knowledge of the way a component works) | |
# Isolates points of contact between component and containing system | |
# Expresses a clear and simple definition of the component's responsibilities defined by use case | |
# Autoloading in Ruby is a simple way to lazy load resources | |
# Downsides include: | |
# Slight learning curve (mostly when learning that component for the first time) | |
# Dependency injection can be abused | |
# Confidence in tests (where do you mock, where do you use real objects?) | |
# Trying to FP in an OOP context (this is actually a benefit in my humble opinion) | |
module ContextLibrary | |
autoload :UseCase1, require 'lib/context_library/use_case1.rb' | |
autoload :UseCase2, require 'lib/context_library/use_case2.rb' | |
autoload :UseCase1Repository, require 'lib/repositories/use_case1.rb' | |
autoload :UseCase2Repository, require 'lib/repositories/use_case2.rb' | |
autoload :OtherDependency1, require 'lib/context_library/other_dependency1' | |
autoload :OtherDependency2, require 'lib/context_library/other_dependency2' | |
... | |
def self.use_case1 | |
UseCase1.new(use_case_1_repository, other_dependency_1) | |
end | |
def self.use_case2 | |
UseCase2.new(use_case_2_repository, other_dependency_2) | |
end | |
def self.use_case_1_repository | |
UseCase1Repository.new | |
end | |
def self.use_case_2_repository | |
UseCase2Repository.new | |
end | |
def self.other_dependency_1 | |
OtherDependency1 | |
end | |
def self.other_dependency_2 | |
OtherDependency2 | |
end | |
... | |
end | |
# Defining UseCase1 as use case in the command pattern | |
module ContextLibrary | |
class UseCase1 | |
def initialize(repository, other_dependency) | |
self.repository = repository # <- emphasis spacing for Kevin Liddle ;) | |
self.other_dependency = other_dependency | |
end | |
def call | |
# relevant business logic | |
end | |
private | |
attr_reader: :repository, :other_dependency | |
# private methods | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment