Class: Rddd::Aggregates::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/rddd/aggregates/entity.rb

Overview

Entity is the object with identity, which is unique across the system. Entities are considered equal only in case their identity is equal discregarding rest of the similarities.

rDDD works agnostic to identifier type. String is used by default.

Lets create a model of flat and rooms with some basic calculations:

class Room < Entity
  def initialize(id, rooms)
    super(id)

    @rooms = rooms
  end
end

# (see AggregateRoot)
class Flat < AggregateRoot 
  attr_reader :size

  def initialize(id, size)
    super(id)

    @size = size
  end

  def size
    @rooms.reduce(0) {|total, room| total += room.size }
  end
end

rooms = []
rooms << Room.new('kitchen', 15)
rooms << Room.new('living room', 35)
rooms << Room.new('bed room', 20)

flat = Flat.new('A12TY83', rooms)

flat.size #= 70

As you can see Room entity has its identity (room name) which although isn’t global, but local to the given flat instead. Its natural way how we seem rooms. We always identify flat/house first and then room within it. Maintaining global identity is sometimes overkill. Local identity can do just fine.

Direct Known Subclasses

Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Entity

Create entity with given identity.

Parameters:

  • Entity (String)

    identity.



61
62
63
# File 'lib/rddd/aggregates/entity.rb', line 61

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Entity unique identifier.



54
55
56
# File 'lib/rddd/aggregates/entity.rb', line 54

def id
  @id
end

Instance Method Details

#==(other) ⇒ Object

Compare two entities for equality.

Parameters:

  • Entity (Entity)

    to compare with.



70
71
72
# File 'lib/rddd/aggregates/entity.rb', line 70

def ==(other)
  @id == other.id
end