📋 CrewEmployee Domain Class Documentation
Welcome to the comprehensive documentation for the CrewEmployee domain class in the IrbyImaging.DomainClasses project. This guide will walk you through its purpose, structure, relationships, and show you how to use it in Entity Framework Core.
📑 Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Class Definition
4.1. Namespace & Attributes
4.2. Properties & Relationships - Entity Relationships & Delete Behaviors
- Database Context Configuration (EF Core)
- UML Class Diagram (Mermaid)
- Sample Code Blocks & Usage Examples
8.1. Creating a CrewEmployee
8.2. Querying Data
8.3. Updating & Deleting - View Models & Data Models
- Helper Methods & Utilities
- Error Handling & Return Types
- API Endpoints Overview
- Third‑party Integrations
- Conclusion
1. Overview & Purpose
The CrewEmployee class represents the many-to-many relationship between Crews and Employees in the Irby Imaging domain. Each record associates an employee with a crew, and can optionally carry cost codes, job classifications, per diem information, start times, and auditing metadata.
Use Case: When you need to assign employees to crews on a project, track rates, start times, and audit who created the assignment.
2. Key Functional Areas
-
🎯 Relationship Mapping
- Links
Crew↔Employee(many-to-many) - Optional link to
CostCodeandJobClassification
- Links
-
💼 Per Diem & Rates
PerDiemType&PerDiemRatefor expense tracking
-
⏰ Timing
StartTimefor scheduling
-
📝 Auditing
CreatedBy,CreatedDate, and soft-delete (IsDeleted)
3. Dependencies & Technologies
- .NET 6+
- C# 10
- Entity Framework Core
Microsoft.EntityFrameworkCoreSystem.ComponentModel.DataAnnotations.Schema
4. Class Definition
using System;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace IrbyImaging.DomainClasses
{
[Table("CrewEmployee", Schema = "irby")]
public class CrewEmployee
{
public Guid CrewId { get; set; }
public Crew Crew { get; set; }
public Guid EmployeeId { get; set; }
public Employee Employee { get; set; }
public Guid? CostCodeId { get; set; }
[DeleteBehavior(DeleteBehavior.Restrict)]
public CostCode? CostCode { get; set; }
public Guid? JobClassificationId { get; set; }
[DeleteBehavior(DeleteBehavior.Restrict)]
public JobClassification? JobClassification { get; set; }
public string PerDiemType { get; set; }
public decimal? PerDiemRate { get; set; }
public DateTime? StartTime { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public bool IsDeleted { get; set; }
}
}
4.1 Namespace & Attributes
- Namespace:
IrbyImaging.DomainClasses - [Table] Attribute: Maps to table
irby.CrewEmployeein the database.
4.2 Properties & Relationships
| Property | Type | Purpose |
|---|---|---|
CrewId |
Guid |
FK to Crew |
Crew |
Crew |
Navigation property |
EmployeeId |
Guid |
FK to Employee |
Employee |
Employee |
Navigation property |
CostCodeId |
Guid? |
Optional FK to CostCode |
CostCode |
CostCode? |
Navigation; DeleteBehavior.Restrict |
JobClassificationId |
Guid? |
Optional FK to JobClassification |
JobClassification |
JobClassification? |
Navigation; DeleteBehavior.Restrict |
PerDiemType |
string |
Type of per diem (e.g., “Lodging”, “Meals”) |
PerDiemRate |
decimal? |
Rate per diem |
StartTime |
DateTime? |
When the employee starts |
CreatedBy |
string |
Username or system that created the record |
CreatedDate |
DateTime |
Timestamp of creation |
IsDeleted |
bool |
Soft-delete flag |
5. Entity Relationships & Delete Behaviors
-
Crew ↔ CrewEmployee
- One Crew can have many CrewEmployees.
- On delete of Crew: default cascade or configured separately.
-
Employee ↔ CrewEmployee
- One Employee can belong to many crews.
-
CostCode & JobClassification
- Both are optional (
Guid?). [DeleteBehavior.Restrict]: prevents deletion of a CostCode or JobClassification if referenced by any CrewEmployee.
- Both are optional (
6. Database Context Configuration (EF Core)
public class IrbyContext : DbContext
{
public DbSet<CrewEmployee> CrewEmployees { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Composite Primary Key
modelBuilder.Entity<CrewEmployee>()
.HasKey(ce => new { ce.CrewId, ce.EmployeeId });
// Crew Relationship
modelBuilder.Entity<CrewEmployee>()
.HasOne(ce => ce.Crew)
.WithMany(c => c.CrewEmployees)
.HasForeignKey(ce => ce.CrewId);
// Employee Relationship
modelBuilder.Entity<CrewEmployee>()
.HasOne(ce => ce.Employee)
.WithMany(e => e.CrewEmployees)
.HasForeignKey(ce => ce.EmployeeId);
// CostCode Relationship (Restrict)
modelBuilder.Entity<CrewEmployee>()
.HasOne(ce => ce.CostCode)
.WithMany(cc => cc.CrewEmployees)
.HasForeignKey(ce => ce.CostCodeId)
.OnDelete(DeleteBehavior.Restrict);
// JobClassification Relationship (Restrict)
modelBuilder.Entity<CrewEmployee>()
.HasOne(ce => ce.JobClassification)
.WithMany(jc => jc.CrewEmployees)
.HasForeignKey(ce => ce.JobClassificationId)
.OnDelete(DeleteBehavior.Restrict);
}
}
7. UML Class Diagram (Mermaid)
classDiagram
CrewEmployee ..> Crew : CrewId
CrewEmployee ..> Employee : EmployeeId
CrewEmployee ..> CostCode : CostCodeId
CrewEmployee ..> JobClassification : JobClassificationId
class CrewEmployee {
+Guid CrewId
+Crew Crew
+Guid EmployeeId
+Employee Employee
+Guid? CostCodeId
+CostCode? CostCode
+Guid? JobClassificationId
+JobClassification? JobClassification
+string PerDiemType
+decimal? PerDiemRate
+DateTime? StartTime
+string CreatedBy
+DateTime CreatedDate
+bool IsDeleted
}
8. Sample Code Blocks & Usage Examples
8.1 Creating a CrewEmployee
var crewEmployee = new CrewEmployee
{
CrewId = existingCrew.Id,
EmployeeId = existingEmployee.Id,
CostCodeId = existingCostCode?.Id,
JobClassificationId = existingJobClass?.Id,
PerDiemType = "Meals",
PerDiemRate = 45.00m,
StartTime = new DateTime(2023, 08, 01, 08, 00, 00),
CreatedBy = currentUser,
CreatedDate = DateTime.UtcNow,
IsDeleted = false
};
_context.CrewEmployees.Add(crewEmployee);
await _context.SaveChangesAsync();
8.2 Querying Data
// Fetch all employees in a given crew with related data
var crewWithEmployees = await _context.CrewEmployees
.Where(ce => ce.CrewId == crewId && !ce.IsDeleted)
.Include(ce => ce.Employee)
.Include(ce => ce.CostCode)
.Include(ce => ce.JobClassification)
.ToListAsync();
8.3 Updating & Deleting
// Soft-delete
var record = await _context.CrewEmployees.FindAsync(crewId, employeeId);
if (record != null)
{
record.IsDeleted = true;
await _context.SaveChangesAsync();
}
// Update per diem rate
record.PerDiemRate = 50.00m;
await _context.SaveChangesAsync();
9. View Models & Data Models
Note:
CrewEmployeeis a Data Model (EF Core entity).
For API or UI, you might map it to a ViewModel:
public class CrewEmployeeDto
{
public Guid EmployeeId { get; set; }
public string EmployeeName { get; set; }
public decimal? PerDiemRate { get; set; }
public DateTime? StartTime { get; set; }
}
Use AutoMapper or manual mapping to transform entities.
10. Helper Methods & Utilities
This class is a plain entity—no internal helper methods.
Tip: Create extension methods for common queries:
public static IQueryable<CrewEmployee> Active(this IQueryable<CrewEmployee> q)
=> q.Where(ce => !ce.IsDeleted);
11. Error Handling & Return Types
- Errors: Handled at service or repository layer (e.g., retries, validation).
- Return Types: Typically
Task<CrewEmployee>orTask<IEnumerable<CrewEmployee>>in async services.
12. API Endpoints Overview
This class has no direct endpoints.
In an ASP.NET Core Web API, you might expose:
GET /api/crews/{crewId}/employees
POST /api/crews/{crewId}/employees
DELETE /api/crews/{crewId}/employees/{employeeId}
Mapping to service methods that operate on CrewEmployee.
13. Third‑party Integrations
- Entity Framework Core for ORM
- Optional: AutoMapper for DTO mapping
14. Conclusion
The CrewEmployee class is a cornerstone of crew management in the Irby Imaging domain. It elegantly captures crew assignments, cost control, scheduling, and auditing needs. By configuring it properly in your DbContext, and following the usage examples above, you can seamlessly manage crew–employee relationships and ensure data integrity with EF Core’s powerful features.
Happy coding! 🎉