What is DCI and how can it fit with Rails?
Asked Answered
O

4

24

A recent debate with a co-worker about different approaches to design and code the models in a Rails application brought me across DCI in the context of Rails.

However, I just can't seem to wrap my head around that entire concept, even after going over this example application.

Currently, I tend to just go more or less "by the book" when writing a Rails app.

So there are a few things I'd like to ask --

  • What is DCI and what are its advantages when implemented alongside MVC over plain old MVC (and vanilla ActiveRecord in Rails) ?
  • And how can it be implemented in Rails (or in other words, what's with all the modules) ?

Edit

I'd like to even further expand my question in the context of RoR - is another level of abstraction between the models and the controllers in Rails recommended? How widespread is it in different-scale applications?

Odericus answered 13/3, 2012 at 3:17 Comment(5)
your question is extremely broad and there for very difficult to answer. There's quite a few places where it seems that you've not quite gotten what DCI is making it even harder to answer some of the questions in a DCI context. I can help you with the DCI stuff if that's the goal but you'd need to narrow down the scope thenTakashi
I'd mostly like to understand what is DCI. As for a Rails implementation, a simple example or even a two-line explanation would be great.Odericus
Great question. I didn't know until reading this that there was a name for the paradigm that I've been hankering for just lately. I've been looking at code I maintain (that I didn't write) and thinking "What this needs is classwise separation between data and behaviour." I guess that this is precisely what DCI is trying to do.Bahia
Not DCI specifically but Googling for "Rails service objects" will give a taste of how DCI-like concepts can be applied. And self promotingly: sitepoint.com/using-wisper-to-decompose-applicationsUniversally
If you are still looking for RUby related examples for DCI have a look at a presentation That was inspired by several DCI questions here (this being one of them) youtube.com/watch?v=ZUADinlqHwkTakashi
T
21

DCI is a paradigm and hence much more than a way to design an application. It's a way to think about modelling as well as structuring code. One of the important parts of DCI is keeping what the system is (the domain model) and what the system does (functionality) apart. DCI is not a different approach to solving the same problem as MVC so your first question can't really be answered. You can use MVC and DCI simultaneously which is no coincidence since Trygve Renskaug is the father of both MVC and DCI. He recently answered a similar question to this on the google group 'object-composition'.

The example you've linked to violates some of the basic ideas such as keeping roles private to the contexts and I couldn't actually find a single context either but that could be due to spending only a short time browsing the code.

I don't know RoR my self so I can't give you an RoR example but if you go to fullOO you'll find examples written in different languages including both Ruby and Marvin the first language designed for DCI.

EDIT There's no simply answer to the question "What is DCI" DCI is a paradigm, just like OOP is a paradigm. They both have the same roots and answering the above question is as complicated as answering "What is object orientented programming". Things are even more complicated by the fact that DCI is object oriented and OOP in all the major OO languages is actually class oriented and not object oriented. DCI aims at producing code where the interaction between objects at run time is visible in the code at compile time and in more general terms tries to mkae it easier to reason about the run time behavior from reading the code. The site I've linked to above is devoted to explaining what DCI is all about and also lists examples in a number of languages. Ruby being one of them

EDIT There's a book on ruby and DCI on it's way. The author is pretty active on object-composition and insightfull

Takashi answered 14/3, 2012 at 9:22 Comment(4)
I must say the Ruby example on fullOO seems a little weird, for example the use of thread variables. Are there any DCI examples that are closer to idiomatic Ruby, or is that just not possible?Dubois
@MatijsvanZuijlen which of the examples specifically. Some of them is written using a gem some are written in pure Ruby. The gem fixes some issues because Ruby doesn't support the scoping rules of DCI however the debugging experience is somewhat lacking using that gem. The pure Ruby examples then violates the scoping rules of DCI so both are close approximations of DCI in RUby wihtout any of them being true DCI (because that's not possible in Ruby though you can get pretty close)Takashi
I only found one example: fulloo.info/Examples/RubyExamples/Dijkstra/DijkstraListing.htmlDubois
I was sure at least the maroon dijkstra examples was there. Sorry for that. You can find some maroon examples here github.com/runefs/maroon/tree/master/test/examples. As can be seen from the commit log I haven't done much on maroon for a long time but at least the code should still provide the idea. A more complex example would be maroon itself. It's fully bootstrapped Ie i've written maroon using maroonTakashi
A
14

For the people who are wondering whats DCI stands for..

DCI stands for Data Context Interaction

Appomattox answered 9/3, 2014 at 1:23 Comment(0)
H
8

At the heart of DCI is the cognitive tools it provides the developer. I'm not sure if you've seen all the great James Coplien/Trygve Reenskaug lectures, but I will try to distill the gist of it for anyone new to the concepts. It is about moving system behavior out of the systems' interacting domain objects(data entities, or what the system is), and into behavior objects(what the system does) as first class citizens which mediate the collaboration between objects by injecting them with functionality in the context of a use case just-in-time.

Think BDD. We code the behavior not across many objects like particulates of functionality spread out all over our data objects which are highly coupled to the persistence layer, but within cohesive objects that exist solely for a use case(story), and which inject capabilities into and coordinate the interactions of these dumb data objects. Like sheer layers of a physical architecture, slowly changing data objects are not loaded up with rapidly changing feature implementation that they carry around all the time. Rather, Ruby provides us the ability to easily inject the behavior into the objects at runtime when/if needed only in the context of a use case.

As an example in ROR, if you have a controller action involved in a use case in which there is an event probability matrix where most entries may be triggered in only a small percentage of requests, then instantiating a network of bloated behavior-heavy objects with the knowledge to execute every event for every possible use case of the data is unnecessary. Also, not having to dig through 18 files in my text editor to understand how that interaction works vs. having all the logic cleanly abstracted to patterns in an interface provided by the context object is a definite plus as well.

Regarding your question about 'another' layer of abstraction between controllers and models in rails, I'm not sure which other one your referring to. Regardless, Yes. By all means. No problem. Design patterns and Uncle Bobs' SOLID principles are pretty much generally accepted best practices in OO design. Both of these strongly encourage loosely coupled abstractions between policy and implementation. They both help avoid catostrophic brain dumps of roman empire demolishing magnitude, because they provide a common framework everyone understands. DCI, for me, provides the same type of cognitive framework, but for making a system easier to comprehend and deal with effectively, and this is the holy grail for any object oriented designer.

Hispanicize answered 12/5, 2012 at 2:38 Comment(0)
B
6

There's a book (currently in progress) on using DCI in Ruby/Rails: Clean Ruby. I highly suggest putting yourself on the notification list - I've read parts of this book and it looks really good.

DCI is gaining acceptance in the Rails world - there's been a number of interesting blog posts about it over the last 3 months or so.

Beaulieu answered 14/3, 2012 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.