Aplikasi Web ASP.NET MVC CRUD Menggunakan RepoDB ORM

  • Whatsapp
Aplikasi Web ASP.NET MVC CRUD Menggunakan RepoDB ORM

Selamat Hari Minggu, Programer Luar Biasa!

Seminggu yang lalu, saya menerima buletin tentang ORM yang menjanjikan yang disebut RepoDB. Perpustakaan ORM ini dibuat oleh Michael Pendon yang merupakan arsitek aplikasi di Eropa. Menurut dia, RepoDB adalah open-source. Pustaka NET ORM yang menjembatani celah mikro-ORM dan ORM penuh. Ini membantu Anda menyederhanakan peralihan kapan harus menggunakan operasi BASIC dan ADVANCE selama pengembangan. Ini adalah ORM alternatif terbaik Anda untuk Dapper dan EntityFramework. Ini juga memberikan dukungan query ke beberapa database seperti MySQL, SQL Server, PostGreSQL dan ada survei bahwa kinerja ORM ini lebih cepat daripada Dapper. Saya telah menggunakan Dapper sebelumnya dan kerangka Entitas di aplikasi ASP.NET MVC saya jadi, inilah saatnya untuk mencoba menggunakan alat ini dengan membuat Aplikasi ASP.NET MVC CRUD (Buat, Baca, Perbarui, Hapus) Aplikasi sederhana. Untuk bagian database, buat tabel fiktif di db Anda yang disebut BookDetails.

Bacaan Lainnya

USE [DemoDB]
GO

