What is Clean Architecture: Part 7 - Create Domain Project
mohamed Tayel
Posted on August 26, 2024
In this article, we'll walk through the process of setting up the domain layer of our application by creating a Visual Studio solution. This domain layer will house the core entities that define our application's business logic. We'll implement this using a clean architecture approach, which ensures that our domain remains independent of external dependencies like data access or UI frameworks.
Creating the Domain Project
Based on the domain discussed during the initial planning stage, we will start by setting up the domain project. The domain will be a part of the Core application, which typically includes all the business logic and core entities of the application. We'll place this domain in a separate project, keeping it independent and focused.
Steps to Create the Domain Project
-
Add a New Class Library Project
- Start by creating a new class library project within your Visual Studio solution.
- Navigate to the solution explorer, right-click on the Core folder, and select
Add > New Project...
. - In the project templates, search for "Class Library," choose the C# version, and name the project
GloboTicket.TicketManagement.Domain
.
-
Set Up the Project Structure
- Once the project is created, delete the default
Class1.cs
file. - Organize the project by creating the following folders:
-
Entities: This folder will contain our domain entities like
Event
,Category
, andOrder
. -
Common: This folder will house common classes that other entities might inherit, such as the
AuditableEntity
.
-
Entities: This folder will contain our domain entities like
- Once the project is created, delete the default
-
Create Domain Entities
- Add the following classes to the
Entities
folder:
- Add the following classes to the
public class Category
{
public Guid CategoryId { get; set; }
public string Name { get; set; } = string.Empty;
public ICollection<Event>? Events { get; set; }
}
Event.cs
public class Event
{
public Guid EventId { get; set; }
public string Name { get; set; } = string.Empty;
public int Price { get; set; }
public string? Artist { get; set; }
public DateTime Date { get; set; }
public string? Description { get; set; }
public string? ImageUrl { get; set; }
public Guid CategoryId { get; set; }
public Category Category { get; set; } = default!;
}
Order.cs
public class Order
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public int OrderTotal { get; set; }
public DateTime OrderPlaced { get; set; }
public bool OrderPaid { get; set; }
}
AuditableEntity.cs (in the Common
folder)
public class AuditableEntity
{
public string? CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public string? LastModifiedBy { get; set; }
public DateTime? LastModifiedDate { get; set; }
}
-
Inherit Common Properties
- The
Category
,Event
, andOrder
entities all inherit fromAuditableEntity
, which provides common properties likeCreatedBy
,CreatedDate
,LastModifiedBy
, andLastModifiedDate
. This inheritance allows for easy tracking of who created or modified records.
- The
public class Category: AuditableEntity
{
public Guid CategoryId { get; set; }
public string Name { get; set; } = string.Empty;
public ICollection<Event>? Events { get; set; }
}
Conclusion
With these steps, we have successfully set up the core domain project for our application. This project is now ready to be expanded with additional business logic and connected to other layers, such as the infrastructure and UI. Keeping the domain project clean and focused ensures that our application remains maintainable and scalable.
For the complete source code, you can visit the GitHub repository: Clean Architecture on GitHub.
Posted on August 26, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
August 29, 2024