DEV Community

loading...

Discussion on: Dependency injection in functional programming

psfeng profile image
Pin-Sho Feng Author

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

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)