🖨️ PrinterEditViewModel Documentation


📋 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Class Definition
  5. UML Class Diagram
  6. Usage Examples
  7. Error Handling & Return Types
  8. Third‑party Integrations
  9. Conclusion

1. Overview & Purpose

File: IrbyImaging/Models/Printer/PrinterEditViewModel.cs
Assembly: IrbyImaging.UI
Author: kvamshi
Created On: 03‑31‑2022

The PrinterEditViewModel is a ViewModel used in the IrbyImaging UI layer. It encapsulates the data required to edit an existing printer entity. It inherits from the generic EditViewModel base class, which provides common properties such as Id, audit fields (CreatedBy, CreatedDate, etc.), and validation logic.

Key Goals:

  • Provide a strongly‑typed model for the Edit Printer screen.
  • Leverage shared validation and audit features by inheriting from EditViewModel.
  • Simplify data binding in ASP.NET MVC/Razor pages with minimal boilerplate.

2. Dependencies & Technologies

Category Details
Platform .NET 6.0+
Language C# (8.0+)
UI Framework ASP.NET Core MVC / Razor Pages
NuGet Packages — (inherits from internal IrbyImaging.Models)
Source Control Git

3. Key Functional Areas

  1. Inheritance
    • Gains standard editing properties from EditViewModel.
  2. Validation
    • Relies on data annotations defined in the base class.
  3. Binding
    • Used in Razor pages for two‑way data binding on edit forms.

4. Class Definition

// IrbyImaging/Models/Printer/PrinterEditViewModel.cs
namespace IrbyImaging.Models
{
    /// <summary>
    /// ViewModel for editing Printer entities.
    /// Inherits shared properties from EditViewModel.
    /// </summary>
    public class PrinterEditViewModel : EditViewModel
    {
        // No additional properties.
        // All properties (Id, Name, etc.) come from EditViewModel.
    }
}

4.1. Inheritance Hierarchy

classDiagram
    EditViewModel <|-- PrinterEditViewModel
    class EditViewModel {
        +Guid Id
        +string Name
        +DateTime CreatedDate
        +string CreatedBy
        +DateTime? ModifiedDate
        +string ModifiedBy
        +Validate()
    }
    class PrinterEditViewModel {
        <<ViewModel>>
    }

4.2. Properties (Inherited)

Property Type Description
Id Guid Unique identifier of the printer.
Name string Friendly name of the printer.
CreatedDate DateTime When the printer record was created.
CreatedBy string User who created the record.
ModifiedDate DateTime? When the printer record was last updated.
ModifiedBy string User who last updated the record.

Note: Actual property names in EditViewModel may vary. Please refer to EditViewModel.cs for complete definitions.


5. UML Class Diagram

classDiagram
    %% Base class with audit fields
    class EditViewModel {
        +Guid Id
        +string Name
        +DateTime CreatedDate
        +string CreatedBy
        +DateTime? ModifiedDate
        +string ModifiedBy
        +bool Validate()
    }

    %% Derived class: PrinterEditViewModel
    class PrinterEditViewModel {
        <<ViewModel>>
        // Inherits everything from EditViewModel
    }

    EditViewModel <|-- PrinterEditViewModel

6. Usage Examples

Below are typical scenarios where PrinterEditViewModel is employed.

6.1. In a Controller

public class PrinterController : Controller
{
    private readonly IPrinterService _printerService;

    public PrinterController(IPrinterService printerService)
    {
        _printerService = printerService;
    }

    // GET: /Printer/Edit/{id}
    public async Task<IActionResult> Edit(Guid id)
    {
        var dto = await _printerService.GetByIdAsync(id);
        if (dto == null) return NotFound();

        // Map DTO to ViewModel
        var vm = new PrinterEditViewModel
        {
            Id            = dto.Id,
            Name          = dto.Name,
            CreatedDate   = dto.CreatedDate,
            CreatedBy     = dto.CreatedBy,
            ModifiedDate  = dto.ModifiedDate,
            ModifiedBy    = dto.ModifiedBy
        };

        return View(vm);
    }

    // POST: /Printer/Edit
    [HttpPost]
    public async Task<IActionResult> Edit(PrinterEditViewModel vm)
    {
        if (!ModelState.IsValid)
            return View(vm);

        // Map back to DTO or domain model
        var dto = new PrinterDto
        {
            Id   = vm.Id,
            Name = vm.Name,
            // audit fields handled server-side
        };

        await _printerService.UpdateAsync(dto);
        return RedirectToAction("Index");
    }
}

6.2. In a Razor View

@model IrbyImaging.Models.PrinterEditViewModel

<h2>Edit Printer 🖨️</h2>
<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" />
        <span asp-validation-for="Name" class="text-danger"></span>
    </div>
    <!-- Audit fields (read-only) -->
    <div>
        <small>Created on @Model.CreatedDate by @Model.CreatedBy</small><br/>
        <small>Last modified on @Model.ModifiedDate by @Model.ModifiedBy</small>
    </div>
    <button type="submit" class="btn btn-primary">Save</button>
</form>

7. Error Handling & Return Types

  • ModelState Validation

    • Uses ASP.NET Core’s built‑in data annotations from EditViewModel.
    • Errors are surfaced to the user via asp-validation-for.
  • Controller Returns

    • View(vm) for validation errors or initial GET.
    • RedirectToAction("Index") on successful update.
    • NotFound() if the target printer does not exist.

8. Third‑party Integrations

This ViewModel itself does not integrate directly with third‑party libraries. It relies on:

  • AutoMapper (optional) for mapping between DTOs and ViewModels.
  • FluentValidation (optional) if you enhance validation beyond data annotations.

9. Conclusion

The PrinterEditViewModel is a thin, focused ViewModel designed to power the Edit Printer UI in the IrbyImaging application. By inheriting from a shared EditViewModel, it embraces DRY principles—reusing audit and validation logic while keeping the codebase clean and maintainable. Integrate this ViewModel in your controllers and Razor views to provide a seamless editing experience for your end users. 🛠️


Happy Coding! 🚀