DEV Community

Discussion on: Dependency injection in functional programming

 
patroza profile image
Patrick Roza • Edited

Thanks!

I would say we got as far pure as makes sense. The HTTP Request Handler can't be pure, and the application use-case interactor shouldn't be pure, imo - at least if you like the Interactor to still add meaningful value and sticking to the fact that the Request Handler is just a delivery mechanism detail. However the pure business case, is pure.

I would say that the Interactor is the red parts here, blog.ploeh.dk/2020/03/02/impureim-... while the HTTP request handler is just the mount point for this usecase or workflow.

They do something similar in fsharpforfunandprofit.com/ddd/ which im a huge fan of, I read his book and am intruiged by his railway oriented programming.

Small nit: you could make your domain pure by passing in new Date() instead of instantiating it within complete.

Very good point! I'm still too stuck in non FP languages, that I don't realise these subtle cheats :)
gist.github.com/patroza/8f2d634b23...

How about the Exception? Is that considered Pure? The alternative would be the use of Either.

Thread Thread
 
patroza profile image
Patrick Roza • Edited

Update:
Regarding the post; fernandocejas.com/2014/09/03/archi...
I find the demo project a little boring due to it only implementing Queries, and the Interactors doing nothing more than redirecting calls to a Repository. At least some mapping of Entities to Views would be interesting, but Commands would be of more interest to me :)

I'm personally also more fan of Database contexts, which give lower level access to the data, instead of the more high level one exact query per method like in Repositories, as often in my experience each method is actually only used once, or maximum twice anyway.
But of course you don't give your entities access to these database contexts, that remains in the Interactor. Anyway, it depends on the project.

Old but gold: lostechies.com/jimmybogard/2015/05...
Basically I see Interactors as either Queries or Commands as per how JimmyBogard described them.
What I like about this pattern also is that you can extend the Mediator pipeline with additional behaviours through decorators like validating requests, performing authentication, etc.

Finally - if you're interested, here are some of my own experimentations with all these patterns, in a Functional Programming style, latest fp-ts version github.com/patroza/fp-app-framework2

It's doing CQRS with separate read and write contexts - file-based json databases - living the Uncle Bob dream ;-), domain events, DI, exploring functional programming and trying to find a balance between FP and OO, e.g FP-OO-FP sandwich, which may or may not lead to a 100% FP implementation ;-) Multi level validation (basic JSON fields in delivery mechanism, and Domain level validation in the Interactors/Domain objects)
It's runnable, and has a documented schema so you can easily discover what to post.

Thread Thread
 
psfeng profile image
Pin-Sho Feng

How about the Exception? Is that considered Pure? The alternative would be the use of Either.

I'd say that if the exception is recoverable it's not really an exception but a valid case so it should be modelled as such.

For example, you could have something like

type Todo = IncompleteTodo | CompletedTodo

and then you get compile-time verification plus no exceptions. I just happen to have written about it very recently (shameless plug :D).

I find the demo project a little boring due to it only implementing Queries, and the Interactors doing nothing more than redirecting calls to a Repository. At least some mapping of Entities to Views would be interesting, but Commands would be of more interest to me :)

Yeah, the example is perhaps a bit too simple, but the repository pattern is useful because it abstracts from the data source that's going to be used. From an interactor's point of view, it only wants to fetch data from a repository, but it doesn't care if this data comes from a disk file, database or Redis. These data sources can even be mixed, you might want to check if you've got something in Redis and if not, go fetch it.

Old but gold: lostechies.com/jimmybogard/2015/05...
Basically I see Interactors as either Queries or Commands as per how JimmyBogard described them.

That's a nice one. If we simplify things to the extreme, you can strip classes and interfaces and have only functions left. We can think of interactors like modules of query/command functions and if these are pure, it should be very easy to compose them with extra functionality such as validation or authentication.

I'll check your repo, thanks :)

Thread Thread
 
patroza profile image
Patrick Roza

I'd say that if the exception is recoverable it's not really an exception but a valid case so it should be modelled as such.

Fair enough. Will definitely check your post.

but the repository pattern is useful because it abstracts from the data source that's going to be used

I agree, I guess the main difference is just - do you put all functions in a Repository class, or do you have a bunch of separate functions - perhaps living closer to the usecases that they are used in, perhaps even as private functions to the usecase (function or file), i'm leaning more to the latter, as growing a repository class with single use functions seems to me just not that useful. It centralises them, but as they're often single-use, their ideal place seems to be more close to their use.

That's a nice one. If we simplify things to the extreme, you can strip classes and interfaces and have only functions left.

Yep, 100 with you, I prefer doing this with functions. When the language allows, doing this with just functions is awesome.
Except on the pure part; i'm fine with the Interactors depending on impure functions (only known by interface, not implementation), imo that's their main purpose;
receiving input from delivery mechanism - calling impure, pure, impure - producing output for the delivery mechanism.

Thread Thread
 
patroza profile image
Patrick Roza

Small follow-up on Repository, so i'm kind of more in the boat of:

find();
findOne(1); // find by id
findOne({ firstName: "Timber", lastName: "Saw" }); // find by query

and then using these functions in the interactor, instead of encapsulating specific queries in single use methods in the Repository.

Thread Thread
 
patroza profile image
Patrick Roza • Edited

Of course Jimmy has a word on that too ;-)
lostechies.com/jimmybogard/2012/10...
(it stands at the basis for the Query/Command handlers, which is an evolution of this)