許多 MVC 教學預設使用 Linq to SQL 或 Entity Framework,但實務上 ADO.NET、Dapper、原生 SqlClient 等同樣適用。本文示範如何在 MVC 專案中整合經典 N 層架構(BO/BLL/DAL),採用 ADO.NET 實作完整 CRUD 功能,展現資料存取技術的可替換性。
參考 Imar Spaanjaars 的 N 層架構系列:

Web.config,DAL 讀取設定。public class ContactController : Controller
{
private readonly IContactManager _contactMgr = new ContactManager(); // BLL 介面
public ActionResult Index()
{
var contacts = _contactMgr.GetAllContacts();
return View(contacts);
}
[HttpPost]
public ActionResult Create(ContactPerson contact)
{
if (ModelState.IsValid)
{
_contactMgr.AddContact(contact);
return RedirectToAction("Index");
}
return View(contact);
}
// Edit、Delete 同理...
}

MVC 是表現層架構,不綁定特定 ORM。採用 N 層 + 介面導向設計,讓資料存取技術成為可換拔的實作細節,才是企業級應用的長久之計。