Handling centralised state

Evening all.

I’ve just taken the plunge into F# after a mix of curiosity & frustration of dealing with a rapidly growing C# console application that syncs data between key parts of our business.

Up until now, I’ve only had any real experience with Elixir and some Clojure in the (semi-) FP world so I’ve spent a little while reading and watching some resources (fsharpforfunandprofit and Scott’s videos on ROP) and it’s really helped cement some of the more ‘advanced’ & alien topics I never understood when I tried Elm & Haskell super briefly in the past (ADTs, map, bind, etc.)

Anyway, in an effort to begin rewriting this application as a proof of concept, I’m looking to see what an effective way of approaching long-lasting, centralised state may be.

Currently, the app uses the dotnet core generic host and implements a few key (singleton) services that reach out to the external data sources and cache retrieved entities/DTOs in memory and to disk.

There are also some short-lived ‘processors’ that run on a schedule (and may overlap), which will perform any small-scale ETL functions (this is the part I’m super excited to try in a pure, functional way).

With the the current C# implementation using state-holding singletons, and my past experience with the likes of Vuex and Elixir’s GenServers, my current view on long-lasting state is pretty biased around the idea of a service I can reach out to and mutate in a controlled way (singleton or MailboxProcessor). Is this a bad pattern to fall into? Is this perfectly acceptable, but there are certain ways to approach this? Any help is greatly appreciated!

After toying a little, I don’t think I’m quite ready to reach for some of the more functional techniques like partial application to create a composition root.

I’m sticking with DI and stateful service singletons, and I’m going to focus on reaping the benefit of a functional processing/transformation model until I’m more confident and I’ve had time to read & write some more.

If you don’t need to persist the long-lasting state then MailboxProcessor or actors (see Akka.NET and Akkling) might be what you’re after.