Skip to content

Instantly share code, notes, and snippets.

@tomkis
Created April 30, 2015 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomkis/6783b848401b6a9b351a to your computer and use it in GitHub Desktop.
Save tomkis/6783b848401b6a9b351a to your computer and use it in GitHub Desktop.
Flux - business logic in stores

This is a reaction to https://twitter.com/tomkisw/status/593822104589578240

I would personally prefer to keep business logic in one place. And this place ideally should be the store. I have always been fan of having stores completely synchronous and to move any communication with the API to action creators. It works pretty nice as long as you don't need to parametrize your API calls, once you get to this point, then some funky stuff in your code may actually happen. The state of your application should be encapsulated inside your stores and the only component that can directly access it, is a controller-view.

It's absolutely valid to keep your API calls in action creators as long as you don't do any business logic in there (no branching, no store dependency, no state manipulation).

You have also mentioned you might even need state from multiple stores. Technically, there is nothing wrong about duplicating state across multiple stores (e.g. listening to the same action from multiple stores), it's something like Bouned Context in Domain Driven Design terms, state is much more projection of multiple events rather than persistent value (EventSourcing reference here) and therefore it's natural to have the result behave different in different contexts, even though technically it's the same value.

An excellent reference is Bill Fisher's comment on stackoverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment