OutstandingAssetMapper Service Documentation πŸ“¦πŸ”„


Table of Contents πŸ“š

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Data Models & View Models
  5. Interface: IOutstandingAssetMapper
  6. Implementation: OutstandingAssetMapper
  7. Method Details
  8. UML Class Diagram
  9. Sample Usage & Code Examples
  10. Error Handling & Return Types
  11. 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 domain Employee + Asset entities into UI OutstandingViewModel.
  • Flattening
    Concatenate and format nested properties (e.g., Make.Name + Model.Name).
  • Collection Transformation
    Accept an IEnumerable<Employee> and produce an IList<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 Employee and nested Asset.
  • Constructs MakeAndModel by combining Make.Name & Model.Name.
  • Appends each mapped OutstandingViewModel to 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

  1. Initialize List<OutstandingViewModel>.
  2. Loop through each Employee.
  3. Loop through each Asset in Employee.Assets.
  4. Map properties from Asset β†’ OutstandingViewModel.
  5. Add to result list.
  6. 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 null for employees or employee.Assets.
    • Recommendation: Validate inputs and throw ArgumentNullException if necessary.
  • Empty Collections
    • Returns an empty List<OutstandingViewModel> if no employees or no assets.

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! πŸš€