Inheritance adalah salah satu pilar utama dalam pemrograman berorientasi objek (OOP). Dalam ASP.NET MVC, inheritance memungkinkan Anda untuk membuat kode yang reusable dan lebih mudah dikelola. Artikel ini akan membahas penerapan inheritance dengan contoh kasus sistem Reservation.
Apa itu Inheritance?
Inheritance
memungkinkan Anda untuk membuat kelas baru yang mewarisi properti dan metode
dari kelas yang sudah ada. Kelas yang mewarisi disebut child class atau derived
class, sedangkan kelas yang diwarisi disebut parent class atau base
class.
Dengan
inheritance, kita dapat menghindari pengulangan kode (code duplication) dan
mempermudah pengelolaan proyek.
Struktur Dasar
Inheritance
public class BaseClass
{
public int Id { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
}
public class Reservation : BaseClass
{
public string CustomerName { get; set; }
public DateTime ReservationDate { get; set; }
public string ReservationType { get; set; }
}
Pada contoh di
atas:
- BaseClass memiliki properti umum
yang dapat digunakan oleh kelas lain.
- Reservation mewarisi properti dari
BaseClass dan menambahkan properti spesifik terkait reservasi.
Implementasi
di ASP.NET MVC
1. Membuat
Base Model
Buatlah kelas
BaseModel yang akan digunakan untuk properti umum di semua model.
public class BaseModel
{
public int Id { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public string UpdatedBy { get; set; }
public DateTime? UpdatedDate { get; set; }
}
2. Membuat
Model Reservation
Buat kelas
Reservation yang mewarisi BaseModel.
public class Reservation : BaseModel
{
public string CustomerName { get; set; }
public DateTime ReservationDate { get; set; }
public string ReservationType { get; set; }
public int NumberOfGuests { get; set; }
}
3. Membuat
Controller
Tambahkan
controller untuk mengelola reservasi.
public class ReservationController : Controller
{
private static List _reservations = new List();
public ActionResult Index()
{
return View(_reservations);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Reservation model)
{
if (ModelState.IsValid)
{
model.Id = _reservations.Count + 1;
model.CreatedBy = User.Identity.Name;
model.CreatedDate = DateTime.Now;
_reservations.Add(model);
return RedirectToAction("Index");
}
return View(model);
}
}
4. Membuat
View
View Index
Tampilkan daftar
reservasi.
@model List<YourNamespace.Models.Reservation>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Customer Name</th>
<th>Reservation Date</th>
<th>Type</th>
<th>Created By</th>
<th>Created Date</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.CustomerName</td>
<td>@item.ReservationDate.ToShortDateString()</td>
<td>@item.ReservationType</td>
<td>@item.CreatedBy</td>
<td>@item.CreatedDate.ToShortDateString()</td>
</tr>
}
</tbody>
</table>
View Create
Form untuk
menambahkan reservasi baru.
@model YourNamespace.Models.Reservation
<h2>Create Reservation</h2>
@using (Html.BeginForm())
{
<div class="form-group">
@Html.LabelFor(m => m.CustomerName)
@Html.TextBoxFor(m => m.CustomerName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.ReservationDate)
@Html.TextBoxFor(m => m.ReservationDate, new { @class = "form-control", type = "date" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.ReservationType)
@Html.TextBoxFor(m => m.ReservationType, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.NumberOfGuests)
@Html.TextBoxFor(m => m.NumberOfGuests, new { @class = "form-control", type = "number" })
</div>
<button type="submit" class="btn btn-primary">Submit</button>
}
5. Menjalankan
Aplikasi
Jalankan aplikasi
Anda dan tambahkan data reservasi melalui form. Data akan ditampilkan di
halaman Index dengan properti yang diwarisi dari BaseModel.
Kesimpulan
ConversionConversion EmoticonEmoticon