Functional Principles in C# – method should be honest

Below are the key ideas of how to apply functional principles in C# explained in Pluralsight course by Vladimir Khorikov.

  1. Immutable Architecture
    Separate domain logic, Infrastructure(Persister), Service logic. Make the domain logic immutable and functional(input -> output) so as to easily unit test it. Make the infrastructure and service logic as simple as possible.
  2. Exception
    Use return value over an exception if it’s a known exception.
    Fail Fast
    General exception catch should be in a single place to catch all unexpected exception.
  3. Avoid primitive obsession
    Utilize value object which may contain validation logic as a single place to check input data.
  4. Avoid null
    Use ‘NullGuard’ library with [assembly: NullGuard(ValidationFlags.All)] option to check not only public method but also private method and properties.
    Use Maybe<T> around a domain model which is separated from the presentation layer.
  5.  Handling Failures and Input Errors
    Use value object
    Try catch at the lowest level
    Use Railway-oriented Programming => Result<T> and Maybe<T> => Monad.

Leave a comment

Your email address will not be published.