OutstandingAssetMapper Service Documentation π¦π
Table of Contents π
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Data Models & View Models
- Interface:
IOutstandingAssetMapper - Implementation:
OutstandingAssetMapper - Method Details
- UML Class Diagram
- Sample Usage & Code Examples
- Error Handling & Return Types
- Conclusion
1. Overview & Purpose βΉοΈ
The OutstandingAssetMapper service is part of the IrbyImaging.UI assembly. Its primary role is to transform a collection of terminated employees (from the domain layer) into a list of OutstandingViewModel instances. This mapping is useful when you want to display, report on, or process assets still assigned to employees who have left the organization.
Why?
When an employee is terminated, any assets they still possess (laptops, phones, etc.) become outstanding assets. We need a clean view model representation to feed into UI components, reports, or export routines.
2. Dependencies & Technologies π
| Layer / Tech | Details |
|---|---|
| .NET / C# | Target framework: .NET Standard / .NET Core |
| Dependency Injection | Register via IServiceCollection (Scoped) |
| Project Assemblies | - IrbyImaging.BusinessLogic- IrbyImaging.DomainClasses- IrbyImaging.Models- IrbyImaging.Utility |
| Tools | Visual Studio, Rider, VS Code |
3. Key Functional Areas π
- Mapping
Convert domainEmployee+Assetentities into UIOutstandingViewModel. - Flattening
Concatenate and format nested properties (e.g.,Make.Name + Model.Name). - Collection Transformation
Accept anIEnumerable<Employee>and produce anIList<OutstandingViewModel>.
4. Data Models & View Models π―
Domain Models (simplified)
| Class | Key Properties |
|---|---|
| Employee | int EmployeeIdstring FullNameICollection<Asset> Assets |
| Asset | int Idstring CategoryDateTime PurchaseDatestring SerialNumberstring StatusAssetModel Model |
| AssetModel | string NameMake Make |
| Make | string Name |
View Model
| Class | Properties |
|---|---|
| OutstandingViewModel | int Idstring Categorystring MakeAndModelstring OwnerFullNameint OwnerIdDateTime PurchaseDatestring SerialNumberstring Status |
5. Interface: IOutstandingAssetMapper π
public interface IOutstandingAssetMapper
{
/// <summary>
/// Maps terminated employees (with assets) into a list of OutstandingViewModel.
/// </summary>
/// <param name="employees">Collection of terminated employees</param>
/// <returns>List of view models representing outstanding assets</returns>
IList<OutstandingViewModel> TerminatedEmployeesWithAssetsToOutstandingViewModel(IEnumerable<Employee> employees);
}
- Purpose: Defines the contract for any mapper that turns domain employees+assets into UI view models.
- Benefits: Promotes SOLID (Interface Segregation & Dependency Inversion).
6. Implementation: OutstandingAssetMapper π οΈ
public class OutstandingAssetMapper : IOutstandingAssetMapper
{
public IList<OutstandingViewModel> TerminatedEmployeesWithAssetsToOutstandingViewModel(IEnumerable<Employee> employees)
{
var outstandingAssets = new List<OutstandingViewModel>();
foreach (var employee in employees)
{
foreach (var asset in employee.Assets)
{
var outstandingAsset = new OutstandingViewModel
{
Id = asset.Id,
Category = asset.Category,
MakeAndModel = $"{asset.Model.Make.Name} {asset.Model.Name}",
OwnerFullName = employee.FullName,
OwnerId = employee.EmployeeId,
PurchaseDate = asset.PurchaseDate,
SerialNumber = asset.SerialNumber,
Status = asset.Status
};
outstandingAssets.Add(outstandingAsset);
}
}
return outstandingAssets;
}
}
Highlights
- Iterates each
Employeeand nestedAsset. - Constructs
MakeAndModelby combiningMake.Name&Model.Name. - Appends each mapped
OutstandingViewModelto the result list.
7. Method Details π
| Parameter | Type | Description |
|---|---|---|
employees |
IEnumerable<Employee> |
List of terminated employees, each with a collection of assets. |
| Return Type | Description |
|---|---|
IList<OutstandingViewModel> |
A flat list of view models, one per asset. |
Step-by-Step Flow
- Initialize
List<OutstandingViewModel>. - Loop through each
Employee. - Loop through each
AssetinEmployee.Assets. - Map properties from
AssetβOutstandingViewModel. - Add to result list.
- Return the filled list.
8. UML Class Diagram π
classDiagram
IOutstandingAssetMapper <|.. OutstandingAssetMapper
OutstandingAssetMapper : +TerminatedEmployeesWithAssetsToOutstandingViewModel(IEnumerable<Employee>) IList<OutstandingViewModel>
DomainClasses.Employee --> DomainClasses.Asset : has 0..*
DomainClasses.Asset --> DomainClasses.AssetModel : has 1
DomainClasses.AssetModel --> DomainClasses.Make : has 1
OutstandingViewModel : +Id
OutstandingViewModel : +Category
OutstandingViewModel : +MakeAndModel
OutstandingViewModel : +OwnerFullName
OutstandingViewModel : +OwnerId
OutstandingViewModel : +PurchaseDate
OutstandingViewModel : +SerialNumber
OutstandingViewModel : +Status
9. Sample Usage & Code Examples π‘
1. Registering in DI Container
// Startup.cs or Program.cs
services.AddScoped<IOutstandingAssetMapper, OutstandingAssetMapper>();
2. Consuming in a Controller / Service
public class AssetController : Controller
{
private readonly IEmployeeService _employeeService;
private readonly IOutstandingAssetMapper _mapper;
public AssetController(IEmployeeService employeeService, IOutstandingAssetMapper mapper)
{
_employeeService = employeeService;
_mapper = mapper;
}
public IActionResult OutstandingAssets()
{
// 1. Fetch terminated employees with assets
var terminatedEmployees = _employeeService.GetTerminatedEmployeesWithAssets();
// 2. Map to view models
var viewModels = _mapper.TerminatedEmployeesWithAssetsToOutstandingViewModel(terminatedEmployees);
// 3. Pass to view or return as JSON
return View(viewModels);
}
}
3. JSON API Example
[ApiController]
[Route("api/assets")]
public class AssetsApiController : ControllerBase
{
private readonly IEmployeeService _employeeService;
private readonly IOutstandingAssetMapper _mapper;
public AssetsApiController(IEmployeeService employeeService, IOutstandingAssetMapper mapper)
{
_employeeService = employeeService;
_mapper = mapper;
}
[HttpGet("outstanding")]
public ActionResult<IEnumerable<OutstandingViewModel>> GetOutstanding()
{
var employees = _employeeService.GetTerminatedEmployeesWithAssets();
var result = _mapper.TerminatedEmployeesWithAssetsToOutstandingViewModel(employees);
return Ok(result);
}
}
10. Error Handling & Return Types β οΈ
- Null Checks
- Current implementation does not guard against
nullforemployeesoremployee.Assets. - Recommendation: Validate inputs and throw
ArgumentNullExceptionif necessary.
- Current implementation does not guard against
- Empty Collections
- Returns an empty
List<OutstandingViewModel>if no employees or no assets.
- Returns an empty
11. Conclusion π―
The OutstandingAssetMapper is a straightforward, single-responsibility component designed to convert terminated employeesβ assets into a consumable view model for UI and API layers. By adhering to an interface (IOutstandingAssetMapper), it promotes maintainability, testability, and clear separation of concerns.
Next Steps & Enhancements
- Add null and validation checks.
- Support filtering or sorting criteria (e.g., by category, date).
- Incorporate Automapper for more complex scenarios.
Happy coding! π