πŸ—‚οΈ CarrierType.cs Documentation


πŸ“‹ Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
  3. Dependencies & Technologies
  4. Data Model & View Models
  5. Entity Relationship (UML Diagram)
  6. Data Annotations & Schema Mapping
  7. Sample Code Blocks & Usage Examples
  8. Error Handling & Validation
  9. Third-party Integrations
  10. Conclusion

1. Overview & Purpose

CarrierType.cs defines two Plain Old CLR Objects (POCOs) used in the IrbyImaging.DomainClasses assembly. These classes represent the Carrier Type master data and its associated detail records. They are designed for:

  • πŸ”„ Persistence via Entity Framework Core
  • πŸ”Ž Domain Modeling of carrier-related information
  • πŸ“Š Hierarchical Organization (one-to-many)

Use Case: Imagine an application that needs to store different kinds of shipment carriers (e.g., UPS, FedEx) and additional detail records per carrier (regions, service levels). CarrierType and CarrierTypeDetail fulfill that requirement.


2. Key Functional Areas

  • Entity Definition: Domain classes representing database tables.
  • Schema Mapping: Attribute-based EF Core mapping.
  • Audit Fields: Standard AddedBy, AddedDate, ModifiedBy, ModifiedDate.
  • Active/Inactive Flag: Soft-delete or deactivation via IsInactive.
  • Relationships: One CarrierType β†’ Many CarrierTypeDetail records.

3. Dependencies & Technologies

Dependency Purpose
.NET 5+ / .NET Core Runtime environment
Entity Framework Core ORM for object–relational mapping
System.ComponentModel.DataAnnotations Validation and display metadata
System.ComponentModel.DataAnnotations.Schema Database schema mapping attributes

4. Data Model & View Models

4.1 CarrierType Entity

[Table("CarrierType", Schema = "irby")]
public class CarrierType
{
    [Key]
    public Guid Id { get; set; }

    public string Carrier { get; set; }
    
    public bool IsInactive { get; set; }

    [Display(Name = "Added By")]
    public string AddedBy { get; set; }

    [Display(Name = "Added Date")]
    public DateTime AddedDate { get; set; }

    [Display(Name = "Modified By")]
    public string ModifiedBy { get; set; }

    [Display(Name = "Modified Date")]
    public DateTime ModifiedDate { get; set; }

    public List<CarrierTypeDetail> CarriersTypeDetail { get; set; }
}

Key Points

  • Id: Primary key (GUID).
  • Carrier: Name/description of the carrier (e.g., "UPS").
  • IsInactive: Marks the carrier type as inactive (soft delete).
  • Audit Fields: Track who/when created or modified.
  • CarriersTypeDetail: Navigation property to detail records.

4.2 CarrierTypeDetail Entity

public class CarrierTypeDetail
{
    public Guid Id { get; set; }

    public string Carrier { get; set; }

    public bool IsInactive { get; set; }

    public string AddedBy { get; set; }
    public DateTime AddedDate { get; set; }

    public string ModifiedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
}

Differences from Parent

  • No [Key] or [Table] attributes (inherits conventions).
  • Relies on EF Core’s Fluent API or conventions to map the FK to CarrierType.Id.

5. Entity Relationship (UML Diagram)

classDiagram
    CarrierType "1" -- "0..*" CarrierTypeDetail : has >
    class CarrierType {
        +Guid Id
        +string Carrier
        +bool IsInactive
        +string AddedBy
        +DateTime AddedDate
        +string ModifiedBy
        +DateTime ModifiedDate
        +List<CarrierTypeDetail> CarriersTypeDetail
    }
    class CarrierTypeDetail {
        +Guid Id
        +string Carrier
        +bool IsInactive
        +string AddedBy
        +DateTime AddedDate
        +string ModifiedBy
        +DateTime ModifiedDate
    }

6. Data Annotations & Schema Mapping

Attribute Entity Property Description
[Table("CarrierType", Schema="irby")] Class CarrierType Map to table irby.CarrierType.
[Key] Id Primary key of the entity.
[Display(Name="...")] Audit fields Friendly field names for UI scaffolding.

Note: CarrierTypeDetail relies on EF conventions for primary key (Id) and foreign key (CarrierTypeId, to be configured in DbContext).


7. Sample Code Blocks & Usage Examples

7.1 Defining DbContext

public class AppDbContext : DbContext
{
    public DbSet<CarrierType> CarrierTypes { get; set; }
    public DbSet<CarrierTypeDetail> CarrierTypeDetails { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<CarrierType>()
            .HasMany(ct => ct.CarriersTypeDetail)
            .WithOne()
            .HasForeignKey("CarrierTypeId")
            .OnDelete(DeleteBehavior.Cascade);

        base.OnModelCreating(modelBuilder);
    }
}

7.2 Adding a New CarrierType

using(var context = new AppDbContext())
{
    var carrier = new CarrierType
    {
        Id = Guid.NewGuid(),
        Carrier = "FastExpress",
        IsInactive = false,
        AddedBy = "admin",
        AddedDate = DateTime.UtcNow,
        ModifiedBy = "admin",
        ModifiedDate = DateTime.UtcNow,
        CarriersTypeDetail = new List<CarrierTypeDetail>
        {
            new CarrierTypeDetail {
                Id = Guid.NewGuid(),
                Carrier = "FastExpress US",
                AddedBy = "admin",
                AddedDate = DateTime.UtcNow,
                ModifiedBy = "admin",
                ModifiedDate = DateTime.UtcNow
            }
        }
    };

    context.CarrierTypes.Add(carrier);
    context.SaveChanges();
}

7.3 Querying Active Carrier Types

var activeCarriers = context.CarrierTypes
    .Where(ct => !ct.IsInactive)
    .Include(ct => ct.CarriersTypeDetail)
    .ToList();

foreach(var ct in activeCarriers)
{
    Console.WriteLine($"{ct.Carrier} (Details: {ct.CarriersTypeDetail.Count})");
}

8. Error Handling & Validation

  • Validation Attributes: Extend with [Required], [StringLength], etc., as needed.
  • DbContext Exceptions: Catch DbUpdateException / DbUpdateConcurrencyException around SaveChanges().
  • Soft Delete Pattern: Set IsInactive = true instead of hard delete to preserve history.
try
{
    context.SaveChanges();
}
catch(DbUpdateException ex)
{
    // Log and handle DB errors
    throw new ApplicationException("Failed to save CarrierType", ex);
}

9. Third-party Integrations

  • Entity Framework Core: For ORM and migrations.
  • FluentValidation (optional): Could integrate for richer input validation.
  • Swagger / OpenAPI (optional): If you expose API endpoints around carriers.

10. Conclusion

πŸ“ CarrierType.cs provides a robust domain model for managing carrier master data within the IrbyImaging system. With a clean entity design, built-in audit fields, and EF Core integration, it lays a solid foundation for CRUD operations, reporting, and business logic. Extend it by adding validation, DTO mapping, and API layers to build comprehensive carrier management features.


Happy Coding! πŸš€