Where to put validations when following a DCI design?

I'm following DCI to structure the behavior of a new Rails application, but I have some doubts about where to put the validations.

Traditionally, if you're going to manage your data using ActiveRecord models, validations are defined at the particular class that inherits from AR, and they seem to fit right as part of the data layer.

However, to my eyes it makes sense to have certain validations that only happens under a specific role, and they should only be checked if the object is on that context, being ignored in all the other cases. Which basically means that those validations should be defined at particular roles, and the object should be extended with those role modules when it's being used on the context where it makes sense.

Do you think it's a good idea to keep those validations at the roles? If so, how do you declare them without contaminating the other instances of the same class than the object? If I want to use the ActiveRecord validations, they are declared at class level, so I can't attach them to the object individually, being forced to use a re-declaration of the "validate" instance method on the role module (attaching the errors to the errors array of the object directly), or some similar technique.


It depends on the validations/rules in question. A main goal of DCI is to segregated what the system is (domain model) from what the system does (functionality) if the rules are associated with the domain model. Eg rules for welformedness of SSN then it should be part of the data objects. On the other side if the rules are related to the functionality of the system Eg a user is only allowed to order two super discounted products each week, then it's a matter for a context

链接地址: http://www.djcxy.com/p/64704.html

上一篇: 调整大小后布局视图

下一篇: 遵循DCI设计时,在哪里进行验证?