Skip to main content

Generic Repository with Entity Framework Core

Recently I worked on a project with ASP.NET Core which uses Entity Framework Core.
With them, I used a generic repository pattern in the data layer.
Repository interface was like below:
 using System;  
 using System.Collections.Generic;  
 using System.Linq.Expressions;  
 using OnlineSurvey.Models;  
 namespace OnlineSurvey.Data  
 {  
   public interface IRepository<T> where T : BaseEntity  
   {  
     T GetById(int id, params Expression<Func<T, object>>[] includExpressions);  
     void Add(T entity);  
     void Delete(T entity);  
     void Delete(int id);  
     IEnumerable<T> GetAll(params Expression<Func<T, object>>[] includExpressions);  
     IEnumerable<T> Find(Expression<Func<T, bool>> where);  
     int Count();  
   }  
 }  
And the implementation was like below:
 using System;  
 using System.Data.Entity;  
 using System.Data.Entity.Infrastructure;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Linq.Expressions;  
 using OnlineSurvey.Models;  
 namespace OnlineSurvey.Data  
 {  
   public class Repository<T> : IRepository<T> where T : BaseEntity  
   {  
     protected DbSet<T> DbSet => this.Context.Set<T>();  
     protected DataContext Context { get; set; }  
     public Repository(DataContext context)  
     {  
       this.Context = context ?? throw new ArgumentException("An instance of DbContext is required to use this repository.", nameof(context));  
     }  
     public IEnumerable<T> GetAll(params Expression<Func<T, object>>[] includExpressions)  
     {  
       if (includExpressions.Any())  
       {  
         return  
           includExpressions.Aggregate<Expression<Func<T, object>>, IQueryable<T>>(this.DbSet,  
             (current, expression) => current.Include(expression)).ToList();  
       }  
       return this.DbSet.ToList();  
     }  
     public T GetById(int id, params Expression<Func<T, object>>[] includExpressions)  
     {  
       if (includExpressions.Any())  
       {  
         var set =  
           includExpressions.Aggregate<Expression<Func<T, object>>, IQueryable<T>>(this.DbSet,  
             (current, expression) => current.Include(expression));  
         return set.SingleOrDefault(s => s.Id == id);  
       }  
       return this.DbSet.Find(id);  
     }  
     public void Add(T entity)  
     {  
       DbEntityEntry entry = this.Context.Entry(entity);  
       if (entry.State != EntityState.Detached)  
       {  
         entry.State = EntityState.Added;  
       }  
       else  
       {  
         this.DbSet.Add(entity);  
       }  
     }  
     public void Delete(T entity)  
     {  
       DbEntityEntry entry = this.Context.Entry(entity);  
       if (entry.State != EntityState.Deleted)  
       {  
         entry.State = EntityState.Deleted;  
       }  
       else  
       {  
         this.DbSet.Attach(entity);  
         this.DbSet.Remove(entity);  
       }  
     }  
     public void Delete(int id)  
     {  
       var entity = this.GetById(id);  
       if (entity != null)  
       {  
         this.Delete(entity);  
       }  
     }  
     public IEnumerable<T> Find(Expression<Func<T, bool>> where)  
     {  
       return DbSet.Where(where).ToList();  
     }  
     public int Count()  
     {  
       return DbSet.Count();  
     }  
   }  
 }  

Usage of the above mentioned implementation:
 Question question = this._unitOfWork.Questions.GetById(id, q=>q.Answers);  
Above code has one issue. There is no way load entities inside a list. This problem can be fixed if we could have access to inbuilt function .ThenInclude.

The following change will give us access to the above mentioned function.
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Linq.Expressions;  
 using System.Threading.Tasks;  
 using OnlineSurvey.Models;  
 using Microsoft.EntityFrameworkCore.Query;
  
 namespace OnlineSurvey.Data  
 {  
   public interface IRepository<T> where T : BaseEntity  
   {  
     Task<T> GetByIdAsync(int id, Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null);  
     Task AddAsync(T entity);  
     void Delete(T entity);  
     Task DeleteAsync(int id);  
     Task<IEnumerable<T>> GetAllAsync(Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null);  
     Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate = null,  
                     Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null);  
     int Count();  
     Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicte = null,  
                   Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null);  
     void Update(T entity);  
   }  
 }  
