Domanda

I've been playing around with DCI after following along with The Right Way to Code DCI in Ruby. I find that I keep wanting my roles to add properties to my data objects.

For instance, if I have a user object.

class User
  def initialize(name)
    @name = name
  end
end

user = User.new('JonMR')

The user may play the role of a customer.

module Customer
  def add_to_cart(item)
    self.cart << item
  end
end

customer = user.extend Customer
customer.add_to_cart 'an item'

To make this work I need to add a cart method. Does the shopping cart really belong in the user object? It feels like the role should add the cart to the data object as needed.

È stato utile?

Soluzione

In general DCI data objects should consist of just the data plus very basic domain logic, like basic validation logic. And DCI roles should be purely behaviour. Adding the cart to the user in a role feels like a violation of that second. Maybe your missing a data object type or a role? Remember roles can use other roles from the context, so maybe your role should just expect an object playing the role of a cart to be part of the current context.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top