Ther are many repository out there. From many of them meets our requirement or not. So I have created a Complete Repository For any CRUD application.
This project using MVC Core 2.1 along with visual studio Download it for here
After creating Project Add DBContext class If you don't know how to add DbContext:How to Cteate Application DbContext
Now flow the steps carefully
- Add Application DbContext
- Add IRepository.cs
- Add Repository.cs
- Add IUnitOfWork.cs
- Add UnitOfWork.cs
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped(typeof(IUnitOfWork), typeof(UnitOfWork));
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public int BrandId { get; set; }
public Brand Brand { get; set; }
}
public class Brand
{
public int Id { get; set; }
public string Name { get; set; }
public List<Product> Products { get; set; }
}
Initialize in every controller where you making Database Call
private readonly IUnitOfWork _unitOfWork;
)
public class ProductController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public ProductController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult BrandIndex()
{
var model = _unitOfWork.Repository<Brand>().GetAllInclude(x => x.Products);
//do something if you want
return View(model);
}
public IActionResult AddBrand()
{
return View();
}
[HttpPost]
public IActionResult AddBrand(Brand model)
{
if (!ModelState.IsValid)
{
ModelState.AddModelError("", "Something wrong");
return View(model);
}
_unitOfWork.Repository<Brand>().Insert(model);
//save new data to database
_unitOfWork.Commit();
return RedirectToAction("BrandIndex");
}
public IActionResult AddProduct()
{
var model=new Product();
//brand list for dropdown
var dbBrand = _unitOfWork.Repository<Brand>().GetAll();
ViewBag.BrandList = dbBrand.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString()
});
return View(model);
}
[HttpPost]
public async Task<IActionResult> AddProduct(Product product)
{
if (!ModelState.IsValid)
{
ModelState.AddModelError("","Something wrong");
return View(product);
}
_unitOfWork.Repository<Product>().Insert(product);
//save asynchronously to database
await _unitOfWork.CommitAsync();
return RedirectToAction("ProductList");
}
public IActionResult ProductList()
{
var model = _unitOfWork.Repository<Product>().GetAllInclude(x => x.Brand);
return View(model);
}
}
- Repository should not contain any save method (Personal advice)