What is Clean Architecture: Part 7 - Create Domain Project

moh_moh701

mohamed Tayel

Posted on August 26, 2024

What is Clean Architecture: Part 7 - Create Domain Project

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

  1. 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.

Adding New Project

Configuring the Project Structure

Image description

  1. 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, and Order. Image description
      • Common: This folder will house common classes that other entities might inherit, such as the AuditableEntity.

Image description

  1. Create Domain Entities
    • Add the following classes to the Entities folder:

Image description
Category.cs



   public class Category 
   {
       public Guid CategoryId { get; set; }
       public string Name { get; set; } = string.Empty;
       public ICollection<Event>? Events { get; set; }
   }


Enter fullscreen mode Exit fullscreen mode

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!;
   }


Enter fullscreen mode Exit fullscreen mode

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; }
   }


Enter fullscreen mode Exit fullscreen mode

AuditableEntity.cs (in the Common folder)
Image description



   public class AuditableEntity
   {
       public string? CreatedBy { get; set; }
       public DateTime CreatedDate { get; set; }
       public string? LastModifiedBy { get; set; }
       public DateTime? LastModifiedDate { get; set; }
   }


Enter fullscreen mode Exit fullscreen mode
  1. Inherit Common Properties
    • The Category, Event, and Order entities all inherit from AuditableEntity, which provides common properties like CreatedBy, CreatedDate, LastModifiedBy, and LastModifiedDate. This inheritance allows for easy tracking of who created or modified records.

Image description
Category.cs



public class Category: AuditableEntity
{
public Guid CategoryId { get; set; }
public string Name { get; set; } = string.Empty;
public ICollection<Event>? Events { get; set; }
}

Enter fullscreen mode Exit fullscreen mode




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.


💖 💪 🙅 🚩
moh_moh701
mohamed Tayel

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