refactor: clean project based session01 notes
This commit is contained in:
@@ -102,12 +102,9 @@ var invalidUser = new User(); // ❌ Compilation Error: Name is required
|
||||
|
||||
## **A note about strings in C# (Chat GPT) :**
|
||||
**`string` is nullable in C#**, but in **nullable reference types (C# 8+), `string` is treated as non-nullable unless explicitly marked `string?`**.
|
||||
|
||||
- **`string`** → Default behavior (non-nullable by default in nullable context).
|
||||
- **`string?`** → Explicitly nullable.
|
||||
|
||||
|
||||
|
||||
## **Why virtual navigation properties? (Chat GPT):**
|
||||
|
||||
- If you mark a navigation property as `virtual`, EF Core **creates a proxy class** at runtime that overrides the property and loads related data **only when accessed**.
|
||||
@@ -148,3 +145,5 @@ var tickets = context.Tickets.ToList(); // Loads all tickets foreach (var ticket
|
||||
|
||||
---
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
# Branching
|
||||
|
||||
# 🛠️ Task Checklist
|
||||
|
||||
## Preparation
|
||||
### EF Core Code First
|
||||
- [ ] Watch this [video](https://www.youtube.com/watch?v=b8fFRX0T38M&ab_channel=PatrickGod)
|
||||
## 🚧 Branching
|
||||
- [ ] Create the develop branch
|
||||
- [ ] Create the feature/domain-entities branch based on develop
|
||||
|
||||
# `IEntity.cs`
|
||||
## `IEntity.cs`
|
||||
- [ ] Create the IEntity **interface**
|
||||
|
||||
> Location: Domain Project > Framework > Interfaces
|
||||
📂 Suggested Folder: `Domain/Framework/Interfaces`
|
||||
```C#
|
||||
public interface IEntity<TKey>
|
||||
{
|
||||
public TKey Id { get; set; }
|
||||
}
|
||||
```
|
||||
# `Entity.cs`
|
||||
## `Entity.cs`
|
||||
- [ ] Create the Entity **class**
|
||||
|
||||
> Location: Domain Project > Framework > Base
|
||||
📂 Suggested Folder: `Domain/Framework/Base`
|
||||
```C#
|
||||
public class Entity<TKey> : IEntity<TKey>
|
||||
{
|
||||
@@ -23,20 +26,25 @@ public class Entity<TKey> : IEntity<TKey>
|
||||
}
|
||||
```
|
||||
|
||||
### Why do we need IEntity and Entity? (Chat GPT):
|
||||
> this approach is **valid and commonly used** in **Domain-Driven Design (DDD)** and **Clean Architecture**. It provides **consistency**, **reusability**, and **common functionality** across all entities.
|
||||
# Create Entities
|
||||
> Location: Domain Project > Aggregates > (RelatedFolder)
|
||||
|
||||
### They all (with the exception of join tables) should inherit Entity, and you should specify the datatype of the Id
|
||||
### Use the latest version of ERD to specify the properties
|
||||
### You can use this project as a reference:
|
||||
https://github.com/MehrdadShirvani/AlibabaClone-Backend/tree/develop/AlibabaClone.Domain/Aggregates
|
||||
### Link to the ERD:
|
||||
https://github.com/TheOrderOfPhoenix/ASP.NET/tree/main/02_ProjectOrientedSessions/docs
|
||||
|
||||
### One Example:
|
||||
|
||||
## Create Entities
|
||||
📂 Suggested Folder: `Domain/Aggregates/[RelatedFolder]`
|
||||
Entities represent the core business objects in our domain model. By defining them explicitly and consistently, we ensure that:
|
||||
- Our domain logic remains **clear and maintainable**.
|
||||
- We follow **Domain-Driven Design (DDD)** principles, keeping the business rules close to the data they govern.
|
||||
- All developers have a **standardized structure** to follow, improving code readability and collaboration.
|
||||
### Guidelines
|
||||
1. **Base Class**
|
||||
- All entities (except join tables) must inherit from `Entity` and explicitly specify the datatype of their `Id`.
|
||||
2. **Properties**
|
||||
- Use the **latest version of the ERD** to define properties and relationships.
|
||||
3. **Reference Project**
|
||||
- For implementation details, you can refer to this project:
|
||||
👉 [AlibabaClone-Backend Domain Layer](https://github.com/MehrdadShirvani/AlibabaClone-Backend/tree/develop/AlibabaClone.Domain/Aggregates)
|
||||
4. **ERD Reference**
|
||||
- Latest ERD available here:
|
||||
👉 [Project ERD](obsidian://open?vault=ASP.NET&file=Repo%2FProjectOrientedSessions%2Fdocs%2FAlibabaERD-Version02.pdf)### One Example:
|
||||
### Example
|
||||
```C#
|
||||
public class Account : Entity<long>
|
||||
{
|
||||
@@ -44,40 +52,32 @@ public class Account : Entity<long>
|
||||
public required string Password { set; get; }
|
||||
public string? Email { get; set; }
|
||||
public long? PersonId { get; set; }
|
||||
//...
|
||||
}
|
||||
```
|
||||
|
||||
# Add Navigation Properties
|
||||
### What is a navigation property? (the following pieces of code are just there for educational purposes)
|
||||
#### **🔹 Navigation Properties in Entity Framework Core: Everything You Need to Know**
|
||||
|
||||
---
|
||||
|
||||
#### **📌 What Are Navigation Properties?**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## Add Navigation Properties
|
||||
|
||||
Navigation properties in Entity Framework Core (EF Core) **represent relationships between entities**. They allow you to **navigate** (follow) the relationships between different tables using **C# objects** instead of writing SQL joins manually.
|
||||
### Add the needed navigation properties inside entities
|
||||
- [ ] Figure out what navigation properties are needed based on the ERD, and use this project as a reference:
|
||||
https://github.com/MehrdadShirvani/AlibabaClone-Backend/tree/develop/AlibabaClone.Domain/Aggregates
|
||||
- [ ] Don't forget to mark all the navigation properties as `virtual`
|
||||
|
||||
For example, if you have a **Ticket** entity related to a **Buyer**, the navigation property allows you to access the buyer from a ticket without writing a separate SQL query.
|
||||
### **🔹 Examples of Defining Navigation Properties**
|
||||
|
||||
---
|
||||
|
||||
#### **🔹 Types of Navigation Properties**
|
||||
|
||||
Navigation properties can be of two types:
|
||||
|
||||
| Relationship Type | Description |
|
||||
| ---------------------------------- | ----------------------------------------------------------------------------- |
|
||||
| **Reference Navigation Property** | Represents a **single entity** related to another (one-to-one or many-to-one) |
|
||||
| **Collection Navigation Property** | Represents **a list of related entities** (one-to-many or many-to-many) |
|
||||
|
||||
---
|
||||
|
||||
#### **🔹 How to Define Navigation Properties?**
|
||||
|
||||
##### **🔹 One-to-Many Example**
|
||||
|
||||
>These are just examples, and not how the project should look like
|
||||
#### **🔹 One-to-Many Example**
|
||||
A **Buyer** can have **multiple Tickets**, but each **Ticket** belongs to one **Buyer**.
|
||||
|
||||
```csharp
|
||||
public class Buyer
|
||||
{
|
||||
@@ -98,7 +98,7 @@ public class Ticket
|
||||
}
|
||||
```
|
||||
|
||||
##### **🔹 One-to-One Example**
|
||||
#### **🔹 One-to-One Example**
|
||||
|
||||
A **Ticket** can have only **one Transaction**, and a **Transaction** belongs to exactly **one Ticket**.
|
||||
|
||||
@@ -121,7 +121,7 @@ public class Transaction
|
||||
}
|
||||
```
|
||||
|
||||
##### **🔹 Many-to-Many Example**
|
||||
#### **🔹 Many-to-Many Example**
|
||||
|
||||
A **Buyer** can buy **many Tickets**, and each **Ticket** can be bought by **many Buyers** (if resale is allowed).
|
||||
|
||||
@@ -144,13 +144,23 @@ public class Ticket
|
||||
}
|
||||
```
|
||||
|
||||
### Add the needed navigation properties inside entities
|
||||
- [ ] Figure out what navigation properties are needed based on the ERD, and use this project as a reference:
|
||||
https://github.com/MehrdadShirvani/AlibabaClone-Backend/tree/develop/AlibabaClone.Domain/Aggregates
|
||||
- [ ] Don't forget to mark all the navigation properties as `virtual`
|
||||
|
||||
# Add Packages to Infrastructure Project
|
||||
## Add Packages to Infrastructure Project
|
||||
- [ ] Add `Microsoft.EntityFrameworkCore.Proxies` to Infrastructure Project
|
||||
# Merge
|
||||
## 🚧 Merge
|
||||
- [ ] Create a PR and merge the current branch with develop
|
||||
# 🧠 Hints & Notes
|
||||
- Mark navigation properties `virtual`
|
||||
# 🙌 Acknowledgements
|
||||
|
||||
- ChatGPT for snippet refinement and explanations
|
||||
|
||||
# 🔍 References
|
||||
[[Session01 Additional Info]]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
|
||||
# EF Core Code First
|
||||
https://www.youtube.com/watch?v=b8fFRX0T38M&ab_channel=PatrickGod
|
||||
# Repository Pattern
|
||||
|
||||
## Brief Introduction (without DB Context)
|
||||
https://www.youtube.com/watch?v=Wiy54682d1w&ab_channel=PatrickGod
|
||||
## More detailed Introduction:
|
||||
https://youtu.be/rtXpYpZdOzM
|
||||
## Purpose:
|
||||
Meditates between the domain and data mapping layers, acting like an **in-memory collection** of domain objects
|
||||
## Benefits
|
||||
- Minimizes duplicate query logic
|
||||
- Decouples your application from persistence frameworks
|
||||
- Promotes testability
|
||||
> Repository should not have methods like Update and Save
|
||||
# Unit of Work
|
||||
Keeps track of changes and coordinates the writings and savings
|
||||
## Implementation:
|
||||
https://youtu.be/rtXpYpZdOzM?t=703
|
||||
|
||||
Reference in New Issue
Block a user