2

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.

JonMR
  • 586
  • 3
  • 18

1 Answers1

2

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.

Christian Horsdal
  • 4,914
  • 23
  • 24
  • I hadn't thought of a context taking a cart. I'll give that a go and see how it turns out. – JonMR Jul 17 '12 at 17:01
  • @JonMR Did you ever go any further with this? I'm in a similar place and am curious what you wound up doing. – JStroop Oct 28 '13 at 15:16
  • @JStroop I didn't get much farther with it unfortunately. For my eventual DCI use case I just dropped the concept of roles. In that case add_to_cart just moved into it's own class. Hopefully that makes sense. :) – JonMR Oct 28 '13 at 20:35
  • @JonMR It does; I think. DCI seems to really shine when you have complex or highly customized interactions between two objects; when it's just one class that needs to do something it starts to feel like modelling overhead and concerns or plain old instance methods seem a lot easier. Everything has its place. Thanks for getting back. – JStroop Oct 29 '13 at 12:22