In my last post, I showed how using a function delegate can be used to create named or keyed dependency injection resolutions.
When I was writing the demo code, it struck me that the object orientated code I was writing seemed to be bloated for what I was trying to achieve.
Now don’t get me wrong, I am a big believer in the SOLID principles, but when the interface has only a single function, having to create multiple class implementations seems overkill.
This was the ‘aha’ moment you sometimes get as a developer where reading and going to user groups or conferences plants seeds in your mind that make you think about problems differently. In other words, if the only tool you have in your tool-belt is a hammer, then every problem looks like a nail; but if you have access to other tools, then you can choose the right tool for the job.
Over the past few months, I have been looking more at functional programming. This was initially triggered by the excellent talk ‘Functional C#’ given by Simon Painter (a YouTube video from the Dot Net Sheffield user group is here, along with my own talk – plug, plug, here).
In the talk, Simon advocates using functional programming techniques in C#. At the end of the talk, he recommends the Functional Programming in C# book by Enrico Buonanno. It is in Chapter 7 of this book that the seed of this blog was planted.
In short, if your interface has only a single method that is a pure function, register a function delegate instead of an interface.
This has several benefits
- There is a less code to write
- The intent of what is being provided is not masked by having an interface getting in the way – it is just the function exposed via a delegate
- There is no object creation of interface implementations, so less memory allocations and may be faster to initially execute (as there is no object creation involved)
- Mocking is easier – you are just providing an implementation of a function signature without the cruft of having to hand craft an interface implementation or use a mocking framework to mock the interface for you.
So with this in mind, I revisited the demo from the last post and performed the following refactoring:
- Replaced the delegate signature to perform the temperature conversion instead of returning an interface implementation that has a method
- Moved the methods in the class implementations to static functions within the startup class (but could easily be a new static class)
- Change the DI registration to return the results from the appropriate static function instead of using the DI container to find the correct implementation and forcing the caller to execute the implementation’s method.
As can be seen from the two code listings, Listing 1 (Functional) is a lot cleaner than Listing 2 (Object Orientated).
Having done this, it also got me thinking about how I approach other problems. An example of this is unit-testable timestamps.
Previously, when I needed to use a timestamp to indicate the current date and time, I would create an interface of ICurrentDateTime that would have a property for the current datetime. The implementation for production use would be a wrapper over the DateTime.Now property, but for unit testing purposes would be a mock with a fixed date and time to fulfill a test criteria.
Whilst not a pure function, the same approach used above can be applied to this requirement, by creating a delegate to return the current date and time and then registering the delegate to return the system’s DateTime.Now.
This achieves the same goal of decoupling code from the system implementation via an abstraction, but negates the need to create an unnecessary object and interface to simply bridge to the underlying system property.
If you are interested in looking at getting into functional programming while staying within the comfort zone of C#, I highly recommend Enrico’s book.
The demo of both the OO and Functional approaches can be found in the GitHub project at https://github.com/configureappio/NamedDiDemo.