Marcin Markowski - 7/4/2019

How to use C# projects and namespaces in a DDD project?

C# Projects and namespaces are often neglected as a method to improve code readability. In reality, when used correctly, they are a really powerful tool. How to use them correctly? Make them "scream" about the project domain!


This post is a part of DDD-starter-dotnet project. We encourage you to watch:

  1. Our code (give us a star ­čśë )
  2. Our blog
  3. Twitter: ITLIBRIUM,Marcin, Szymon

Screaming architecture

So what does the architecture of your application scream? When you look at the top level directory structure, and the source files in the highest level package; do they scream: Health Care System, or Accounting System, or Inventory Management System? Or do they scream: Rails, or Spring/Hibernate, or ASP ?

ÔÇő - Uncle Bob

What we see after the first glimpse at the Solution Explorer. Probably that we have lots of projects. Often the project names are so long that their most interesting part is invisible. We also see that we have more than one Service and that we distinguish Frontend , Backend, Business Logic and Data Access Logic. What does this facts add to our domain knowledge about the project? Unfortunately nothing... They are obvious, expected and unimportant. So what are projects and namespaces for? For concisely expressed domain concepts.

First steps in improved readability

First and foremost the project names must be short. We should remove all the unnecessary information from them. Company or product name can be moved to the level of Solution. No need to repeat it for every project! Suffixes like Service or Model can also be skipped. On the Assembly name level we of course keep the prefix to ensure that the generated .dll files are unique. For example project Sales from Solution MyCompany.CRM after compilation will become MyCompany.CRM.Sales.dll

Reflecting the Bounded Contexts boundaries

