Where to put validations when following a DCI design?
Asked Answered
G

1

8

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.

Gaidano answered 13/10, 2012 at 18:48 Comment(4)
I'm not familiar with DCI, but have you considered single table inheritance?Seibert
Hi Beerlington. This is unrelated to STI. With DCI, you have modules that defines roles for your data models, so you can have for example a User data model, and then a role module like Seller. When you're running a use case defined by a context class, like SellerSignUp, you want to extend the user object that you will pass in to the context with the Seller module, like user.extend(Seller) What I'm wondering is if roles should define things like extra validations, and if so, how people do it without infecting the class. Thanks!Gaidano
I can see a couple of solutions (using facades, or checking those validations manually on the context), but it might not be a good idea, or it might be easier uses.Gaidano
wrappers (such as Facades) are generally discouraged in DCI you can achieve the same result without the wrappers if the language in question supports DCIUnstudied
U
5

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. E.g. 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 E.g. a user is only allowed to order two super discounted products each week, then it's a matter for a context

Unstudied answered 23/10, 2012 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.