π ContractTypeDetails Domain Model Documentation
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Data Model & Schema Mapping
- Class Properties Explained
- EF Core Configuration
- UML Class Diagram
- Usage Examples & Code Snippets
- Best Practices & Tips
- Conclusion
1. Overview & Purpose
The ContractTypeDetails class is a Domain Model within the IrbyImaging.DomainClasses namespace. It represents the structure of the ContractTypeDetails table in your database, capturing a summary of contracts by state and work type. This class is intended for:
- π Data storage: Persisting contract statistics (counts) per state and work category.
- π Entity Framework Core mapping: Leveraging EF Core for CRUD operations.
- π€ Business logic integration: Serving as a data carrier in repository or service layers.
2. Dependencies & Technologies
| Technology / Package | Purpose |
|---|---|
| .NET Core / .NET 5+ | Runtime for the application. |
| Microsoft.EntityFrameworkCore | ORM for mapping C# classes to database tables. |
| System.ComponentModel.DataAnnotations | Provides attribute-based validation and schema mapping (e.g., [Key], [Table]). |
3. Data Model & Schema Mapping
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace IrbyImaging.DomainClasses
{
[Table("ContractTypeDetails", Schema = "irby")]
public class ContractTypeDetails
{
[Key]
public Guid Id { get; set; }
public string State { get; set; }
public string TypeOfWork { get; set; }
public int NumberOfContracts { get; set; }
}
}
- [Table] attribute
- Maps this class to the
irby.ContractTypeDetailstable.
- Maps this class to the
- [Key] attribute
- Marks
Idas the primary key column.
- Marks
4. Class Properties Explained
| Property | Type | Nullable | Description |
|---|---|---|---|
| Id | Guid | No | Unique identifier for each record. Primary key. |
| State | string | Yes | U.S. state (e.g., "NY", "CA"). |
| TypeOfWork | string | Yes | Category or type of the contract/work performed. |
| NumberOfContracts | int | No | Total count of contracts matching the state & type. |
βΉοΈ Note: By default, strings are mapped to nullable columns in EF Core. Adjust via Fluent API or
[Required]if you need non-nullable strings.
5. EF Core Configuration
No extra Fluent API configuration is required beyond the dataβannotation attributes. However, you can further customize in your DbContext:
public class IrbyContext : DbContext
{
public DbSet<ContractTypeDetails> ContractTypeDetails { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ContractTypeDetails>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.State).HasMaxLength(2);
entity.Property(e => e.TypeOfWork).HasMaxLength(100);
});
}
}
- HasMaxLength: Enforces column length in the database.
- HasKey: Explicit primary key declaration (redundant if
[Key]is used).
6. UML Class Diagram
classDiagram
IrbyImaging.DomainClasses.ContractTypeDetails {
+Guid Id
+string State
+string TypeOfWork
+int NumberOfContracts
}
7. Usage Examples & Code Snippets
7.1. Inserting a New Record
using (var context = new IrbyContext())
{
var detail = new ContractTypeDetails
{
Id = Guid.NewGuid(),
State = "TX",
TypeOfWork = "Electrical",
NumberOfContracts = 42
};
context.ContractTypeDetails.Add(detail);
context.SaveChanges();
}
7.2. Fetching & Querying
// Get all Texas electrical contracts
var texasElectrical = await context.ContractTypeDetails
.Where(d => d.State == "TX" && d.TypeOfWork == "Electrical")
.ToListAsync();
7.3. Updating a Record
var record = context.ContractTypeDetails.First(d => d.Id == existingId);
record.NumberOfContracts += 1;
context.SaveChanges();
7.4. Deleting a Record
var toDelete = context.ContractTypeDetails.Find(existingId);
context.ContractTypeDetails.Remove(toDelete);
context.SaveChanges();
8. Best Practices & Tips
- π Validation: Use
[Required],[MaxLength], or Fluent API to enforce data integrity. - π Concurrency: Consider adding a
RowVersion(byte[]) for optimistic concurrency control. - π¦ Batch Operations: For bulk insert/update, look into EF Coreβs
BulkExtensionsor raw SQL for performance. - βοΈ Migration: After changing the model, run
Add-Migration&Update-Database.
9. Conclusion
The ContractTypeDetails class is a simple yet essential building block in the Irby Imaging domain, capturing critical contract statistics across states and work types. By combining EF Core attributes and best practices, you can efficiently persist and query this data, integrating it seamlessly into your business logic layers.
Happy coding! π
Generated by the Irby Imaging Documentation Generator π