# Implementing Domain Driven Design ## Introduction This is a **practical guide** for implementing the Domain Driven Design (DDD). While the implementation details relies on the ABP Framework infrastructure, core concepts, principles and patterns are applicable in any kind of solution, even if it is not a .NET solution. ### Goals The goals of this document are; * **Introduce and explain** the DDD architecture, concepts, principles, patterns and building blocks. * Explain the **layered architecture** & solution structure offered by the ABP Framework. * Introduce **explicit rules** to implement DDD patterns and best practices by giving **concrete examples**. * Show what **ABP Framework provides** you as the infrastructure for implementing DDD in a proper way. ### Simple Code! > **Playing football** is very **simple**, but **playing simple football** is the **hardest thing** there is. > — Johan Cruyff If we take this famous quote for programming, we can say; > **Writing code** is very **simple**, but **writing simple code** is the **hardest thing** there is. > — ??? In this document, we will introduce **simple rules** those are **easy to implement**. Once your **application grows**, it will be **hard to follow** these rules. Sometimes you find **breaking rules** will save your time in a short term. However, the saved time in the short term will bring much **more time loss** in the middle and long term. Your code base becomes **complicated** and hard to maintain. Most of the business applications are **re-written** just because you **can't maintain** it anymore. If you **follow the rules and best practices**, your code base will be simpler and easier to maintain. Your application **react to changes** faster. ## What is the Domain Driven Design? Domain-driven design (DDD) is an approach to software development for **complex** needs by connecting the implementation to an **evolving** model; DDD is suitable for **complex domains** and **large-scale** applications rather than simple CRUD applications. It focuses on the **core domain logic** rather than the infrastructure details. It helps to build a **flexible**, modular and **maintainable** code base. ### OOP & SOLID Implementing DDD highly relies on the Object Oriented Programming (OOP) and [SOLID](https://en.wikipedia.org/wiki/SOLID) principles. Actually, it **implements** and **extends** these principles. So, a **good understanding** of OOP & SOLID helps you a lot while truly implementing the DDD. ### DDD Layers & Clean Architecture There are four fundamental layers of a Domain Driven Based Solution; ![domain-driven-design-layers](images/domain-driven-design-layers.png) **Business Logic** places into two layers, the *Domain layer* and the *Application Layer*, while they contains different kinds of business logic; * **Domain Layer** implements the core, use-case independent business logic of the domain/system. * **Application Layer** implements the use cases of the application based on the domain. A use case can be thought as a user interaction on the User Interface (UI). * **Presentation Layer** contains the UI elements (pages, components) of the application. * **Infrastructure Layer** supports other layer by implementing the abstractions and integrations to 3rd-party library and systems. The same layering can be shown as the diagram below and known as the **Clean Architecture**, or sometimes the **Onion Architecture**: ![domain-driven-design-clean-architecture](images/domain-driven-design-clean-architecture.png) In the Clean Architecture, each layer only **depends on the layer directly inside it**. The most independent layer is shown in the most inner circle and it is the Domain Layer. ### Core Building Blocks DDD mostly **focuses on the Domain & Application Layers** and ignores the Presentation and Infrastructure. They are seen as *details* and the business layers should not depend on them. That doesn't mean the Presentation and Infrastructure layers are not important. They are very important. UI frameworks and database providers have their own rules and best practices that you need to know and apply. However these are not in the topics of DDD. This section introduces the essential building blocks of the Domain & Application Layers. #### Domain Layer Building Blocks * **Entity**: An [Entity](Entities.md) is an object with its own properties (state, data) and methods that implements the business logic that is executed on these properties. An entity is represented by its unique identifier (Id). Two entity object with different Ids are considered as different entities. * **Value Object**: A [Value Object](Value-Objects.md) is another kind of domain object that is identified by its properties rather than a unique Id. That means two Value Objects with same properties are considered as the same object. Value objects are generally implemented as immutable and mostly are much simpler than the Entities. * **Aggregate & Aggregate Root**: An [Aggregate](Entities.md) is a cluster of objects (entities and value objects) bound together by an **Aggregate Root** object. The Aggregate Root is a specific type of an entity with some additional responsibilities. * **Repository** (interface): A [Repository](Repositories.md) is a collection-like interface that is used by the Domain and Application Layers to access to the data persistence system (the database). It hides the complexity of the DBMS from the business code. Domain Layer contains the `interface`s of the repositories. * **Domain Service**: A [Domain Service](Domain-Services.md) is a stateless service that implements core business rules of the domain. It is useful to implement domain logic that depends on multiple aggregate (entity) type or some external services. * **Specification**: A [Specification](Specifications.md) is used to define named, reusable and combinable filters for entities and other business objects. * **Domain Event**: A [Domain Event](Event-Bus.md) is a way of informing other services in a loosely coupled manner, when a domain specific event occurs. #### Application Layer Building Blocks * **Application Service**: An [Application Service](Application-Services.md) is a stateless service that implements use cases of the application. An application service typically gets and returns DTOs. It is used by the Presentation Layer. It uses and coordinates the domain objects to implement the use cases. A use case is typically considered as a Unit Of Work. * **Data Transfer Object (DTO)**: A [DTO](Data-Transfer-Objects.md) is a simple object without any business logic that is used to transfer state (data) between the Application and Presentation Layers. * **Unit of Work (UOW)**: A [Unit of Work](Unit-Of-Work.md) is an atomic work that should be done as a transaction unit. All the operations inside a UOW should be committed on success or rolled back on a failure. ## Implementation: The Big Picture ### Layering of a .NET Solution The picture below shows a Visual Studio Solution created using the ABP's [application startup template](Startup-Templates/Application.md): ![domain-driven-design-vs-solution](images/domain-driven-design-vs-solution.png) The solution name is `IssueTracking` and it consists of multiple projects. The solution is layered by considering **DDD principles** as well as **development** and **deployment** practicals. The sub sections below explains the projects in the solution; > Your solution structure may be slightly different if you choose a different UI or Database provider. However, the Domain and Application layers will be same and this is the essential point for the DDD perspective. See the [Application Startup Template](Startup-Templates/Application.md) document if you want to know more about the solution structure. #### The Domain Layer The Domain Layer is splitted into two projects; * `IssueTracking.Domain` is the **essential domain layer** that contains all the **building blocks** (entities, value objects, domain services, specifications, repository interfaces, etc.) introduced before. * `IssueTracking.Domain.Shared` is a thin project that contains some types those belong to the Domain Layer, but shared with all other layers. For example, it may contain some constants and `enum`s related to the Domain Objects but need to be **reused by other layers**. #### The Application Layer The Application Layer is also splitted into two projects; * `IssueTracking.Application.Contracts` contains the application service **interfaces** and the **DTO**s used by these interfaces. This project can be shared by the client applications (including the UI). * `IssueTracking.Application` is the **essential application layer** that **implements** the interfaces defined in the Contracts project. #### The Presentation Layer * `IssueTracking.Web` is an ASP.NET Core MVC / Razor Pages application for this example. This is the only executable application that serves the application and the APIs. > ABP Framework also supports different kind of UI frameworks including [Angular](UI/Angular/Quick-Start.md) and [Blazor](UI/Blazor/Overall.md). In these cases, the `IssueTracking.Web` doesn't exist in the solution. Instead, an `IssueTracking.HttpApi.Host` application will be in the solution to serve the HTTP APIs as a standalone endpoint to be consumed by the UI applications via HTTP API calls. #### The Remote Service Layer * `IssueTracking.HttpApi` project contains HTTP APIs defined by the solution. It typically contains MVC `Controller`s and related models, if available. So, you write your HTTP APIs in this project. > Most of the time, API Controllers are just wrappers around the Application Services to expose them to the remote clients. Since ABP Framework's [Automatic API Controller System](API/Auto-API-Controllers.md) **automatically configures and exposes your Application Services as API Controllers**, you typically don't create Controllers in this project. However, the startup solution includes it for the cases you need to manually create API controllers. * `IssueTracking.HttpApi.Client` project is useful when you have a C# application that needs to consume your HTTP APIs. Once the client application references this project, it can directly [inject](Dependency-Injection.md) & use the Application Services. This is possible by the help of the ABP Framework's [Dynamic C# Client API Proxies System](API/Dynamic-CSharp-API-Clients.md). > There is a Console Application in `test` folder the solution, named `IssueTracking.HttpApi.Client.ConsoleTestApp`. It simply uses the `IssueTracking.HttpApi.Client` project to consume the APIs exposed by the application. It is just a demo application and you can safely delete it. You can even delete the `IssueTracking.HttpApi.Client` project if you think that you don't need to them. #### The Infrastructure Layer In a DDD implementation, you may have a single Infrastructure project to implement all the abstractions and integrations, or you may have different projects for each dependency. We suggest a balanced approach; Create separate projects for main infrastructure dependencies (like Entity Framework Core) and a common infrastructure project for other infrastructure. ABP's startup solution has two projects for the Entity Framework Core integration; * `IssueTracking.EntityFrameworkCore` is the essential integration package for the EF Core. Your application's `DbContext`, database mappings, implementations of the repositories and other EF Core related stuff are located here. * `IssueTracking.EntityFrameworkCore.DbMigrations` is a special project to manage the Code First database migrations. There is a separate `DbContext` in this project to track the migrations. You typically don't touch this project much except you need to create a new database migration or add an [application module](Modules/Index.md) that has some database tables and naturally requires to create a new database migration. > You may wonder why there are two projects for the EF Core. It is mostly related to [modularity](Module-Development-Basics.md). Each module has its own independent `DbContext` and your application has also one `DbContext`. `DbMigrations` project contains a **union** of the modules to track and apply a **single migration path**. While most of the times you don't need to know it, you can see the [EF Core migrations](Entity-Framework-Core-Migrations.md) document for more information. #### Other Projects There is one more project, `IssueTracking.DbMigrator`, that is a simple Console Application that **migrates** the database schema and **[seeds](Data-Seeding.md) the initial** data when you execute it. It is a useful **utility application** that you can use it in development as well as in production environment. ### Dependencies of the Projects in the Solution The diagram below shows the essential dependencies (project references) between the projects in the solution (`IssueTracking.` part is not shown to be simple) ![domain-driven-design-project-relations](images/domain-driven-design-project-relations.png) The projects have been explained before. Now, we can explain the reasons of the dependencies; * `Domain.Shared` is the project all other projects directly or indirectly depend on. So, all the types in this project are available to all projects. * `Domain` only depends on the `Domain.Shared` because it is already a (shared) part of the domain. For example, an `IssueType` enum in the `Domain.Shared` can be used by an `Issue` entity in the `Domain` project. * `Application.Contracts` depends on the `Domain.Shared`. In this way, you can reuse these types in the DTOs. For example, the same `IssueType` enum in the `Domain.Shared` can be used by a `CreateIssueDto` as a property. * `Application` depends on the `Application.Contracts` since it implements the Application Service interfaces and uses the DTOs inside it. It also depends on the `Domain` since the Application Services are implemented using the Domain Objects defined inside it. * `EntityFrameworkCore` depends on the `Domain` since it maps the Domain Objects (entities and value types) to database tables (as it is an ORM) and implements the repository interfaces defined in the `Domain`. * `HttpApi` depends on the `Application.Contacts` since the Controllers inside it inject and use the Application Service interfaces as explained before. * `HttpApi.Client` depends on the `Application.Contacts` since it can consume the Application Services as explained before. * `Web` depends on the `HttpApi` since it serves the HTTP APIs defined inside it. Also, in this way, it indirectly depends on the `Application.Contacts` project to consume the Application Services in the Pages/Components. #### Dashed Dependencies When you investigate the solution, you will see two more dependencies shown with the dashed lines in the figure above. `Web` project depends on the `Application` and `EntityFrameworkCore` projects which *theoretically* should not be like that but actually it is. This is because the `Web` is the final project that runs and hosts the application and the **application needs to the implementations of the Application Services and the Repositories** while running. This design decision potentially allows you to use Entities and EF Core objects in the Presentation Layer which is **should be strictly avoided**. However, we find the alternative designs over complicated. Here, two of the alternatives if you want to remove this dependency; * Convert `Web` project to a razor class library and create a new project, like `Web.Host`, that depends on the `Web`, `Application` and `EntityFrameworkCore` projects and hosts the application. You don't write any UI code here, but use **only for hosting**. * Remove `Application` and `EntityFrameworkCore` dependencies from the `Web` project and load their assemblies on application initialization. You can use ABP's [Plug-In Modules](PlugIn-Modules.md) system for that purpose. ### Execution Flow a DDD Based Application The figure below shows a typical request flow for a web application that has been developed based on DDD patterns. ![](images/domain-driven-design-web-request-flow.png) * The request typically begins by a user interaction on the UI (a *use case*) that causes an HTTP request to the server. * An MVC Controller or a Razor Page Handler in the Presentation Layer (or in the Distributed Services Layer) handles the request and can perform some cross cutting concerns in this stage ([Authorization](Authorization.md), [Validation](Validation.md), [Exception Handling](Exception-Handling.md), etc.). A Controller/Page injects the related Application Service interface and calls its method(s) by sending and receiving DTOs. * The Application Service use the Domain Objects (Entities, Repository interfaces, Domain Services, etc.) to implement the *use case*. Application Layer implements some cross cutting concerns (Authorization, Validation, etc.). An Application Service method should be a [Unit Of Work](Unit-Of-Work.md). That means it should be atomic. Most of the cross cutting concerns are **automatically and conventionally implemented by the ABP Framework** and you typically don't need to write code for them. ### Common Principles Before going into details, let's see some overall DDD principles; #### Database Provider / ORM Independence The domain and application layers should be ORM / Database Provider agnostic. They only depends on the Repository interfaces and the Repository interfaces doesn't use any ORM specific objects. Here, the main reasons of this principle; 1. To make your domain/application **infrastructure independent** since the infrastructure may change in the future or you may need to support a second database type later. 2. To make your domain/application **focus on the business code** by hiding the infrastructure details behind the repositories. 3. To make your **automated tests** easier since you can mock the repositories in this case. > As a respect to this principle, none of the projects in the solution has reference to the `EntityFrameworkCore` project, except the startup application. ##### Discussion About the Database Independence Principle Especially, the **reason 1** deeply effects your domain **object design** (especially, the entity relations) and **application code**. Assume that you are using [Entity Framework Core](Entity-Framework-Core.md) with a relational database. If you try to make your application so that it is possible to switch to [MongoDB](MongoDB.md) later, you can't use some very **useful EF Core features**. Examples; * You can't assume [Change Tracking](https://docs.microsoft.com/en-us/ef/core/querying/tracking) since MongoDB provider can't do it. So, you always need to explicitly update the changed entities. * You can't use [Navigation Properties](https://docs.microsoft.com/en-us/ef/core/modeling/relationships) (or Collections) to other Aggregates in your entities since this is not possible for a Document Database. See the "Rule: Reference Other Aggregates Only By Id" section for more info. If you think such features are **important** for you and you **will never move away** from the EF Core, we believe that it is worth **relaxing this principle**. We still suggest to use the repository pattern to hide the infrastructure details. But you can assume that you are using EF Core while designing your entity relations and writing your application code. You can even reference to the EF Core NuGet Package from your application layer to be able to directly use the asynchronous LINQ extension methods, like `ToListAsync()` (see the *IQueryable & Async Operations* section in the [Repositories](Repositories.md) document for more info). #### Presentation Technology Agnostic The presentation technology (UI Framework) is one of the most changed parts of a real world application. It is very important to design the **Domain and Application Layers** to be completely **unaware** of the presentation technology/framework. This principle is relatively easy to implement and ABP's startup template makes it even easier. In some cases, you may need to have **duplicate logic** in the application and presentation layers. For example, you may need to duplicate the **validation** and **authorization** checks in both layers. The checks in the UI layer is mostly for **user experience** while checks in the application and domain layers are for **security and data integrity**. That's perfectly normal and necessary. #### Focus on the State Changes, Not Reporting DDD focuses on how the domain objects **changes and interactions**; How to create an entity and change its properties by preserving the data **integrity/validity** and implementing the **business rules**. DDD **ignores reporting** and mass querying. That doesn't mean they are not important. If your application doesn't have fancy dashboards and reports, who would use it? However, reporting is another topic. You typically want to use the full power of the SQL Server or even use a separate data source (like ElasticSearch) for reporting purpose. You will write optimized queries, create indexes and even stored procedures(!). You are free to do all as long as you don't mix all these into your business logic. ## Implementation: The Building Blocks This is the essential part of this guide. We will introduce and explain some **explicit rules** with examples. You can follow these rules and apply in your solutions while implementing the Domain Driven Design. ### The Example Domain The examples will use some concepts those are used by GitHub, like `Issue`, `Repository`, `Label` and `User`, you already familiar with. The figure below shows some of the aggregates, aggregate roots, entities, value object and the relations between them: ![domain driven design example schema](images/domain-driven-design-example-domain-schema.png) **Issue Aggregate** consists of an `Issue` Aggregate Root that contains `Comment` and `IssueLabel` collections. Other aggregates are shown as simple since we will focus on the Issue Aggregate: ![domain-driven-design-issue-aggregate-diagram](images/domain-driven-design-issue-aggregate-diagram.png) ### Aggregates As said before, an [Aggregate](Entities.md) is a cluster of objects (entities and value objects) bound together by an Aggregate Root object. This section will introduce the principles and rules related to the Aggregates. > We refer the term *Entity* both for *Aggregate Root* and *sub-collection entities* unless we explicitly write *Aggregate Root* or *sub-collection entity*. #### Aggregate / Aggregate Root Principles ##### Business Rules Entities are responsible to implement the business rules related to the properties of their own. The *Aggregate Root Entities* are also responsible for their sub-collection entities. An aggregate should maintain its self **integrity** and **validity** by implementing domain rules and constraints. That means, unlike the DTOs, Entities have **methods to implement some business logic**. Actually, we should try to implement business rules in the entities wherever possible. ##### Single Unit An aggregate is **retrieved and saved as a single unit**, with all the sub-collections and properties. For example, if you want to add a `Comment` to an `Issue`, you need to; * Get the `Issue` from database with including all the sub-collections (`Comment`s and `IssueLabel`s). * Use methods on the `Issue` class to add a new comment, like `Issue.AddComment(...);`. * Save the `Issue` (with all sub-collections) to the database as a single database operation (update). That may seem strange to the developers used to work with **EF Core & Relational Databases** before. Getting the `Issue` with all details seems **unnecessary and inefficient**. Why don't we just execute an SQL `Insert` command to database without querying any data? The answer is that we should **implement the business** rules and preserve the data **consistency** and **integrity** in the **code**. If we have a business rule like "*Users can not comment on the locked issues*", how can we check the `Issue`'s lock state without retrieving it from the database? So, we can execute the business rules only if the related objects available in the application code. On the other hand, **MongoDB** developers will find this rule very natural. In MongoDB, an aggregate object (with sub-collections) is saved in a **single collection** in the database (while it is distributed into several tables in a relational database). So, when you get an aggregate, all the sub-collections are already retrieved as a part of the query, without any additional configuration. ABP Framework helps to implement this principle in your applications. **Example: Add a comment to an issue** ````csharp public class IssueAppService : ApplicationService, IIssueAppService { private readonly IRepository _issueRepository; public IssueAppService(IRepository issueRepository) { _issueRepository = issueRepository; } [Authorize] public async Task CreateCommentAsync(CreateCommentDto input) { var issue = await _issueRepository.GetAsync(input.IssueId); issue.AddComment(CurrentUser.GetId(), input.Text); await _issueRepository.UpdateAsync(issue); } } ```` `_issueRepository.GetAsync` method retrieves the `Issue` with all details (sub-collections) as a single unit by default. While this works out of the box for MongoDB, you need to configure your aggregate details for the EF Core. But, once you configure, repositories automatically handle it. `_issueRepository.GetAsync` method gets an optional parameter, `includeDetails`, that you can pass `false` to disable this behavior when you need it. > See the *Loading Related Entities* section of the [EF Core document](Entity-Framework-Core.md) for the configuration and alternative scenarios. `Issue.AddComment` gets a `userId` and comment `text`, implements the necessary business rules and adds the comment to the Comments collection of the `Issue`. Finally, we use `_issueRepository.UpdateAsync` to save changes to the database. > EF Core has a **change tracking** feature. So, you actually don't need to call `_issueRepository.UpdateAsync`. It will be automatically saved thanks to ABP's Unit Of Work system that automatically calls `DbContext.SaveChanges()` at the end of the method. However, for MongoDB, you need to explicitly update the changed entity. > > So, if you want to write your code Database Provider independent, you should always call the `UpdateAsync` method for the changed entities. ##### Transaction Boundary An aggregate is generally considered as a transaction boundary. If a use case works with a single aggregate, reads and saves it as a single unit, all the changes made to the aggregate objects are saved together as an atomic operation and you don't need to an explicit database transaction. However, in real life, you may need to change **more than one aggregate instances** in a single use case and you need to use database transactions to ensure **atomic update** and **data consistency**. Because of that, ABP Framework uses an explicit database transaction for a use case (an application service method boundary). See the [Unit Of Work](Unit-Of-Work.md) documentation for more info. ##### Serializability An aggregate (with the root entity and sub-collections) should be serializable and transferrable on the wire as a single unit. For example, MongoDB serializes the aggregate to JSON document while saving to the database and deserializes from JSON while reading from the database. > This requirement is not necessary when you use relational databases and ORMs. However, it is an important practice of Domain Driven Design. The following rules will already bring the serializability. #### Aggregate / Aggregate Root Rules & Best Practices The following rules ensures implementing the principles introduced above. ##### Reference Other Aggregates Only By Id The first rule says an Aggregate should reference to other aggregates only by their Id. That means you can not add navigation properties to other aggregates. * This rule make possible to implement the serializability principle. * It also prevents different aggregates manipulate each other and leaking business logic of an aggregate to one another. You see two aggregate roots, `GitRepository` and `Issue` in the example below; ![domain-driven-design-reference-by-id-sample](images/domain-driven-design-reference-by-id-sample.png) * `GitRepository` should not have a collection of `Issue`s since they are different aggregates. * `Issue` should not have a navigation property for the related `GitRepository` since it is a different aggregate. * `Issue` can have `RepositoryId` (as a `Guid`). So, when you have an `Issue` and need to have `GitRepository` related to this issue, you need to explicitly query it from database by the `RepositoryId`. ###### For EF Core & Relational Databases In MongoDB, it is naturally not suitable to have such navigation properties/collections. If you do that, you find a copy of the destination aggregate object in the database collection of the source aggregate since it is being serialized to JSON on save. However, EF Core & relational database developers may find this restrictive rule unnecessary since EF Core can handle it on database read and write. We see this an important rule that helps to **reduce the complexity** of the domain prevents potential problems and we strongly suggest to implement this rule. However, if you think it is practical to ignore this rule, see the *Discussion About the Database Independence Principle* section above. ##### Keep Aggregates Small One good practice is to keep an aggregate simple and small. This is because an aggregate will be loaded and saved as a single unit and reading/writing a big object has performance problems. See the example below: ![domain-driven-design-aggregate-keep-small](images/domain-driven-design-aggregate-keep-small.png) Role aggregate has a collection of `UserRole` value objects to track the users assigned for this role. Notice that `UserRole` is not another aggregate and it is not a problem for the rule *Reference Other Aggregates Only By Id*. However, it is a problem in practical. A role may be assigned to thousands (even millions) of users in a real life scenario and it is a significant performance problem to load thousands of items whenever you query a `Role` from database (remember: Aggregates are loaded by their sub-collections as a single unit). On the other hand, `User` may have such a `Roles` collection since a user doesn't have much roles in practical and it can be useful to have a list of roles while you are working with a User Aggregate. If you think careful, there is one more problem when Role and User both have the list of relation if you use a **non-relational database, like MongoDB**. In this case, the same information is duplicated in different collections and it will be hard to maintain data consistency (whenever you add an item to `User.Roles`, you need to add it to `Role.Users` too). So, determine your aggregate boundaries and size based on the following considerations; * Objects used together. * Query (load/save) performance and memory consumption. * Data integrity, validity and consistency. In practical; * Most of the aggregate roots will **not have sub-collections**. * A sub-collection should not have more than **100-150 items** inside it at the most case. If you think a collection potentially can have more items, don't define the collection as a part of the aggregate and consider to extract another aggregate root for the entity inside the collection. ##### Primary Keys of the Aggregate Roots / Entities * An aggregate root typically has a single `Id` property for its identifier (Primark Key: PK). We prefer `Guid` as the PK of an aggregate root entity (see the [Guid Genertation document](Guid-Generation.md) to learn why). * An entity (that's not the aggregate root) in an aggregate can use a composite primary key. For example, see the Aggregate root and the Entity below: ![domain-driven-design-entity-primary-keys](images/domain-driven-design-entity-primary-keys.png) * `Organization` has a `Guid` identifier (`Id`). * `OrganizationUser` is a sub-collection of an `Organization` and has a composite primary key consists of the `OrganizationId` and `UserId`. That doesn't mean sub-collection entities should always have composite PKs. They may have single `Id` properties when it's needed. > Composite PKs are actually a concept of relational databases since the sub-collection entities have their own tables and needs to a PK. On the other hand, for example, in MongoDB you don't need to define PK for the sub-collection entities at all since they are stored as a part of the aggregate root. ##### Constructors of the Aggregate Roots / Entities The constructor is where the lifecycle of an entity begins. There are a some responsibilities of a well designed constructor: * Gets the **required entity properties** as parameters to **create a valid entity**. Should force to pass only for the required parameters and may get non-required properties as optional parameters. * **Checks validity** of the parameters. * Initializes **sub-collections**. **Example: `Issue` (Aggregate Root) constructor** ````csharp using System; using System.Collections.Generic; using System.Collections.ObjectModel; using Volo.Abp; using Volo.Abp.Domain.Entities; namespace IssueTracking.Issues { public class Issue : AggregateRoot { public Guid RepositoryId { get; set; } public string Title { get; set; } public string Text { get; set; } public Guid? AssignedUserId { get; set; } public bool IsClosed { get; set; } public IssueCloseReason? CloseReason { get; set; } //enum public ICollection Labels { get; set; } public Issue( Guid id, Guid repositoryId, string title, string text = null, Guid? assignedUserId = null ) : base(id) { RepositoryId = repositoryId; Title = Check.NotNullOrWhiteSpace(title, nameof(title)); Text = text; AssignedUserId = assignedUserId; Labels = new Collection(); } private Issue() { /* for deserialization & ORMs */ } } } ```` * `Issue` class properly **forces to create a valid entity** by taking minimum necessary properties in its constructor as parameters. * The constructor **validates** the inputs (`Check.NotNullOrWhiteSpace(...)` throws `ArgumentException` if the given value is empty). * It **initializes the sub-collections**, so you don't get a null reference exception when you try to use the `Labels` collection after creating the `Issue`. * The constructor also **takes the `id`** and passes to the `base` class. We don't generate `Guid`s inside the constructor to be able to delegate this responsibility to another service (see [Guid Generation](Guid-Generation.md)). * Private **empty constructor** is necessary for ORMs. We made it `private` to prevent accidently using it in our own code. > See the [Entities](Entities.md) document to learn more about creating entities with the ABP Framework. ##### Entity Property Accessors & Methods The example above seems strange to you. For example, we force to pass a non-null `Title` in the constructor. However, the developer may then set the `Title` property to `null` without any control. This is because the example code above just focuses on the constructor. If we declare all the properties with **public setters** (like the example `Issue` class above), we can't force **validity** and **integrity** of the entity in its lifecycle. So; * Use **private setter** for a property when you need to perform any **logic** while setting that property. * Define public methods to manipulate such properties. **Example: Methods to change the properties in a controlled way** ````csharp using System; using Volo.Abp; using Volo.Abp.Domain.Entities; namespace IssueTracking.Issues { public class Issue : AggregateRoot { public Guid RepositoryId { get; private set; } //Never changes public string Title { get; private set; } //Needs validation public string Text { get; set; } //No validation public Guid? AssignedUserId { get; set; } //No validation public bool IsClosed { get; private set; } //Should change with CloseReason public IssueCloseReason? CloseReason { get; private set; } //Should change with IsClosed //... public void SetTitle(string title) { Title = Check.NotNullOrWhiteSpace(title, nameof(title)); } public void Close(IssueCloseReason reason) { IsClosed = true; CloseReason = reason; } public void ReOpen() { IsClosed = false; CloseReason = null; } } } ```` * `RepositoryId` setter made private and there is no way to change it after creating an `Issue` because this is what we want for this domain: An issue can't be moved to another repository. * `Title` setter made private and `SetTitle` method has been created if you want to change it later in a controlled way. * `Text` and `AssignedUserId` has public setter since there is no restriction on them. They can be null or any other value. We think it is unnecessary to define separate methods to set them. If we need later, we can add methods and make the setters private. Breaking changes are not problem in the domain layer since the domain layer is an internal project, it is not exposed to clients. * `IsClosed` and `IssueCloseReason` are pair properties. Defined `Close` and `ReOpen` methods to change them together. In this way, we prevent to close an issue without any reason. ##### Business Logic & Exceptions in the Entities TODO