Entity framework with dapper in repository

ASP Developer 1 Reputation point
2022-01-07T12:25:30.627+00:00

Hi,

We are using entity framework with repository/unit of work pattern. Now, we would like to use dapper along with EF. Below are few basic classes we are using:

    public interface IRepository<TEntity> where TEntity : class
    {
        void Add(TEntity entity);
        void Update(TEntity entity);
        void Delete(int id);
        TEntity GetById(int id);
        IEnumerable<TEntity> GetAll();
    }



 public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        protected readonly DbContext _context;
        protected readonly DbSet<TEntity> _entities;

        public Repository(DbContext context)
        {
            _context = context;
            _entities = context.Set<TEntity>();
        }

        public virtual void Add(TEntity entity)
        {
            _entities.Add(entity);
        }

        public virtual void Update(TEntity entity)
        {
            _entities.Update(entity);
        }

        public virtual void Delete(int id)
        {
            var entity =  GetById(id);
            _entities.Remove(entity);
        }

        public virtual TEntity GetById(int id)
        {
            return _entities.Find(id);
        }

        public virtual IEnumerable<TEntity> GetAll()
        {
            return _entities.ToList();
        }
    }



    public interface IUnitOfWork
    {
        IEmployeeRepository Employees { get; }
        ICityRepository Cities { get; }
        int SaveChanges();
    }



  public class UnitOfWork : IUnitOfWork
    {
        readonly AppDbContext _context;

        IEmployeeRepository _employees;
        ICityRepository _cities;

        public UnitOfWork(AppDbContext context)
        {
            _context = context;
        }

        public IEmployeeRepository Employees
        {
            get
            {
                if (_employees == null)
                    _employees = new EmployeeRepository(_context);

                return _employees;
            }
        }

        public ICityRepository Cities
        {
            get
            {
                if (_cities == null)
                    _cities = new CityRepository(_context);

                return _cities;
            }
        }

        public int SaveChanges()
        {
            return _context.SaveChanges();
        }
    }



    public interface IEmployeeRepository : IRepository<Employee>
    {        
        bool CheckEmployeeExists(int employeeId, string employeeName);
    }


 public class EmployeeRepository : Repository<Employee>, IEmployeeRepository
    {
        public EmployeeRepository(DbContext context) : base(context)
        { }


        public bool CheckEmployeeExists(int employeeId, string employeeName)
        {
            // Implement Dapper code here
        }

        private AppDbContext _appContext => (AppDbContext)_context;
    }

Now, we would like to implement few methods of EmployeeRepository like CheckEmployeeExists to use Dapper. What changes do we need to make in above code considering best coding practices so that both EF and dapper work in proper way. How to integrate Dapper here?

Thank you.

Developer technologies .NET Other
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-01-11T07:24:28.14+00:00

    @ASP Developer , you could try the following code to check if Employee exists in the database.

    public bool CheckEmployeeExists(int employeeId, string employeeName)  
            {  
                // Implement Dapper code here  
                var sql = "SELECT * FROM Emplpoyee";  
                using (var connection = new SqlConnection(connstr))  
                {  
                    connection.Open();  
                    var list = connection.Query<Employee>(sql).ToList();  
                    var result = list.Any(i => i.Name==employeeName&&i.Id==employeeId);  
                    return result;  
                }  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.