Composing an F# (non-web) app - DI, partial application?

I've recently embarked upon the F# journey, and I'm rewriting an internal .NET core C# app that syncs between different systems on a schedule.

The current C# app makes use of the generic host and `HostedServices` (`Task`s) for scheduled jobs and leans heavily upon dependency injection to instantiate these.

From what I've read, the more functional style is to use partial application and create explicit compositional root(s). But I can't see to find any concrete examples _anywhere_, and the theoretical examples don't seem to hold up to any kind of scale.

Do people forego the generic/web hosts and the IoC container?
If so, does anyone have any concrete examples?
Should I carry on with the setup that's working, at the risk of more run-time errors and abstraction (I've already faced some issues with DI that were not obvious)?

Posted May 28, 2020, 1:26 PM UTC

1410
mariner70•

Two cents from another recent adopter here. Mark Seemann addresses this matter in his series of posts on “From dependency injection to dependency rejection”. https://blog.ploeh.dk/2017/01/27/from-dependency-injection-to-dependency-rejection/

According to him, partial application is not necessarily the answer. YMMV, hope it can be of use.

9
DanJSum•

+1 on that series; it's great.

2
alexleansquad•
8
cmothebean•
1
alexleansquad•

Nice. Its on my list to do a refactor around this at some point, will note :)

1
TarMil•

Do people forego the generic/web hosts and the IoC container?

In aspnet core it's pretty much impossible, the DI container is too tightly integrated. People have tried to make it feel more functional with stuff like Giraffe.GoodRead, but I think in general you're better off with a few "normal" DI-using classes that serve as shells around a functional core.

5
MRainzo•

I think the partial application only works to replace interfaces that have one method in them. I read that in a comment on Fsharpforfunandprofit but I also am new to F# so I might be wrong

1
jhewlett•

An interface with one method is equivalent to a function signature. That’s probably what you’re thinking of.

So then you can think of an interface with multiple methods as a grouping of functions. (A record of functions is similar.)

Both with interfaces with multiple methods, and with records of functions, you can use partial application to produce the functions that are part of those collections.

4
jhewlett•

I see a few approaches. You can try to avoid the container altogether and do everything manually. This is tricky to do in a web app but I think it’s simpler in your situation (console app/service?).

Another approach is a bit of a hybrid, which is to use the container minimally, i.e. to register top-level dependencies only (instantiating objects manually and partially applying functions)

Here’s a post I wrote that talks about this approach in the context of a web app, with examples: https://dev.to/jhewlett/dependency-injection-in-f-web-apis-4h2o

At the end it recommends registering records of functions, but what I’ve seen more commonly is folks registering interfaces that they create through object expressions. They are pretty similar in practice, but interfaces are more idiomatic .NET.

1
DanJSum•

I wrote about this process in my yet-unfinished project "objects |> functions"; the section "Dependency Injection with Functional Style" details how I did it. I was just replacing one dependency, but by putting code in the root of my application, I was able initialize an instance of the dependency with a concrete instance (in this case, of RavenDB's IDocumentStore interface).

Instead of partial application, the functions that access the "DI container" (AKA Reader monad) take it as a parameter instead. There is then another function that, given the Reader, extracts the needed dependency from it. At that point, you have your concrete instance, and can perform your operations on it and return whatever you need.

"Monad" made my head hurt initially, but at least in the case of Reader, it helped me to think of it as defining a set of properties, then defining a separate function to extract one of those properties, instead of writing a function within that object to obtain it. Once you have it, those two combined become your actual "thing you want to do use". Think of it in terms of a book - the reader says, "Here's a book, and here's how to find where this highlighted part is."

Anyway, I hope that's helpful. There were a couple of folks on the F# team who helped me with those concepts, and it worked really well. I have another project where I had a similar thing but with IDependencies having more than one property; I can't remember now if that one was open source or not. I've also written things using Giraffe, which provides a nicer syntax, but is still using the ASP.NET Core framework's DI container under the hood.

1