ASP.NET Core  

Building a Web-Based Hospital Management System in ASP.NET Core MVC – Part 2

✨ Introduction

In Part 1, we explored the architecture, database schema, and Razor screens for the HospitalManagement system.

In this second part, we’ll dive into the Controllers and Services. These layers handle business logic, data persistence, and communication between Razor views and the database.

⚙️ Controllers Overview

Controllers are responsible for:

  • Handling HTTP requests (GET, POST).

  • Calling the appropriate service methods.

  • Returning data to Razor views.

Each module has its own controller:

  • PatientController

  • StaffController

  • FinanceController

  • NotificationController

  • ChatController

👨‍⚕️ PatientController

csharp

public class PatientController : Controller
{
    private readonly IPatientService _patientService;

    public PatientController(IPatientService patientService)
    {
        _patientService = patientService;
    }

    public IActionResult Index()
    {
        var patients = _patientService.GetAll();
        return View(patients);
    }

    [HttpPost]
    public IActionResult Create(Patient patient)
    {
        _patientService.Add(patient);
        return RedirectToAction("Index");
    }
}

🔎 Explanation

  • Index() → Fetches all patients from the service and passes them to the Razor view.

  • Create() → Adds a new patient record via the service.

👩‍⚕️ StaffController

csharp

public class StaffController : Controller
{
    private readonly IStaffService _staffService;

    public StaffController(IStaffService staffService)
    {
        _staffService = staffService;
    }

    public IActionResult Index()
    {
        var staff = _staffService.GetAll();
        return View(staff);
    }

    [HttpPost]
    public IActionResult Create(Staff staff)
    {
        _staffService.Add(staff);
        return RedirectToAction("Index");
    }
}

💳 FinanceController

csharp

public class FinanceController : Controller
{
    private readonly IFinanceService _financeService;

    public FinanceController(IFinanceService financeService)
    {
        _financeService = financeService;
    }

    public IActionResult Index()
    {
        var invoices = _financeService.GetAll();
        return View(invoices);
    }

    [HttpPost]
    public IActionResult Create(Invoice invoice)
    {
        _financeService.Add(invoice);
        return RedirectToAction("Index");
    }
}

🔔 NotificationController

csharp

public class NotificationController : Controller
{
    private readonly INotificationService _notificationService;

    public NotificationController(INotificationService notificationService)
    {
        _notificationService = notificationService;
    }

    public IActionResult Index()
    {
        var notifications = _notificationService.GetAll();
        return View(notifications);
    }

    [HttpPost]
    public IActionResult Create(Notification notification)
    {
        _notificationService.Add(notification);
        return RedirectToAction("Index");
    }
}

⚙️ Services Overview

Services encapsulate business logic and interact with the database context. Each module has its own service interface and implementation.

PatientService

csharp

public class PatientService : IPatientService
{
    private readonly ApplicationDbContext _context;

    public PatientService(ApplicationDbContext context)
    {
        _context = context;
    }

    public IEnumerable<Patient> GetAll()
    {
        return _context.Patients.ToList();
    }

    public void Add(Patient patient)
    {
        _context.Patients.Add(patient);
        _context.SaveChanges();
    }
}

StaffService

csharp

public class StaffService : IStaffService
{
    private readonly ApplicationDbContext _context;

    public StaffService(ApplicationDbContext context)
    {
        _context = context;
    }

    public IEnumerable<Staff> GetAll()
    {
        return _context.Staff.ToList();
    }

    public void Add(Staff staff)
    {
        _context.Staff.Add(staff);
        _context.SaveChanges();
    }
}

FinanceService

csharp

public class FinanceService : IFinanceService
{
    private readonly ApplicationDbContext _context;

    public FinanceService(ApplicationDbContext context)
    {
        _context = context;
    }

    public IEnumerable<Invoice> GetAll()
    {
        return _context.Invoices.ToList();
    }

    public void Add(Invoice invoice)
    {
        _context.Invoices.Add(invoice);
        _context.SaveChanges();
    }
}

NotificationService

csharp

public class NotificationService : INotificationService
{
    private readonly ApplicationDbContext _context;

    public NotificationService(ApplicationDbContext context)
    {
        _context = context;
    }

    public IEnumerable<Notification> GetAll()
    {
        return _context.Notifications.ToList();
    }

    public void Add(Notification notification)
    {
        _context.Notifications.Add(notification);
        _context.SaveChanges();
    }
}

📌 Conclusion (Part 2)

In this second part, we covered:

  • Controllers for Patients, Staff, Finance, and Notifications.

  • Services that encapsulate business logic and database operations.

  • How data flows from Razor views → Controllers → Services → Database.

This completes the backend logic of the HospitalManagement system.