Immutable domain class: entity or a value object?
Another interesting and quite common question relates to immutable domain classes. Let’s take an example. Let’s say that we have a list of countries that our system supports. Each country is represented with the following domain class: public class Country { public long Id { get; } public string Name { get; } public Country(long id, string name) { Id = id; Name = name; } } Notice that this class is immutable.
Should you reuse Ids among microservices?
There’s a common and quite interesting question about how to manage Ids in a microservices architecture. This question is best described with an example, so let’s say we have two bounded contexts, Sales and Support, that work in their own respective microservices: Notice that there is the same entity, Customer, in both of these microservices. Let’s also say that the Sales microservice is upstream to Support microservice.
Unit Testing vs. BDD
Here’s a question about BDD (behavior-driven development) that I thought I would share with you: Where does BDD fit in the testing pyramid? I see BDD as unit tests since unit tests verify the domain behavior (classical school). If so, would you limit at all what amount of the units tests are done with BDD/Gherkin? This is a very interesting question because it touches upon the bigger topic of writing valuable unit tests.
A follow-up to domain model purity and ORMs
In the previous email, I elaborated on the article about domain model purity and lazy loading. Just to re-iterate, here’s the sample code: public class User : Entity { private List<LoginSession> _loginSessions; public virtual IReadOnlyList<LoginSession> LoginSessions => _loginSessions.ToList(); public virtual void RegisterSession(DateTime now) { LoginSession session = _loginSessions.Last(); if (session.HappenedRecently(now)) session.Update(now); else _loginSessions.Add(new LoginSession(this, now)); } } User and LoginSession are part of the domain model; User here relates to LoginSession as 1-to-many.
Domain model purity and ORMs
I’d like to elaborate on an interesting comment to the article about domain model purity and lazy loading. In that article, I made a case that proxy classes created by ORMs in order to enable lazy loading don’t violate domain model purity: A base class is not responsible for what its subclasses are doing. As long as this base class doesn’t explicitly rely on its subclasses to communicate with the database (that is, doesn’t introduce abstract methods with Task's in their signature), it remains pure.
How to handle unique constraint errors
I received a question recently about the topic I wanted to cover for a long time already: How to handle unique constraint violations? Let’s say that a controller needs to change the user email. A typical code would look something like this: public class UserController { public string ChangeEmail(int userId, string newEmail) { Result<Email> emailResult = Email.Create(newEmail); if (emailResult.IsFailure) return "Email is incorrect"; User existingUser = _userRepository.GetByEmail(newEmail); if (existingUser !
Future Pluralsight courses survey results
I’d like to thank everyone who participated in the survey about future Pluralsight courses and especially those who sent me in-depth comments and suggestions. Here are the results: Event Sourcing in Practice: 1.96 average rating (where 1 was the most desirable and 4 — the least desirable) CQRS with MediatR: 2.24 Advanced error handing in ASP.NET Core: 2.46 NHibernate for EF Core developers: 3.
Should you fix bugs in private methods if they cancel each other out?
I had an interesting conversion recently that I thought I would share with you. The conversation is about bugs that would compensate each other and would ultimately lead to a correct behavior. Let’s say we have a method like this: public void PerformAction() { Step1(); Step2(); } private void Step1() { /* ... */ } private void Step2() { /* ... */ } And let’s also say that both Step1 and Step2 contain bugs in their implementation, but the overall behavior of the PerformAction method is correct because, by pure chance, the bugs in the two steps cancel each other out.
Free week on Pluralsight and future courses survey
Just wanted to give you a quick shutout as Pluralsight is going to make all their courses free for a week of October 12-18. So be sure to register if you haven’t already. Use this link to register. While you are at it, check out my courses, especially these three: Applying Functional Principles in C# Refactoring from Anemic Domain Model Towards a Rich One
Domain modeling trilemma comments
I’d like to continue the topic of domain modeling trilemma. Just to re-iterate, the trilemma states that you can’t have all 3 of the following attributes: Domain model completeness — When all the application’s domain logic is located in the domain layer, i.e. not fragmented. Domain model purity — When the domain layer doesn’t have out-of-process dependencies. Performance, which is defined by the presence of unnecessary calls to out-of-process dependencies.