/****** Object: Table [dbo].[BookDetails] Script Date: 11/2/2020 12:26:53 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[BookDetails](
[BookSerialNo] [int] IDENTITY(1,1) NOT NULL,
[BookISBN] [nchar](15) NULL,
[BookTitle] [varchar](120) NULL,
[BookAuthor] [varchar](60) NULL,
[BookPublisher] [varchar](50) NULL,
[BookCategory] [varchar](20) NULL,
PRIMARY KEY CLUSTERED
(
[BookSerialNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Selanjutnya adalah membuat proyek ASP.NET MVC dan menambahkan melalui NuGet file RepoDb.SqlServer ORM. Pustaka ini secara khusus menargetkan database SQL Server.

ASP.NET MVC CRUD (Membuat, Membaca, Memperbarui, Menghapus) Aplikasi Menggunakan RepoDB ORM

Setelah itu, panggil bootstrapper untuk menginisialisasi semua dependensi untuk SQL Server dalam metode Global.asax.cs Application_Start ().

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
RepoDb.SqlServerBootstrap.Initialize();
}

Tambahkan kelas model BookDetails dalam proyek Anda dengan atribut yang akan cocok dengan skema db.

public class BookDetails
{
public int BookSerialNo { get; set; }

[Required]
[Display(Name = "ISBN")]
public string BookISBN { get; set; }

[Required]
[Display(Name = "Title")]
public string BookTitle { get; set; }

[Required]
[Display(Name = "Author")]
public string BookAuthor { get; set; }

[Required]
[Display(Name = "Publisher")]
public string BookPublisher { get; set; }

[Required]
[Display(Name = "Category")]
public string BookCategory { get; set; }
}

Tambahkan antarmuka yang disebut IBookDetailsRepository yang mendefinisikan akses Data dan fungsi CRUD (Buat, Baca, Perbarui, Hapus).

public interface IBookDetailsRepository
{
List<BookDetails> GetAll();
BookDetails FindById(int BookSerialNo);
bool AddBookDetail(BookDetails book);
bool UpdateBookDetail(BookDetails book);
bool DeleteBookDetail(int BookSerialNo);
}

Kemudian tentukan kelas yang mengimplementasikan metode antarmuka. Kelas ini berisi pernyataan CRUD aktual dan menggunakan sintaksis ORM yang lancar. Pastikan untuk menambahkan menggunakan RepoDb; pernyataan untuk dapat mengakses metode yang mendasari ORM.

public class BookDetailsRepository : IBookDetailsRepository
{
private readonly string connectionString;

public BookDetailsRepository()
{
this.connectionString = ConfigurationManager.ConnectionStrings["BookApplication"].ConnectionString;
}

public bool AddBookDetail(BookDetails book)
{
int serialNumber = 0;

try
{
using (var connection = new SqlConnection(this.connectionString))
{
var item = new BookDetails()
{
BookAuthor = book.BookAuthor,
BookCategory = book.BookCategory,
BookISBN = book.BookISBN,
BookTitle = book.BookTitle,
BookPublisher = book.BookPublisher
};

serialNumber = (int)connection.Insert<BookDetails>(item);
}
}
catch (Exception ex)
{
return false;
}

return true;
}

public bool DeleteBookDetail(int BookSerialNo)
{
int affectedRows = 0;

try
{
using (var connection = new SqlConnection(this.connectionString))
{
affectedRows = (int)connection.Delete<BookDetails>(BookSerialNo);
}
}
catch (Exception ex)
{
return false;
}

return true;
}

public BookDetails FindById(int BookSerialNo)
{
BookDetails result;

result = new BookDetails();

try
{
using (var connection = new SqlConnection(this.connectionString))
{
result = connection.Query<BookDetails>(x => x.BookSerialNo == BookSerialNo).FirstOrDefault();
}
}
catch (Exception ex)
{
return null;
}

return result;
}

public List<BookDetails> GetAll()
{
List<BookDetails> lstBooks;

lstBooks = new List<BookDetails>();

try
{
using (var connection = new SqlConnection(this.connectionString))
{
connection.Open();
lstBooks = connection.QueryAll<BookDetails>().ToList();
}
}
catch (Exception ex)
{
return null;
}

return lstBooks;
}

public bool UpdateBookDetail(BookDetails book)
{
int affectedRows = 0;

try
{
using (var connection = new SqlConnection(this.connectionString))
{
var item = new BookDetails()
{
BookSerialNo = book.BookSerialNo,
BookAuthor = book.BookAuthor,
BookCategory = book.BookCategory,
BookISBN = book.BookISBN,
BookTitle = book.BookTitle,
BookPublisher = book.BookPublisher
};

affectedRows = (int)connection.Update<BookDetails>(item);
}
}
catch (Exception ex)
{
return false;
}

return true;
}
}

Tambahkan kelas layanan yang memanggil metode repositori. Ini akan menjadi perantara antara kelas pengontrol dan repositori.

public class BookService
{
private IBookDetailsRepository _repository;

public BookService()
{
_repository = new BookDetailsRepository();
}

public List<BookDetails> GetAll()
{
return _repository.GetAll();
}

public BookDetails FindById(int SerialNo)
{
return _repository.FindById(SerialNo);
}

public bool AddBookDetails(BookDetails item)
{
return _repository.AddBookDetail(item);
}

public bool UpdateBookDetails(BookDetails item)
{
return _repository.UpdateBookDetail(item);
}

public bool DeleteBookDetail(int SerialNo)
{
return _repository.DeleteBookDetail(SerialNo);
}
}

Seperti yang dinyatakan di atas, kelas pengontrol membuat instance kelas layanan dan memanggil metodenya dalam fungsi ActionResult pengontrol.

public class BooksController : Controller
{
private BookService _service;

public BooksController()
{
_service = new BookService();
}

public ActionResult Index()
{
var model = _service.GetAll();
return View(model);
}

public ActionResult Create()
{
return View();
}

[HttpPost]
public ActionResult Create(BookDetails book)
{
try
{
_service.AddBookDetails(book);
return RedirectToAction("Index");
}
catch
{
return View();
}
}

public ActionResult Edit(int id)
{
var model = _service.FindById(id);
return View(model);
}

[HttpPost]
public ActionResult Edit(BookDetails book)
{
try
{
_service.UpdateBookDetails(book);
return RedirectToAction("Index");
}
catch
{
return View();
}
}

public ActionResult Delete(int id)
{
var model = _service.FindById(id);
return View(model);
}

[HttpPost]
public ActionResult Delete(BookDetails book)
{
try
{
_service.DeleteBookDetail(book.BookSerialNo);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}

Tampilan yang akan berinteraksi dengan pengguna adalah sebagai berikut.
Index.cshtml

@model IEnumerable<RepoDB.Models.BookDetails>

@{
ViewBag.Title = "Index";
}

<br />
<p>
@Html.ActionLink("Create New", "Create", null, new { @class = "btn btn-primary" })
</p>
<div class="table-responsive">
<table id="tblBooks" class="table table-bordered table-condensed table-striped">
<tr>
<th>
@Html.Label("ISBN")
</th>
<th>
@Html.Label("Title")
</th>
<th>
@Html.Label("Author")
</th>
<th>
@Html.Label("Publisher")
</th>
<th>
@Html.Label("Category")
</th>
<th></th>
</tr>

@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.BookISBN)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookTitle)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookAuthor)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookPublisher)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookCategory)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.BookSerialNo }, new { @class = "btn btn-info" })
@Html.ActionLink("Delete", "Delete", new { id = item.BookSerialNo }, new { @class = "btn btn-warning" })
</td>
</tr>
}

</table>
</div>

Create.cshtml

@model RepoDB.Models.BookDetails

@{
ViewBag.Title = "Create";
}

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>New Book</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.BookISBN, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookISBN, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BookISBN, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.BookTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BookTitle, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.BookAuthor, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookAuthor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BookAuthor, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.BookPublisher, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookPublisher, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BookPublisher, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.BookCategory, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookCategory, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BookCategory, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

Edit.cshtml

@model RepoDB.Models.BookDetails

@{
ViewBag.Title = "Update";
}

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.BookSerialNo)
<div class="form-horizontal">
<h4>Update Book</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.BookISBN, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookISBN, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BookISBN, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.BookTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BookTitle, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.BookAuthor, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookAuthor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BookAuthor, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.BookPublisher, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookPublisher, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BookPublisher, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.BookCategory, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookCategory, new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(model => model.BookCategory, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

Delete.cshtml

@model RepoDB.Models.BookDetails

@{
ViewBag.Title = "Delete";
}

<h3>Are you sure you want to delete this record?</h3>
<div>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.BookISBN)
</dt>

<dd>
@Html.DisplayFor(model => model.BookISBN)
</dd>

<dt>
@Html.DisplayNameFor(model => model.BookTitle)
</dt>

<dd>
@Html.DisplayFor(model => model.BookTitle)
</dd>

<dt>
@Html.DisplayNameFor(model => model.BookAuthor)
</dt>

<dd>
@Html.DisplayFor(model => model.BookAuthor)
</dd>

<dt>
@Html.DisplayNameFor(model => model.BookPublisher)
</dt>

<dd>
@Html.DisplayFor(model => model.BookPublisher)
</dd>

<dt>
@Html.DisplayNameFor(model => model.BookCategory)
</dt>

<dd>
@Html.DisplayFor(model => model.BookCategory)
</dd>
</dl>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.BookSerialNo)
<div class="form-actions no-color">
@Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-primary" })
<input type="submit" value="Delete" class="btn btn-danger" />
</div>
}
</div>

Keluaran

Selamat membuat kode!

Pos terkait

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *