Snippets tagged domain

  • C#-Friendly Single-Case Discriminated Unions

    Single-case Discriminated Unions (https://fsharpforfunandprofit.com/posts/designing-with-types-single-case-dus/) are a lightweight way to create types for important concepts in your domain. Unfortunately, there's a few gotchas when consuming from C# to be aware of. For one, if you use the `==` or `!=` operators in C#, it will do a reference comparison, which is almost certainly not what you want for these wrapper types. By overriding `op_Equality` and `op_Inequality`, this will force it to use a structural comparison. Secondly, when using string concatenation via `+`, `String.Format`, or string interpolation, C# will implicitly convert non-string arguments to a string via `ToString`. This means if you forget to use `.Item` to unwrap your value, you will not get a compiler error, it will just implicitly call `ToString`, which by default will look like this: `Id "c148b684-2c40-4383-a1b9-0e8f37752fd0"`. By overriding `ToString`, we can make sure it will look like the raw underlying type when converted to a string: `c148b684-2c40-4383-a1b9-0e8f37752fd0`.

    2 people like this

    Posted: 6 years ago by Justin Hewlett