📄 Documentation: ComputerListViewModel (IrbyImaging/Models/Computer/ComputerListViewModel.cs)
Welcome to the comprehensive guide for the ComputerListViewModel class! This document covers everything you need to know about the file’s purpose, structure, usage, and integration points.
📑 Table of Contents
- Overview & Purpose
- Namespace & Dependencies
- Key Functional Areas
- UML Class Diagram
- View Models & Data Models
- API Endpoints Overview
- Sample Code Blocks
- Usage Examples
- Error Handling & Return Types
- Third‑Party Integrations
- Conclusion
🧐 Overview & Purpose
The ComputerListViewModel is a Presentation Layer ViewModel used in the IrbyImaging UI to:
- Aggregate a paginated list of computers.
- Provide warranty status summaries (out‐of‐warranty and nearing expiration).
- Serve as the model for list views in ASP.NET MVC/Razor or any front‐end consuming JSON.
It inherits from a base ListViewModel, gaining common pagination fields (e.g., Page, PageSize, TotalCount), and extends it with computer‐specific properties.
📦 Namespace & Dependencies
using System.Collections.Generic;
namespace IrbyImaging.Models
{
// ...
}
- Namespace:
IrbyImaging.Models - Dependencies:
System.Collections.GenericforICollection<T>
No external NuGet packages are required for this file alone.
🔑 Key Functional Areas
| Feature | Description |
|---|---|
| Inheritance | Extends ListViewModel for pagination support |
| NumberOutOfWarranty | Tracks how many computers are already out of warranty |
| NumberApproachingEndOfWarranty | Tracks computers nearing warranty expiration |
| Computers | Collection of detailed computer records (ComputerDetailViewModel) |
🗺 UML Class Diagram
Below is a Mermaid diagram representing the relationships:
classDiagram
IrbyImaging.Models.ListViewModel <|-- IrbyImaging.Models.ComputerListViewModel
class ComputerListViewModel {
+string NumberOutOfWarranty
+string NumberApproachingEndOfWarranty
+ICollection<ComputerDetailViewModel> Computers
}
class ListViewModel {
<<abstract>>
+int Page
+int PageSize
+int TotalCount
// ... other pagination members
}
class ComputerDetailViewModel {
// Properties for individual computer details
}
📂 View Models & Data Models
ComputerListViewModel
public class ComputerListViewModel : ListViewModel
{
public string NumberOutOfWarranty { get; set; }
public string NumberApproachingEndOfWarranty { get; set; }
public ICollection<ComputerDetailViewModel> Computers { get; set; }
}
-
ListViewModel (Base Class)
- Common properties:
int Pageint PageSizeint TotalCount
- Common properties:
-
ComputerDetailViewModel
- Represents an individual computer record (e.g., Serial Number, Model, Warranty Dates).
📡 API Endpoints Overview
While this file itself doesn’t define endpoints, it is typically returned by these backend controllers/services:
| HTTP Method | Endpoint | Description |
|---|---|---|
| GET | /api/computers |
Returns a ComputerListViewModel in JSON. |
| GET | /computers/list (MVC View) |
Renders a Razor view with this ViewModel. |
// Example Controller Action (ASP.NET Core MVC)
public IActionResult Index(int page = 1, int pageSize = 20)
{
var result = _computerService.GetPagedComputers(page, pageSize);
var vm = new ComputerListViewModel {
Page = page,
PageSize = pageSize,
TotalCount = result.TotalCount,
NumberOutOfWarranty = result.Stats.OutOfWarranty.ToString(),
NumberApproachingEndOfWarranty = result.Stats.ApproachingEnd.ToString(),
Computers = result.Items.Select(c => new ComputerDetailViewModel(c)).ToList()
};
return View(vm);
}
🛠 Sample Code Blocks
1. Instantiating in C# Service Layer
var pagedResult = computerRepo.GetComputers(page, pageSize);
var viewModel = new ComputerListViewModel
{
Page = page,
PageSize = pageSize,
TotalCount = pagedResult.TotalCount,
NumberOutOfWarranty = pagedResult.Statistics.OutOfWarrantyCount.ToString(),
NumberApproachingEndOfWarranty = pagedResult.Statistics.NearExpiryCount.ToString(),
Computers = pagedResult.Items
.Select(c => new ComputerDetailViewModel(c))
.ToList()
};
2. Rendering in Razor View
@model IrbyImaging.Models.ComputerListViewModel
<h2>📋 Computer Inventory</h2>
<p>
🔴 Out of Warranty: @Model.NumberOutOfWarranty<br/>
🟠 Approaching End of Warranty: @Model.NumberApproachingEndOfWarranty
</p>
<table class="table">
<thead>
<tr>
<th>Serial #</th>
<th>Model</th>
<th>Purchase Date</th>
<th>Warranty Expiry</th>
</tr>
</thead>
<tbody>
@foreach(var comp in Model.Computers)
{
<tr>
<td>@comp.SerialNumber</td>
<td>@comp.ModelName</td>
<td>@comp.PurchaseDate.ToShortDateString()</td>
<td>@comp.WarrantyExpiry.ToShortDateString()</td>
</tr>
}
</tbody>
</table>
📝 Usage Examples
- JSON Response (for SPA/Angular/React):
{ "page": 1, "pageSize": 20, "totalCount": 150, "numberOutOfWarranty": "12", "numberApproachingEndOfWarranty": "5", "computers": [ { "serialNumber": "ABC123", "modelName": "Dell XPS 13", "purchaseDate": "2020-02-15T00:00:00", "warrantyExpiry": "2023-02-15T00:00:00" }, // ... ] } - Client‑Side Rendering:
fetch('/api/computers?page=2&pageSize=10') .then(res => res.json()) .then(data => { console.log('Out-of-Warranty:', data.numberOutOfWarranty); // Render table rows dynamically... });
⚠️ Error Handling & Return Types
-
Error Handling:
- Handled at the service/controller level; this ViewModel assumes valid data.
- If no computers found,
Computersshould be an empty collection (Count = 0).
-
Return Types:
- MVC: Returns
IActionResult(View) withComputerListViewModel. - API: Returns
ActionResult<ComputerListViewModel>or raw JSON.
- MVC: Returns
🔗 Third‑Party Integrations
None directly within this file. However:
- Newtonsoft.Json or System.Text.Json may be used for serialization.
- UI frameworks (e.g., Bootstrap) can style tables built from this ViewModel.
🏁 Conclusion
The ComputerListViewModel is a straightforward, extensible ViewModel that:
- Harnesses base pagination logic (
ListViewModel). - Provides clear, user‐friendly summary metrics on warranty status.
- Exposes a detailed list of computers for UI rendering or API consumption.
🚀 Next Steps:
- Ensure
ComputerDetailViewModelaccurately reflects your domain fields.- Hook up localization for headings and labels if needed.
- Consider converting count properties to numeric types (
int) if aggregation or sorting is required client‐side.
Feel free to integrate this into your controllers, services, and front‐end to build a robust computer inventory dashboard!