πŸ“„ EditViewModel.cs Documentation

File Path: IrbyImaging/Models/Shared/EditViewModel.cs
Assembly: IrbyImaging.UI
Author: kvamshi
Created: 2022‑03‑31
Last Modified: 2022‑03‑31


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Class Summary
  4. Inheritance Hierarchy
  5. Key Functional Areas
  6. UML Class Diagram
  7. Sample Code Blocks
  8. Usage Examples
  9. Error Handling & Return Types
  10. API Endpoints Overview 🚫
  11. Third-Party Integrations πŸ”Œ
  12. Conclusion

1. Overview & Purpose

EditViewModel is an abstract ViewModel designed for the edit/update scenarios in the IrbyImaging UI. It extends the base CreateViewModel to include two critical properties:

  • A unique Id to identify the entity being edited.
  • A mandatory Reason string to capture why the update was performed.

This pattern enforces auditability and user accountability whenever an existing record is modified.


2. Dependencies & Technologies

Category Details
Language C# (.NET Core / .NET 5+)
Framework ASP.NET MVC / Razor Views
NuGet / Assemblies System.ComponentModel.DataAnnotations
Layer Presentation / ViewModels

3. Class Summary

namespace IrbyImaging.Models
{
    /// <summary>
    /// Abstract base class for view models that support edit/update operations.
    /// Inherits from <see cref="CreateViewModel"/>.
    /// </summary>
    public abstract class EditViewModel : CreateViewModel
    {
        /// <summary>
        /// Gets or sets the unique identifier of the entity to edit.
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// Gets or sets the reason for updating the entity.
        /// </summary>
        [Required]
        [Display(Name = "Reason For Update")]
        public string Reason { get; set; }
    }
}
  • Abstract: Cannot be instantiated directly; intended for inheritance.
  • Namespace: IrbyImaging.Models
  • Derived From: CreateViewModel (inherits all properties for creating a new entity).

4. Inheritance Hierarchy

Object
 └─ CreateViewModel    (base for creation scenarios)
     └─ EditViewModel  (adds Id & Reason for update scenarios)

βš™οΈ This design reuses creation logic while augmenting with edit-specific data.


5. Key Functional Areas

5.1 Id Property

Attribute Details
Type Guid
Purpose Uniquely identifies the record being edited.
Usage Sent from the client to server to specify the target ID.
/// <summary>
/// Gets or sets the identifier.
/// </summary>
public Guid Id { get; set; }

5.2 Reason Property

Attribute Details
Type string
Annotations [Required], [Display(Name = "Reason For Update")]
Purpose Captures the justification for the edit.
Validation Message Automatically enforced by MVC if left blank.
/// <summary>
/// Gets or sets the reason.
/// </summary>
[Required]
[Display(Name = "Reason For Update")]
public string Reason { get; set; }

Validation: If the user omits the Reason, the model state will be invalid, and the UI will display the label "Reason For Update" alongside an error message.


6. UML Class Diagram

+-----------------------------------+
|           CreateViewModel         |
| - (properties for new entity)     |
+-----------------------------------+
               β–²
               |
+-----------------------------------+
|           EditViewModel           |  Β«abstractΒ»
+-----------------------------------+
| - Guid Id                         |
| - string Reason                   |
+-----------------------------------+

7. Sample Code Blocks

7.1 Defining a Concrete Edit Model

public class PatientEditViewModel : EditViewModel
{
    // Inherits: Id, Reason, and all CreateViewModel props (e.g., Name, DOB)
    [Display(Name = "Patient Email")]
    [EmailAddress]
    public string Email { get; set; }
}

7.2 Controller Action (ASP.NET MVC)

[HttpGet]
public IActionResult Edit(Guid id)
{
    // Fetch existing entity from database
    var patient = _patientService.GetById(id);
    var vm = new PatientEditViewModel
    {
        Id = patient.Id,
        Name = patient.Name,
        DOB = patient.DOB,
        Email = patient.Email
    };
    return View(vm);
}

[HttpPost]
public IActionResult Edit(PatientEditViewModel model)
{
    if (!ModelState.IsValid)
        return View(model);

    // Log Reason for update
    _auditService.RecordUpdate(model.Id, model.Reason);

    // Map & persist changes
    _patientService.Update(new Patient {
        Id = model.Id,
        Name = model.Name,
        DOB = model.DOB,
        Email = model.Email
    });

    return RedirectToAction("Details", new { id = model.Id });
}

8. Usage Examples

8.1 Razor View Snippet

@model PatientEditViewModel

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

    <div class="form-group">
        <label asp-for="Name"></label>
        <input asp-for="Name" class="form-control" />
    </div>
    <!-- ... other CreateViewModel fields ... -->

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

    <button type="submit" class="btn btn-primary">Save Changes πŸ’Ύ</button>
</form>

9. Error Handling & Return Types

  • ModelState Validation

    • If Reason (or any inherited required field) is missing, ModelState.IsValid will be false.
    • UI will display appropriate error messages via <span asp-validation-for>.
  • Exceptions

    • Business logic and data access exceptions should be caught in the service/controller layer, not within the ViewModel.

10. API Endpoints Overview 🚫

Note:
This class is purely a ViewModel and does not define API endpoints. It is used by MVC controllers or Razor Pages.


11. Third-Party Integrations πŸ”Œ

  • DataAnnotations
    • Validation attributes are provided by System.ComponentModel.DataAnnotations.
  • No other third-party libraries are directly referenced in this file.

12. Conclusion

🎯 EditViewModel provides a clean, reusable foundation for edit forms across the IrbyImaging application. By inheriting from CreateViewModel and adding only the Id and Reason properties, it encourages:

  • Consistency in edit workflows
  • Mandatory audit trails via the Reason field
  • Separation of concerns between creation and update use cases

Feel free to extend EditViewModel for any domain-specific entities to streamline your update logic and maintain a standardized user experience. πŸš€