Skip to content

Commit

Permalink
Added dynamic eager loading to generic repository for including relat…
Browse files Browse the repository at this point in the history
…ed entities in product queries.
  • Loading branch information
aliarmaganuygun committed Oct 13, 2024
1 parent ffbdcba commit 8c57d0a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Bulky.DataAccess/Repository/IRepository/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace BookBazaar.DataAccess.Repository.IRepository
{
public interface IRepository<T> where T : class
{
IEnumerable<T> GetAll();
T Get(Expression<Func<T, bool>> filter);
IEnumerable<T> GetAll(string? includeProperties = null);
T Get(Expression<Func<T, bool>> filter, string? includeProperties = null);
void Add(T entity);
void Update(T entity);
void Remove(T entity);
Expand Down
28 changes: 24 additions & 4 deletions Bulky.DataAccess/Repository/Repository.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@
using BookBazaar.DataAccess.Data;
using BookBazaar.DataAccess.Repository.IRepository;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Linq.Expressions;

namespace BookBazaar.DataAccess.Repository
{
public class Repository<T> : IRepository<T> where T : class
{
private readonly ApplicationDbContext _db;
internal DbSet<T> dbSet;
public DbSet<T> dbSet;
public Repository(ApplicationDbContext db)
{
_db = db;
this.dbSet = _db.Set<T>();
_db.Products.Include(u => u.Category);
}

public void Add(T entity)
{
dbSet.Add(entity);
}

public T Get(Expression<Func<T, bool>> filter)
public T Get(Expression<Func<T, bool>> filter,string? includeProperties = null)
{
IQueryable<T> query = dbSet;
query = query.Where(filter);
if (!string.IsNullOrEmpty(includeProperties))
{
foreach (var includeProperty in includeProperties
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
}
return query.FirstOrDefault();
}

public IEnumerable<T> GetAll()

//Category is a navigation property in Product, so we need to include it
//Category, CoverType, Company etc. may be navigation properties in Product
public IEnumerable<T> GetAll(string? includeProperties = null)
{
IQueryable<T> query = dbSet;
if (!string.IsNullOrEmpty(includeProperties))
{
foreach (var includeProperty in includeProperties
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
}
return query.ToList();
}

Expand Down
2 changes: 1 addition & 1 deletion BulkyWeb/Areas/Admin/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ProductController(IUnitOfWork unitOfWork, IWebHostEnvironment webHostEnvi
}
public IActionResult Index()
{
List<Product> productList = _unitOfWork.Product.GetAll().ToList();
List<Product> productList = _unitOfWork.Product.GetAll(includeProperties:"Category").ToList();
return View(productList);
}

Expand Down
2 changes: 1 addition & 1 deletion BulkyWeb/Areas/Admin/Views/Product/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<td>@obj.Author</td>
<td>@obj.ISBN</td>
<td>@obj.Price</td>
<td>@obj.CategoryId</td>
<td>@obj.Category.Name</td>
<td>
<div class="w-75 btn-group" role="group">
<a asp-controller="Product" asp-action="Upsert" asp-route-id="@obj.Id" class="btn btn-primary mx-2">
Expand Down

0 comments on commit 8c57d0a

Please sign in to comment.