Secondly the project name should describe some part of the business. Good thing is that usually the business names already are not too long (otherwise business people won't be able to spell them during calls and meetings) If the project names have 3 or even 5 parts (except the company name) there is a high chance that they originate form IT. Such names are noisy and should be shortened.

It turns out that DDD Bounded Contexts names are the best source of projects names. Typical Bounded Context examples are Sales, Warehouse, Delivery planning. In the minds of business this is often the least granular division so it ensures that projects will have appropriate (i.e. not too small) size.

Separate project for each layer

Third point - dependencies between projects are a great tool to protect correct dependencies between architecture layers. For simple, CRUD-like parts of the system one layer (and one project) is enough. For other, more complex parts, where we need a deep and independent model we should use Hexagonal / Clean Architecutre. In such cases one Bounded Context will be implemented by several projects.

In case of Hexagonal / Clean Architecture we should have one project for Domain layer (Sales.Domain) and Application layer (Sales.App). Adapters layer may have several projects depending of number of our system integrations. There should be a separate project for communication with the SQL DB (Sales.Adapters.Sql) and a separate one for the REST API (Sales.Adapters.RestApi).

References between projects should reflect the dependencies rules of architecture i.e. they should go inwards in the direction of the Domain Layer which is the heart of the system. This means that:

  • Sales.Adapters.RestApi knows about Sales.App
  • Sales.App knows about Sales.Domain


  • Sales.Domain does NOT know about Sales.App or any concrete Adapter
  • Sales.App does NOT know about any concrete Adapter

In addition, any concept which is not part of a given layer's API should have visibility internal. This way we minimize chances that someone will try to reuse them in different context which will be completely against our architecture approach.

Ensure that Solution has the right size

What happens when we have very many projects in the Solution? First, we should ask ourselves a question how that happened. In theory it should not happen if we defined well-sized bounded contexts and used them as module boundaries as proposed above. In addition, general rule of thumb is that one Solution should be owned by one team. Other teams can also make small changes, but they should be reviewed by the owning team. Such work organization means that Solutions with very many projects (like 100 or even more) should not be seen at all.

But what if we need to deal with a monolithic application stored in one repository? In such case we should also strive to have the boundaries aligned with the bounded contexts. Such an architecture is called modular monolith. Its modules reflect the structure of business in a similar to microservices. In modular monotlith we can use multiple Solutions, each covering only a subset of all the projects in the system.

It may happen that after using all the above suggestions the number of projects in a single Solution will be still too large. In such case we can consider using Solution Folders. All projects belonging to a single bounded context should be grouped in separate folder. This way we keep using business names while improving ease of navigation even in a very big Solution

On the other hand it may happen that we will have one Bounded Context in one Solution especially when we don't deal with a monolith and we don't keep code in a single repo but we rather use micro-services. One Bounded Context in one Solution is a perfectly valid situation and we should keep applying all the previously mentioned rules . The biggest difference is that such a Solution would be very small, so looking at it we won't discover other Bounded Contexts present in the system.

Ubiquitous Language

Nasz kod powinien przede wszystkim nie┼Ť─ç opowie┼Ť─ç o biznesie kt├│ry odzwierciedla. W Domain Driven Design jedn─ů z podstawowych koncepcji, kt├│ra w tym pomaga, jest tzw. Ubiquitous Language, czyli wszechobecny j─Özyk. Jest to j─Özyk wypracowany wsp├│lnie przez biznes i IT, obecny we wszystkich aktywno┼Ťciach i artefaktach, od spotka┼ä z biznesem, przez modelowanie do kodu i dokumentacji. Nazwy w kodzie powinny wi─Öc by─ç takie jak nazwy w biznesie! Podej┼Ťcie takie znacznie u┼éatwia nam komunikacj─Ö, zmusza do precyzji i w ogromnym stopniu u┼éatwia utrzymanie systemu.

Majority of names in Ubiquitous Language will become types and method names in the code. But how about concepts which not singular and narrow as client or offer but are more broad and take many other concepts under the cover? Here we can use namespaces!

Names for Bounded Contexts and Modules

Namespaces have hierarchical structure which can be nicely used to reflect how business perceives the relationships between the most generic concepts and the more detailed ones. On the top level we should put the name of company or product. The next one should be reserved for BoundedContext - often the most generic name for certain part of business i.e. department or divisions where consistent, common language is used. As seen above - this name is also the name of the project.

Single Bounded Context is often big enough to further divide in smaller, more comprehensible parts. This parts in DDD are called Modules. For example in a context of Sales these could be Orders and Pricing. Inside Pricing we could have Discounts so the full namespace name would be MyCompany.CRM.Sales.Pricing.Discounts.

As the directory structure reflects the organization of namespaces we can browse through them and learn about the structure of business. If done well this can give more insights into the domain than any regular documentation.

Namespaces are not necessarily for layers

A co z t─ů cz─Ö┼Ťci─ů nazwy projektu kt├│ra odzwierciedla podzia┼éy na warstwy np: .Domain, .Adapters.Sql ? Osobi┼Ťcie wol─Ö nie dodawa─ç ich do namespace'├│w. Namespace'y niech nios─ů jedynie informacj─Ö o podzia┼éach biznesowych. To jaki jest podzia┼é na warstwy odzwierciedla ju┼╝ podzia┼é na projekty. Oczywi┼Ťcie typy ze wszystkich warstw dotycz─ůce jednej cz─Ö┼Ťci biznesu trafi─ů wtedy do jednego namespace'a. Nie jest to jednak nic z┼éego. Skoro jest to jeden fragment biznesu, to powinien by─ç on wsz─Ödzie nazwany tak samo, niezale┼╝nie od podzia┼éu na warstwy, kt├│ry zale┼╝y od innych czynnik├│w.

Avoid technical concepts in namespaces

How about using technical concepts like Controllers, Repositories, Entities as a part of namespace? This kind of information is pretty much a pure noise there. Such an approach leads to an uncontrolled growth of namespaces as all the controllers, repos or entities have to be squeezed in one place. In addition to analyze code regarding a certain part of business you need to navigate through a directory structure which is meaningless from the business point of view. In general - something to avoid at all cost.

Low coupling - High cohesion

Low coupling - High cohesion - nie ma lepszego podsumowania tych rozwa┼╝a┼ä. Powinni┼Ťmy dba─ç o realizacj─Ö tej zasady┬ána ka┼╝dym poziomie od typ├│w do system├│w. Jednym z poziom├│w jest w┼éa┼Ťnie poziom Bounded Context├│w i Modu┼é├│w. Powinny one mie─ç ┼Ťci┼Ťle okre┼Ťlon─ů, niezbyt szerok─ů odpowiedzialno┼Ť─ç i by─ç na tyle niezale┼╝na na ile to mo┼╝liwe. Odpowiedzialno┼Ťci i dopuszczalny stopie┼ä powi─ůza┼ä nale┼╝y jednak zawsze ustala─ç na podstawie g┼é─Öbokiego zrozumienia biznesu, a nie kryteri├│w czysto technicznych!

Low coupling - High cohesion - it is hard to find a better summary for our suggestions. We should take care of them at every level of architecture - from single types to whole systems. Bounded Contexts and Modules are somewhere between them. They should have single, defined, not too big responsibility and be as independent as possible. Responsibilities and dependencies should be always defined with the deep understanding of business.


Using the suggested approach a glimpse on the Solution Explorer gives us idea about the business we operate in. Component names are short so navigation is easy. In addition every technical element of code has a clear responsibility and we now what should be expected from it. System is divided in a hierarchical way, according to real structure of the business. We use Low coupling on each hierarchy level. Let's summarize, once more, how to get there:

  1. All the names should be as short as possible and without noise (in a form of technical concepts).
  2. Company or product's name should be present in: Solution name (MyCompany.Crm), Assembly name (MyCompany.CRM.Sales.Domain.dll), Root namespace (MyCompany.CRM.Sales), but not necessarily in the project name (Sales).
  3. Project name should start from the name of Bounded Context (Sales or Sales.Adapters.RestAPI).
  4. Projects should reflect the architectural concepts (like having a project per layer). Dependencies should reflect the architectural style (domain is independent, application depends on the domain etc).
  5. Projects should expose only what is absolutely essential. By default we should use internal visibility level (not public).
  6. Namespaces should tell a story about business, not about technology. We should see there hierarchical Modules right after company and Bounded Context name (MyCompany.CRM.Sales.Orders).
  7. Namespaces should not contain names of architectural concepts which are already present in the project names (.Domain, .Adapters.Sql).

Marcin Markowski

Zdj─Öcie Marcin Markowski

Architect, trainer, supporter of the Software Craftsmanship approach and close cooperation with business. He specializes in modeling based on Domain Driven Design and system architecture design.

He started his career in business consulting and then moved to IT. He worked both with enterprise class systems and with solutions for small companies. He even built his own startup product. Finally however he decided to stick with IT and share his knowledge and experience as a consultant and trainer.