Hey guys, sorry for the lateness since i have been implementing new PHP Framework for 3 weeks. Next time i’ll explain about that PHP Framework 😀 . Now lets continue to create CRUD using Jquery Ajax and Entity Framework. After we have data table User in our database that i have explained in the First Part, now we are going to create Repository layer, Service layer and Presentation Layer. But this time i’ll only explain MVC Controller for presentation layer.
Repository is the layer to do data transaction. This time, i’m using Repository Pattern. First we need to create IGenericRepository :
1 2 3 4 5 6 7 8 9 10 11 12 |
public interface IGenericRepository<T> { void Save(T t); void Update(T t); void Delete(Guid id); T GetById(Guid id); IList<T> GetAll(); } |
Why we need to create that interface? Because those methods are common function in all data transactions. So we don’t need to write those method again, it will be handled by inheritance.
Then we create Repository Interface for User Repository that’s called IUserRepository inherit by IGenericRepository :
1 2 3 4 |
public interface IUserRepository : IGenericRepository<User> { // Add your new method if it's necessary } |
After we created those interfaces we need to implement those interfaces into classes. Now create UserRepository class that implement IUserRepository :
You can generate the method of interface that you implement by using right click then click Implement Interface. Then fill the method with codes below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
public class UserRepository : IUserRepository { private RequestPartialPageAjaxDBContext myContext; public UserRepository() { myContext = new RequestPartialPageAjaxDBContext(); } public void Save(User user) { user.UserGuid = Guid.NewGuid(); myContext.Users.Add(user); myContext.SaveChanges(); } public void Update(User user) { User oldUser = myContext.Users.SingleOrDefault(ctx => ctx.UserGuid.Equals(user.UserGuid)); oldUser.UserGuid = user.UserGuid; oldUser.UserCode = user.UserCode; oldUser.UserName = user.UserName; oldUser.UserPhone = user.UserPhone; oldUser.UserAddress = user.UserAddress; myContext.SaveChanges(); } public void Delete(Guid id) { User user = myContext.Users.SingleOrDefault(ctx => ctx.UserGuid.Equals(id)); myContext.Users.Remove(user); myContext.SaveChanges(); } public User GetById(Guid id) { return myContext.Users.SingleOrDefault(ctx => ctx.UserGuid.Equals(id)); } public IList<User> GetAll() { return new List<User>(myContext.Users.ToList()); } } |
Well, Repository layer is done. Now lets create Service Layer. Service Layer is a place to do validation or implementation of your business process. Since this time we are not talking about validation, lets skip about validation, i’ll talk about this later on.
As usual, lets create the Interface that’s called IUserService :
1 2 3 4 5 6 7 8 9 10 11 12 |
public interface IUserService { void Save(User user); void Update(User user); void Delete(Guid id); User GetById(Guid id); IList<User> GetAll(); } |
Then we need to implement that interface, lets create UserService class :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
public class UserService : IUserService { private IUserRepository _iUserRepository; public UserService() { _iUserRepository = new UserRepository(); } public void Save(User user) { try { _iUserRepository.Save(user); } catch (Exception ex) { throw ex; } } public void Update(User user) { try { _iUserRepository.Update(user); } catch (Exception ex) { throw ex; } } public void Delete(Guid id) { try { _iUserRepository.Delete(id); } catch (Exception ex) { throw ex; } } public User GetById(Guid id) { try { return _iUserRepository.GetById(id); } catch (Exception ex) { throw ex; } } public IList<User> GetAll() { try { return _iUserRepository.GetAll(); } catch (Exception ex) { throw ex; } } } |
Ahaa.. Repository Layer and Service Layer are done. Now lets create MVC Controller to utilize the Service and Repository, later we’ll test it using Postman Rest Client to see the Json Result.
Now lets create UserController :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
public class UserController : Controller { private IUserService _iUserService; public UserController() { _iUserService = new UserService(); } public ActionResult Index() { return View(); } public ActionResult ListView() { return View(_iUserService.GetAll()); } public ActionResult Details(Guid id) { return View(_iUserService.GetById(id)); } public ActionResult Create() { return View(); } [HttpPost] public JsonResult CreateUser(User user) { _iUserService.Save(user); return Json(new { success = true }); } public ActionResult Edit(Guid id) { return View(_iUserService.GetById(id)); } [HttpPost] public ActionResult EditUser(User user) { _iUserService.Update(user); return Json(new { success = true }); } public JsonResult Delete(Guid id) { _iUserService.GetById(id); return Json(new { success = true }); } [HttpPost] public JsonResult DeleteUser(Guid id) { _iUserService.Delete(id); return Json(new { success = true }); } } |
After the controller is created. Now we can test it using Postman Rest Client. But only the JsonResult method that we can test since some of methods are using ActionResult to do partial page rendering that will be utilized by Ajax later on the next part.
Test Create Method :
Test Edit Method :
Test Delete Method :
Congratulation your Repository, Service, and Controller have created. Now you are having the Backend Service to continue creating ASP MVC CRUD Using Ajax, Entity Framework and Bootstrap. Please waiting for the next part, i’ll write it soon.
A passionate software engineer who wants to share anything that he learns and some of his life experiences. Want to know more about him? see Fatkhan Fauzi Profile
hehehe top markotop bro, i like it 😀 Sector Code
Thx bro.. 😀