Memahami Konsep Dasar OOP dalam ASP.NET MVC C#
Pendahuluan Pemrograman Berorientasi Objek
(Object-Oriented Programming atau OOP) adalah paradigma pemrograman yang
menggunakan "objek" untuk mewakili data dan metode. Dalam ASP.NET
MVC, konsep OOP membantu mengorganisasi kode dengan lebih baik, mempermudah
pemeliharaan, dan meningkatkan skalabilitas aplikasi.
Artikel ini akan menjelaskan konsep dasar OOP dalam konteks
ASP.NET MVC C#, yaitu Encapsulation, Inheritance, Polymorphism, dan
Abstraction, serta bagaimana mengimplementasikannya.
1. Encapsulation dalam ASP.NET MVC
Encapsulation adalah proses menyembunyikan detail
implementasi dan hanya mengekspos apa yang diperlukan. Dalam ASP.NET MVC, ini
biasanya diterapkan melalui model.
Contoh:
Model - Product.cs
public class Product
{
// Properti dengan akses kontrol
public int Id { get; set; }
public string Name { get; set; }
private decimal price;
// Method untuk mengatur harga
public void SetPrice(decimal newPrice)
{
if (newPrice > 0)
{
price = newPrice;
}
}
public decimal GetPrice()
{
return price;
}
}
View - Details.cshtml
@model Product
@{
ViewData["Title"] = "Product List";
}
<h2>@ViewData["Title"]</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td> @Model.Id</td>
<td> @Model.Name</td>
<td> @Model.GetPrice()</td>
</tr>
</tbody>
</table>
Penjelasan:
- Properti
price dibuat private untuk menjaga integritas data.
- Metode
SetPrice dan GetPrice digunakan untuk mengontrol akses ke price.
Implementasi di Controller:
Controller - ProductController.cs
public class ProductController : Controller
{
public ActionResult Details()
{
var product = new Product
{
Id = 1,
Name = "Laptop"
};
product.SetPrice(1500);
ViewBag.Price = product.GetPrice();
return View(product);
}
}
2. Inheritance dalam ASP.NET
MVC
Inheritance memungkinkan satu
class mewarisi properti dan metode dari class lain. Ini berguna untuk membuat
hierarki class dan mengurangi duplikasi kode.
Contoh:
public class Person
{
public string Name { get; set; }
public string Email { get; set; }
}
public class Employee : Person
{
public string Position { get; set; }
public decimal Salary { get; set; }
}
Penjelasan:
- Employee mewarisi properti Name dan Email dari Person.
Implementasi di Controller:
public class EmployeeController : Controller
{
public ActionResult Index()
{
var employee = new Employee
{
Name = "John Doe",
Email = "john.doe@example.com",
Position = "Software Engineer",
Salary = 5000
};
return View(employee);
}
}
3. Polymorphism dalam ASP.NET MVC
Polymorphism memungkinkan objek untuk berperilaku berbeda
tergantung pada konteksnya. Dalam C#, ini dapat diterapkan melalui overriding
dan interface.
Contoh:
public class Notification
{
public virtual string GetMessage()
{
return "This is a notification.";
}
}
public class EmailNotification : Notification
{
public override string GetMessage()
{
return "This is an email notification.";
}
}
Penjelasan:
- Metode GetMessage di Notification dapat di-override
oleh EmailNotification.
Implementasi di Controller:
public class NotificationController : Controller
{
public ActionResult ShowNotification()
{
Notification notification = new EmailNotification();
ViewBag.Message = notification.GetMessage();
return View();
}
}
4. Abstraction dalam ASP.NET
MVCAbstraction adalah proses
menyembunyikan detail implementasi dan hanya menampilkan fungsionalitas
penting. Dalam ASP.NET MVC, abstraction sering digunakan melalui interface atau
abstract class.
Contoh:
public interface IReport
{
string GenerateReport();
}
public class PdfReport : IReport
{
public string GenerateReport()
{
return "PDF Report Generated.";
}
}
public class ExcelReport : IReport
{
public string GenerateReport()
{
return "Excel Report Generated.";
}
}
Penjelasan:
- Interface IReport mendefinisikan kontrak untuk
pembuatan laporan.
- PdfReport dan ExcelReport mengimplementasikan
kontrak tersebut.
Implementasi di Controller:
public class ReportController : Controller
{
public ActionResult Generate(string reportType)
{
IReport report;
if (reportType == "pdf")
{
report = new PdfReport();
}
else
{
report = new ExcelReport();
}
ViewBag.ReportMessage = report.GenerateReport();
return View();
}
}
Kesimpulan Konsep dasar OOP
seperti Encapsulation, Inheritance, Polymorphism, dan Abstraction adalah
fondasi yang kuat dalam pengembangan aplikasi ASP.NET MVC. Dengan memahami dan
mengimplementasikan konsep ini, Anda dapat membuat aplikasi yang lebih
terstruktur, mudah dikelola, dan scalable.
Selamat mencoba!
ConversionConversion EmoticonEmoticon