πŸ“‘ Department.cs Documentation


πŸ“š Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
  3. Dependencies & Technologies
  4. Data Model: Department Class
    4.1. Entity Framework Mapping
    4.2. Core Properties
    4.3. NotMapped Navigation Properties
  5. UML Class Diagram
  6. Sample Code & Usage Examples
  7. API Endpoints Overview
  8. Error Handling & Return Types
  9. Third-party Integrations & Utilities
  10. 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.Department table via Entity Framework Core.
  • Track metadata such as creation/modification timestamps and β€œsoft-delete” status.
  • Hold runtime-only navigation lists to related Role and Employee objects.

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 IsDeleted flag marks records as logically removed.
  • Audit Metadata: Tracks who/when records are added or modified.
  • Navigation Properties: Provides in-memory lists of associated Role and Employee entities.

πŸ–₯ 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 to irby.Department.
  • [Key]
    Marks DepartmentId as 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> Roles
  • List<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();
}
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 leveraging Department typically reside in a Web API controller in a separate project.


πŸ› οΈ 8. Error Handling & Return Types

  • Entity Framework Exceptions: Handle DbUpdateException, DbEntityValidationException during SaveChanges().
  • Null Checks: Always verify existence before updates/deletions.
  • Return Types: For service methods returning departments, consider using IEnumerable<Department> or Task<Department> for async.

πŸ”Œ 9. Third‑party Integrations & Utilities

  • AutoMapper (optional): Map between domain classes and view models or DTOs.
  • FluentValidation (optional): Validate Name and Description before 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 Role and Employee entities.

Use this model in tandem with EF Core to maintain a clean, consistent representation of departmental data across your application.


πŸŽ‰ Happy coding with IrbyImaging!