4 ms·
I really love the concepts provided by Domain Driven Design (DDD), regardless if you choose OOP or FP to implement it with. It's fine if you want to choose C#,
by redact207 7y ago
I really love the concepts provided by Domain Driven Design (DDD), regardless if you choose OOP or FP to implement it with.
It's fine if you want to choose C#, and there're better ways of addressing the approaches to validation in OOP than were provided in the examples. Value objects are a nice way to ensure strong immutable types like credit cards can be created and passed around without requiring separate validation classes or wild abstract base classes.
I like exceptions in C# - when I used to code that I'd make a lot of domain/business exceptions that the code would throw anytime there was a violation. Here I think Java is a lot stronger in that you are forced to declare what types of errors can be thrown from a function so you have a chance of handling them. In C#, Typescript, I'm finding myself having to lean on codedoc "@throws" to do the same thing (though not as reliably).
That said, I generally am fine for most exceptions to not be handled and instead bubble up "globally". If it happened because of an API request? Let middleware map it back to a 400 Bad Request with the error body. If it happened because of a message handled? Log it, retry the message until it gets dumped to the DLQ. If it's not a violation, then it may not be an exception in the first place, in which case it can be returned with a compensating action performed.
I really like F#, but I struggled to find the actual benefit of it in this article from a DDD perspective.
- UK-AL 7y agoI find doing things like having types that flow through different stages -> UnvalidatedEmail, ValidatedEmail, VerifiedEmail is a lot better in f#. In c# you need to create a lot of value object classes for that. Or have some kind of property inside the value object which indicates current state of the email. Even then you won't be able to exhaustive pattern matching on it to guarantee each situation is handled.
- Flow 7y agoMaybe I'm missing something, but what stops you from having Unvalidated<T> etc wrapper classes in C#?