Last active
December 15, 2015 23:59
-
-
Save mech/5344023 to your computer and use it in GitHub Desktop.
Trying to see how DDD can help in modeling ATS's Education model.
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
# Entity - Represent an important domain concept with identity and life cycle. | |
class Education | |
def initialize(institution, course, when) | |
@institution = Institution.new(institution) # Institution is an Value Object that is immutable | |
@course = Course.for(course) # Course is an Value Object too | |
@period = period(when) # PostgreSQL's daterange Range Type | |
end | |
def course_ended? # Query method - Based on period, is CA still studying? | |
end | |
def source # Query method - from social profile (LinkedIn) or self-enter | |
end | |
def attributes # Convenient hash for JSON serialization | |
{ institution: institution.name, course: course } | |
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
# Repository | |
class EducationStore < ActiveRecord::Base | |
include StoreFactory # This factory will convert AR data model to domain model | |
# Persist | |
def self.add(education) | |
new(education.attributes).save | |
end | |
def self.add_all(educations) | |
end | |
# Finder | |
def self.education_by_id(id) | |
find(id) | |
end | |
def self.educations_by_institution(institution) | |
where(name: institution.name) | |
end | |
def self.educations_by_ids(ids) | |
where(id: ids) | |
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
# Value Object | |
# One instance of "NTU" is the same as another instance of "NTU" | |
# Measure, describe, quantify | |
class Institution | |
def initialize(country, name) | |
@country = Country.find(country) | |
@name = name | |
end | |
# Side-Effect-Free function | |
def replace(name) | |
new(@country, name) | |
end | |
def ==(other) | |
@name == other | |
end | |
end | |
# Or simply | |
class Institution < Struct.new(:country, :name) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment