Design question: Is it really a good idea for models having “side effects”?

58 views
Skip to first unread message

Gergely Polonkai

unread,
Feb 20, 2015, 3:23:39 AM2/20/15
to django...@googlegroups.com
Hello,

I’m currently implementing a finite state machine in my application using Django FSM[1]. In the Usage section of the README the author said that “This function may contain side-effects, like updating caches, notifying users, etc.”

Opposing this statement, the Symfony PHP framework’s authors say that models should never have such side effects.

Thus, I was wondering what is the case with Django in this topic. Should I really do such things, like notifying users, in the model’s code?

Best,
Gergely


Vijay Khemlani

unread,
Feb 20, 2015, 8:32:44 AM2/20/15
to django...@googlegroups.com
I'm not sure if there's an official stance on that, but I believe that since Django is a "MVT" framework, that kind of logic does not seem to be appropiate neither for templates nor views, so models seem like the logical choice.

That way you can also make sure that the side-effect that you want to be called (cache update when changing a model, for example) are always executed when calling the model's method.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/69272d49-de97-40a0-9682-430f28f38663%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Russell Keith-Magee

unread,
Feb 20, 2015, 9:20:01 PM2/20/15
to Django Users
On Fri, Feb 20, 2015 at 4:23 PM, Gergely Polonkai <ger...@polonkai.eu> wrote:
Hello,

I’m currently implementing a finite state machine in my application using Django FSM[1]. In the Usage section of the README the author said that “This function may contain side-effects, like updating caches, notifying users, etc.”

Opposing this statement, the Symfony PHP framework’s authors say that models should never have such side effects.

Thus, I was wondering what is the case with Django in this topic. Should I really do such things, like notifying users, in the model’s code?

Separation of concerns isn't a Django thing or a Symfony thing - it's a good software engineering thing. 

I can't speak for the creators of Django FSM, but if I was to try an interpret that statement, I'd say what they were trying to say is "this is a place from which you can trigger other side effects", not "this is the best place to put side effect logic". 

It's a bit difficult to discuss without a concrete example, but I'd suggest that the internal logic of your FSM isn't the best place to put complex logic that touches external services (unless, of course, your FSM is directly tied to managing that service). However, it *would* be appropriate for your FSM to take the trigger of a state change, and use that as a trigger for other system changes.

Put another way -  look at the process for testing your FSM. Will you be able to test your FSM without a fully functional cache, notification system, etc? If you're not able to configure your FSM in such a way that a mock of your notification service can be passed in as a configuration parameter, then you've probably coupled your FSM and your side effect logic too closely. However, if you *can* drop in a testing mock as a configuration item, you've captured the *type* of side effect that you expect to occur, without tying it to a specific implementation.

Yours,
Russ Magee %-)
 
Reply all
Reply to author
Forward
0 new messages