Clean Architecture P7: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 us…


This content originally appeared on DEV Community and was authored by mohamed Tayel

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

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

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.

This article covers the essential steps to set up a domain project using clean architecture principles. The domain project serves as the foundation for your application's business logic, keeping it separate from infrastructure concerns. This separation ensures that your core logic remains independent, flexible, and easy to maintain.


This content originally appeared on DEV Community and was authored by mohamed Tayel


Print Share Comment Cite Upload Translate Updates
APA

mohamed Tayel | Sciencx (2024-08-26T21:24:53+00:00) Clean Architecture P7:Create Domain Project. Retrieved from https://www.scien.cx/2024/08/26/clean-architecture-p7create-domain-project/

MLA
" » Clean Architecture P7:Create Domain Project." mohamed Tayel | Sciencx - Monday August 26, 2024, https://www.scien.cx/2024/08/26/clean-architecture-p7create-domain-project/
HARVARD
mohamed Tayel | Sciencx Monday August 26, 2024 » Clean Architecture P7:Create Domain Project., viewed ,<https://www.scien.cx/2024/08/26/clean-architecture-p7create-domain-project/>
VANCOUVER
mohamed Tayel | Sciencx - » Clean Architecture P7:Create Domain Project. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/26/clean-architecture-p7create-domain-project/
CHICAGO
" » Clean Architecture P7:Create Domain Project." mohamed Tayel | Sciencx - Accessed . https://www.scien.cx/2024/08/26/clean-architecture-p7create-domain-project/
IEEE
" » Clean Architecture P7:Create Domain Project." mohamed Tayel | Sciencx [Online]. Available: https://www.scien.cx/2024/08/26/clean-architecture-p7create-domain-project/. [Accessed: ]
rf:citation
» Clean Architecture P7:Create Domain Project | mohamed Tayel | Sciencx | https://www.scien.cx/2024/08/26/clean-architecture-p7create-domain-project/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.