📋 EditCrewViewModel.cs Documentation

Location: IrbyImaging/UI/Models/EditCrewViewModel.cs
Namespace: IrbyImaging.UI.Models


🗂 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Class Definition & Properties
  5. UML Class Diagram
  6. Usage Examples
  7. Best Practices & Conventions
  8. Conclusion

🎯 1. Overview & Purpose

The EditCrewViewModel is a ViewModel used in an ASP.NET MVC/Core application to facilitate the editing of a Crew entity in the user interface. It encapsulates only the fields required by the view—decoupling your UI layer from the domain/data layer.

  • When to use?
    • Rendering an Edit Crew form (pre-populating existing values).
    • Receiving user input on Crew updates.
    • Validating and transferring data between Controller and View.

🔧 2. Dependencies & Technologies

Dependency Purpose
System Core .NET types (e.g., Guid)
Microsoft.AspNetCore.Mvc (implicit) MVC infrastructure, model binding

Technologies & Patterns

  • ASP.NET Core MVC (or ASP.NET MVC)
  • ViewModel Pattern
  • (Optional) AutoMapper for object mapping

🚀 3. Key Functional Areas

  • Data Binding
    Binds form fields to properties: CrewId, CrewName, CreatedBy.
  • Separation of Concerns
    Keeps UI-specific data isolated from domain entities.
  • Model Validation
    Can be extended with Data Annotations (e.g., [Required], [StringLength]).

📐 4. Class Definition & Properties

using System;

namespace IrbyImaging.UI.Models
{
    public class EditCrewViewModel
    {
        /// <summary>
        /// The unique identifier of the crew being edited.
        /// </summary>
        public Guid CrewId { get; set; }

        /// <summary>
        /// The display name of the crew.
        /// </summary>
        public string CrewName { get; set; }

        /// <summary>
        /// The username or identifier of who originally created this crew.
        /// </summary>
        public string CreatedBy { get; set; }
    }
}
Property Type Description
CrewId Guid Uniquely identifies the Crew record.
CrewName string Human‑readable name of the Crew.
CreatedBy string Creator’s user name or identifier.

🎨 5. UML Class Diagram

@startuml
class EditCrewViewModel {
  +Guid CrewId
  +string CrewName
  +string CreatedBy
}
@enduml
+----------------------+
| EditCrewViewModel    |
+----------------------+
| + CrewId : Guid      |
| + CrewName : string  |
| + CreatedBy : string |
+----------------------+

📝 6. Usage Examples

1. Controller Integration

public class CrewController : Controller
{
    private readonly ICrewService _crewService;

    public CrewController(ICrewService crewService)
    {
        _crewService = crewService;
    }

    // GET: /Crew/Edit/{id}
    [HttpGet]
    public IActionResult Edit(Guid id)
    {
        // Fetch the domain entity from service/data store
        var crew = _crewService.GetById(id);
        if (crew == null) return NotFound();

        // Map to ViewModel
        var viewModel = new EditCrewViewModel
        {
            CrewId   = crew.Id,
            CrewName = crew.Name,
            CreatedBy= crew.CreatedBy
        };

        return View(viewModel);
    }

    // POST: /Crew/Edit
    [HttpPost]
    public IActionResult Edit(EditCrewViewModel model)
    {
        if (!ModelState.IsValid)
            return View(model);

        // Map back to domain entity
        var updatedCrew = new Crew
        {
            Id        = model.CrewId,
            Name      = model.CrewName,
            CreatedBy = model.CreatedBy
        };

        _crewService.Update(updatedCrew);
        return RedirectToAction("Index");
    }
}

2. Razor View Example

@model IrbyImaging.UI.Models.EditCrewViewModel

<form asp-action="Edit" method="post">
    <input asp-for="CrewId" type="hidden" />

    <div class="form-group">
        <label asp-for="CrewName"></label>
        <input asp-for="CrewName" class="form-control" />
        <span asp-validation-for="CrewName" class="text-danger"></span>
    </div>

    <div class="form-group">
        <label asp-for="CreatedBy"></label>
        <input asp-for="CreatedBy" class="form-control" disabled />
    </div>

    <button type="submit" class="btn btn-primary">Save Changes</button>
    <a asp-action="Index" class="btn btn-secondary">Cancel</a>
</form>

3. AutoMapper Mapping

🔄 If you use AutoMapper for DTO ↔ ViewModel mapping, define profiles:

public class CrewProfile : Profile
{
    public CrewProfile()
    {
        CreateMap<Crew, EditCrewViewModel>();
        CreateMap<EditCrewViewModel, Crew>();
    }
}

✅ 7. Best Practices & Conventions

  • Validation Attributes
    Enhance the ViewModel with [Required], [StringLength(100)], etc., to enforce client/server validation.
  • Immutability (optional)
    Consider read‑only auto‑properties and constructor injection for immutable ViewModels.
  • Mapping Layers
    Keep mapping logic in a centralized place (e.g., AutoMapper or custom mappers).
  • Naming
    Suffix with ViewModel to clearly distinguish from domain/data models.

🏁 8. Conclusion

The EditCrewViewModel is a lightweight, UI‑focused data container that:

  • Simplifies data-binding in Edit Crew scenarios
  • Decouples your View from domain entities
  • Serves as the perfect candidate for adding validation and mapping logic

By following best practices—like adding validation attributes and leveraging mapping libraries—you can keep your Controllers and Views clean, maintainable, and safe from over-posting attacks.


✨ Happy Coding!