Refer to the below implementation:
 using System;  
 using System.Data.Entity;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Linq.Expressions;  
 using OnlineSurvey.Models;  
 using System.Threading.Tasks;  
 namespace OnlineSurvey.Data  
 {  
   public class Repository<T> : IRepository<T> where T : BaseEntity  
   {  
     protected DbSet<T> DbSet => this.Context.Set<T>();  
     protected DbContext Context { get; set; }  
     public Repository(DbContext context)  
     {  
       this.Context = context ?? throw new ArgumentException("An instance of DbContext is required to use this repository.", "context");  
     }  
     public async Task<T> GetByIdAsync(int id, Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null)  
     {  
       IQueryable<T> query = this.DbSet;  
       if(include != null)  
       {  
         query = include(query);  
       }  
       return await query.AsNoTracking().SingleOrDefaultAsync(s => s.Id == id);  
     }  
     public async Task AddAsync(T entity)  
     {  
       EntityEntry entry = this.Context.Entry(entity);  
       if(entity.State != EntityState.Detached)  
       {  
         entry.State = EntityState.Added;  
       }  
       else  
       {  
         await this.DbSet.AddAsync(entity);  
       }  
     }  
     public void Delete(T entity)  
     {  
       EntityEntry entry = this.Context.Entry(entity);  
       if (entity.State != EntityState.Deleted)  
       {  
         entry.State = EntityState.Deleted;  
       }  
       else  
       {  
         this.DbSet.Attach(entity);  
         this.DbSet.Remove(entity);  
       }  
     }  
     public async Task DeleteAsync(int id)  
     {  
       T entity = await this.GetByIdAsync(id);  
       if(entity != null)  
       {  
         this.Delete(entity);  
       }  
     }  
     public async Task<IEnumerable<T>> GetAllAsync(Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null)  
     {  
       IQueryable<T> query = this.DbSet;  
       if (include != null)  
       {  
         query = include(query);  
       }  
       return await query.AsNoTracking().ToListAsync();  
     }  
     public async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate = null, Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null)  
     {  
       IQueryable<T> query = this.DbSet;  
       if (include != null)  
       {  
         query = include(query);  
       }  
       if(predicate != null)  
       {  
         query = query.Where(predicate);  
       }  
       return await query.AsNoTracking().ToListAsync();  
     }  
     public int Count()  
     {  
       return this.DbSet.Count();  
     }  
     public async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null, Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null)  
     {  
       IQueryable<T> query = this.DbSet;  
       if (include != null)  
       {  
         query = include(query);  
       }  
       if (predicate != null)  
       {  
         query = query.Where(predicate);  
       }  
       return await query.FirstOrDefaultAsync();  
     }  
     public void Update(T entity)  
     {  
       EntityEntry entry = this.Context.Entry(entity);  
       entity.State = EntityState.Modified;  
     }  
   }  
 }  
Usage of the above-mentioned implementation:
 Question question = this._unitOfWork.Questions.GetById(id, q=>q.Include(q => q.Answers).ThenInclude(a => a.Question));  
Hope this helps you in some way.

Comments

Popular posts from this blog

How to use DataBinder.Eval in HyperLink NavigationUrl - ASP.net

I wanted to append page Url with DataBinder.Eval value in ASP.NET hyperlink control on ASP.NET HTML source. I was trying it using many ways and also search for resources on web but I could not find any useful thing. I thought this will help u guys to get an idea about appending two strings. Solution as follows. <asp:HyperLink ID="customerHyperLink" runat="server" Text= ' ' NavigateUrl=' ' ></asp:HyperLink> You can see how I have appended these two value in NavigateUrl property. Hope some one will get the advantage of this post.

Common Design Principles

There are number of common design principles that, like design patterns, best practice over the years to build maintainable software. I'm up to describe some widely used design principles though out the post. Following common principle are extracted by the same book that I mentioned before ( Professional ASP.Net Design Patterns - Scott Millet ). Principles are as follows: Keep It Simple Stupid (KISS) One common issue in software programming is over-complicating a solution. So main concern of this principle is keep the code simple but not simplistic. Eventually this will avoid unnecessary complexities. Don't Repeat yourself (DRY) Main concern of this principle is to avoid the repetition. In other words this is all about abstracting out the common functionalities into a single place. Ex: If there is a price calculation method in a system. It should lay in a single place there. Tell Don't Ask The Tell, Don’t Ask principle is closely aligned with encapsulation and the assignin...

LINQ : Query expression vs Lambda Expression

All the time I was using LINQ on projects I was wondering whether there is a performance difference between query expression and lambda expression. So I decided to dig into it. I wanted to examine MSIL for both of these situations. I wrote small class which has two methods. One is using query expression and the other one is using lambda. I complied this code and got the dll and view the MSIL using ildasm.exe. This following picture will show you the MSIL of GetFirst() method. And this following picture will show you the MSIL of the GetSecond() method. Yes!!! you can see that there is no different between these 2 MSIL. Will there be a performance difference???