marcingolenia / mcode

Martin's Golenia software development blog.
https://mcode.it
0 stars 0 forks source link

https://mcode.it/blog/2020-12-11-fsharp_composition_root/?utterances=de0d48bb9ff317dd0c675b61MITg6gV5gZJ3Wq%2Ft0Pj5beDSLoMLsPUJ2MPIKOBaAbmIWuCX00r8LrEdLYA7IUvRQRt6u4WLKZtx7J9mw6JZWZdeFqeMZhl4X4CMEA2h%2BdYsIWS5p3hR1BXmd5w%3D #16

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

F# Dependency Injection - how to compose dependencies with partial application and don't fail with tests | Blog

One question you might ask yourself before starting a bigger project in F# How to inject dependencies? Let me show you how we used partial application to achieve loosely coupled testable components that can be tested in isolation or together in a broader perspective (acceptance tests). I will use Giraffe as the host, but the technique is free from any framework dependencies.

https://mcode.it/blog/2020-12-11-fsharp_composition_root/?utterances=de0d48bb9ff317dd0c675b61MITg6gV5gZJ3Wq%2Ft0Pj5beDSLoMLsPUJ2MPIKOBaAbmIWuCX00r8LrEdLYA7IUvRQRt6u4WLKZtx7J9mw6JZWZdeFqeMZhl4X4CMEA2h%2BdYsIWS5p3hR1BXmd5w%3D

megafinz commented 3 years ago

What does RoomQueryDao mean?

marcingolenia commented 3 years ago

@megafinz Thanks for spotting 👍 ! It was a leftover. When I started the post I wanted to have hotel room reservation domain, but then I changed it to stock which is simply more straightforward (the domain is not so relevant in the context of this post).

I have fixed the naming in both - example solution and the post. It is StockItemQueryDao now - A dedicated data access object to query the data. I keep queries that returns data for the external purposes (API) separated from the Daos that returns the domain objects (most people calls them repositories, even it they don't follow the repository design pattern 🤔). They only return some data so I used to skip the domain layer - I simply pass the function from query dao to composition root, which then is used for http handler directly.

xxnickles commented 3 years ago

This post REALLY has help me to get a good grasp on the implementation of the content root using F#. I have been confused for a while on how people replace the DI that is often used in C# projects in the F# world, and this post has clarified that concept. I salute you for that, really well written and clear. I have read also about the monads you are referring, but I often find a) they are very confusing, and b) people explaining F# lacks of the tools that language like haskel has for an effective implementation. I also have seen people relaying in F# idiomatic computational expression, which is another concept I have found hard to understand. In any case, I will be looking forward to you post on those monads (and hopefully some related to computational expressions)!