C# – Microservice and anti-pattern

cdomain-driven-designdomain-modelmicroservices

I am doing a microservice and I was trying to make a decision about how to handle Domain Model.
I am planning to have multiple microservices, as an example: A ClientsMS and a PaymentMS. They both need to have a client class. One way is to have client class in both MS and do a mapping between the two, Or have a Model(domain) Microservice which will have client and other classes, and this will be consumed via DLL.
I am not sure f by using DLL I am introducing an anti pattern. but also using Domain model in every MS I will be loosing the SRP and DRY.

Best Answer

Microservices have a very nice connection to DDD: in a sensible architecture, every microservice will handle a single bounded context.

Every bounded context has its own problem domain and therefore its own model. Instead of sharing parts of the model, keep the model nicely decoupled and translate between models. For microservices, the context boundary corresponds to the input/output of the microservice. You gain nothing by creating a model-microservice or sharing a common model-DLL because no common model exists. A client might be a “user account” in a user service context and a “credit card + billing address” in a payment service context. Just because they are somehow connected does not mean that you should represent them via a single entity in all contexts.

But speaking in practical terms, it can make sense to share a DLL that contains the DTOs and their serialization/deserialization. These DTOs do not represent the model of any context, but only the communication between your microservices. A shared library makes sure that serialization is always done correctly by avoiding repeated code, and it lets your services consume a strongly typed interface. The downside is this reduces one major benefit of microservices: technology-agnosticism. A common library also implies that deployments of new versions need to be synchronized.

Related Topic