π Department.cs Documentation
π Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Data Model: Department Class
4.1. Entity Framework Mapping
4.2. Core Properties
4.3. NotMapped Navigation Properties - UML Class Diagram
- Sample Code & Usage Examples
- API Endpoints Overview
- Error Handling & Return Types
- Third-party Integrations & Utilities
- Conclusion
π 1. Overview & Purpose
The Department class is part of the IrbyImaging.DomainClasses namespace. It represents a Department entity in the systemβs domain model and is used to:
- Persist department data in the
irby.Departmenttable via Entity Framework Core. - Track metadata such as creation/modification timestamps and βsoft-deleteβ status.
- Hold runtime-only navigation lists to related
RoleandEmployeeobjects.
This model underpins business logic around departmental structure, assignment of employees, and role management.
π― 2. Key Functional Areas
- Data Persistence: Maps to a database table with schema βirbyβ.
- Soft Deletion: The
IsDeletedflag marks records as logically removed. - Audit Metadata: Tracks who/when records are added or modified.
- Navigation Properties: Provides in-memory lists of associated
RoleandEmployeeentities.
π₯ Dependencies & Technologies
| Technology / Package | Purpose |
|---|---|
| .NET (Core / Framework) | Runtime & language |
| EntityFrameworkCore (Data Annotations) | ORM mapping via [Table], [Key], etc. |
| System.ComponentModel.DataAnnotations | Model metadata & validation attributes |
| System.ComponentModel.DataAnnotations.Schema | Table & column mapping |
ποΈ 4. Data Model: Department Class
namespace IrbyImaging.DomainClasses
{
[Table("Department", Schema = "irby")]
public class Department
{
[Key]
public Guid DepartmentId { get; set; }
[Display(Name = "Department Name")]
public string Name { get; set; }
[Display(Name = "Department Description")]
public string Description { get; set; }
public bool IsDeleted { get; set; }
public string AddedBy { get; set; }
public DateTime AddedDate { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
[NotMapped]
public List<Role> Roles { get; set; }
[NotMapped]
public List<Employee> Employees { get; set; }
}
}
4.1. Entity Framework Mapping
[Table("Department", Schema = "irby")]
Maps the class toirby.Department.[Key]
MarksDepartmentIdas the primary key.
4.2. Core Properties
| Property | Type | Description |
|---|---|---|
| DepartmentId | Guid | PK: Unique identifier. |
| Name | string | Display name of the department. |
| Description | string | Detailed description. |
| IsDeleted | bool | Soft-delete flag (true = deleted). |
| AddedBy | string | Username or system adding the record. |
| AddedDate | DateTime | Timestamp when record was created. |
| ModifiedBy | string | Username or system updating the record. |
| ModifiedDate | DateTime | Timestamp when record was last updated. |
4.3. NotMapped Navigation Properties
These lists are not stored in the DB; they are populated at runtime:
List<Role> RolesList<Employee> Employees
Use them to aggregate related domain objects after querying the database.
ποΈ 5. UML Class Diagram
classDiagram
class Department {
+Guid DepartmentId
+string Name
+string Description
+bool IsDeleted
+string AddedBy
+DateTime AddedDate
+string ModifiedBy
+DateTime ModifiedDate
+List<Role> Roles
+List<Employee> Employees
}
Department "1" o-- "0..*" Role : contains
Department "1" o-- "0..*" Employee : contains
π‘ 6. Sample Code & Usage Examples
6.1. Adding a New Department
using(var context = new IrbyDbContext())
{
var dept = new Department
{
DepartmentId = Guid.NewGuid(),
Name = "Radiology",
Description = "Handles imaging and scans",
IsDeleted = false,
AddedBy = currentUser,
AddedDate = DateTime.UtcNow,
ModifiedBy = currentUser,
ModifiedDate = DateTime.UtcNow
};
context.Departments.Add(dept);
context.SaveChanges();
}
6.2. Soft-Deleting a Department
var dept = context.Departments.Find(deptId);
if(dept != null)
{
dept.IsDeleted = true;
dept.ModifiedBy = currentUser;
dept.ModifiedDate = DateTime.UtcNow;
context.SaveChanges();
}
6.3. Eager Loading Related Entities
var departmentWithEmployees = context.Departments
.Where(d => !d.IsDeleted)
.Select(d => new Department
{
DepartmentId = d.DepartmentId,
Name = d.Name,
Roles = context.Roles.Where(r => r.DepartmentId == d.DepartmentId).ToList(),
Employees = context.Employees.Where(e => e.DepartmentId == d.DepartmentId).ToList()
})
.FirstOrDefault();
π« 7. API Endpoints Overview
Note: This class itself does not define any API endpoints. It is purely a domain/data model.
Endpoints leveragingDepartmenttypically reside in a Web API controller in a separate project.
π οΈ 8. Error Handling & Return Types
- Entity Framework Exceptions: Handle
DbUpdateException,DbEntityValidationExceptionduringSaveChanges(). - Null Checks: Always verify existence before updates/deletions.
- Return Types: For service methods returning departments, consider using
IEnumerable<Department>orTask<Department>for async.
π 9. Thirdβparty Integrations & Utilities
- AutoMapper (optional): Map between domain classes and view models or DTOs.
- FluentValidation (optional): Validate
NameandDescriptionbefore persistence.
βοΈ 10. Conclusion
The Department class is a fundamental building block in the IrbyImaging domain. It:
- Defines how departments are stored and retrieved.
- Tracks audit data and supports soft deletion.
- Offers runtime navigation to related
RoleandEmployeeentities.
Use this model in tandem with EF Core to maintain a clean, consistent representation of departmental data across your application.
π Happy coding with IrbyImaging!