📦 DockCreateViewModel.cs Documentation

File Path: IrbyImaging/Models/Dock/DockCreateViewModel.cs
Assembly: IrbyImaging.UI
Author: kvamshi
Created: 03-31-2022
Last Modified: 03-31-2022 (kvamshi)


📑 Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
  3. Dependencies & Technologies
  4. Class Structure & UML Diagram
  5. Inheritance: CreateViewModel
  6. Usage Scenarios
  7. Error Handling & Validation
  8. Third‑Party Integrations
  9. Sample Code Blocks & Snippets
  10. Conclusion

🎯 Overview & Purpose

DockCreateViewModel is a ViewModel class in the IrbyImaging UI layer. It inherits from a generic CreateViewModel and represents the data structure used when creating a new “Dock” entity in the application’s user interface.

  • Acts as a strongly‑typed “Create” model in ASP.NET MVC or Web API.
  • Encapsulates all fields required to render and validate the Create Dock view.
  • Provides a clean separation between the UI and domain/data layers.

🔑 Key Functional Areas

  • Marker Type: No additional properties beyond base class → signals a specialized “Dock” create operation.
  • Validation: Leverages data annotations (inherited) for field-level validation.
  • UI Binding: Binds to Razor views or front‑end frameworks (React/Angular) via model binding.
  • Consistency: Ensures naming conventions and common properties are shared across “Create” operations for different entities.

🧰 Dependencies & Technologies

Layer Technology / Package
UI / ViewModel ASP.NET MVC / ASP.NET Core MVC
Language C# (.NET 5.0 or later)
Validation System.ComponentModel.DataAnnotations
Version Control Git
Build MSBuild / Visual Studio

No external NuGet packages beyond the standard ASP.NET and .NET SDK are required.


🏗 Class Structure & UML Diagram

// DockCreateViewModel.cs
namespace IrbyImaging.Models
{
    /// <summary>
    /// Inherits common "create" properties from CreateViewModel.
    /// Used to create a new Dock.
    /// </summary>
    public class DockCreateViewModel : CreateViewModel
    {
        // No additional members; all inherited
    }
}
@startuml
package IrbyImaging.Models {
    class CreateViewModel {
        +int Id
        +string Name
        +DateTime CreatedOn
        ... // other common Create properties
    }

    class DockCreateViewModel {
    }

    CreateViewModel <|-- DockCreateViewModel
}
@enduml

🔗 Inheritance: CreateViewModel

CreateViewModel (base class) typically includes:

  • Common Fields: Name, Code, Description, CreatedOn, etc.
  • Validation Attributes: [Required], [StringLength], etc.
  • Utility Members: May contain methods or properties shared by all “Create” VMs.

Note: Inspect CreateViewModel in IrbyImaging/Models/CreateViewModel.cs for full details.


🚀 Usage Scenarios

6.1 ASP.NET MVC Controller Example

public class DockController : Controller
{
    // GET: /Dock/Create
    public IActionResult Create()
    {
        // Instantiate empty ViewModel
        var vm = new DockCreateViewModel();
        return View(vm);
    }

    // POST: /Dock/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Create(DockCreateViewModel model)
    {
        if (!ModelState.IsValid)
            return View(model);

        // Map ViewModel to Domain or DTO
        var dockDto = new DockDto {
            Name = model.Name,
            Description = model.Description,
            // ... other fields
        };

        // Save via service layer
        _dockService.Create(dockDto);

        return RedirectToAction("Index");
    }
}

6.2 Razor View Example

@model IrbyImaging.Models.DockCreateViewModel

<h2>Create New Dock</h2>
<form asp-action="Create" method="post">
    <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>
    <!-- Additional inherited fields -->
    <button type="submit" class="btn btn-primary">Save 🚢</button>
</form>

6.3 Web API Endpoint Example

[ApiController]
[Route("api/docks")]
public class DockApiController : ControllerBase
{
    [HttpPost]
    public ActionResult<DockReadDto> Create([FromBody] DockCreateViewModel model)
    {
        if (!ModelState.IsValid)
            return BadRequest(ModelState);

        var created = _dockService.Create(MapToDto(model));
        return CreatedAtAction(nameof(GetById), new { id = created.Id }, created);
    }

    // GET api/docks/{id}
    [HttpGet("{id}")]
    public ActionResult<DockReadDto> GetById(int id) { ... }
}

⚠️ Error Handling & Validation

  • ModelState: Leveraged in MVC/Web API to capture validation errors.
  • Data Annotations: Inherited attributes enforce required fields, string lengths, and custom validation logic.
  • HTTP Return Types:
    • MVC: View(model) on failure, RedirectToAction on success.
    • Web API: BadRequest(ModelState) on validation failure, CreatedAtAction on success.

🔧 Third‑Party Integrations

This ViewModel itself has no direct third‑party dependencies. Integration may occur at:

  • Service Layer: e.g., Automapper for mapping between VM and DTO/domain objects.
  • Client‑Side Validation: jQuery Validation, Bootstrap for UI feedback.

💡 Sample Code Blocks & Snippets

Mapping with AutoMapper

// AutoMapper Profile
public class DockProfile : Profile
{
    public DockProfile()
    {
        CreateMap<DockCreateViewModel, Dock>();
        CreateMap<Dock, DockReadDto>();
    }
}

FluentValidation Alternative

public class DockCreateViewModelValidator : AbstractValidator<DockCreateViewModel>
{
    public DockCreateViewModelValidator()
    {
        RuleFor(x => x.Name)
            .NotEmpty().WithMessage("Dock name is required")
            .MaximumLength(100);
    }
}

🏁 Conclusion

DockCreateViewModel is a concise, purpose‑driven class that:

  • Inherits common creation logic from CreateViewModel.
  • Acts as a binding model for UI and API layers when adding a new Dock.
  • Enables consistency and reuse across multiple create‑operation view models.

By following standard ASP.NET MVC/Web API patterns, it ensures that:

  • Validation is centralized (via data annotations or FluentValidation).
  • Controller actions can remain thin, focusing on orchestration rather than field‑level logic.
  • UI developers enjoy strong typing, IntelliSense, and built‑in validation feedback.

Happy coding! 🚀