Datareader vs Dataadapter vs something else

ankit goel 766 Reputation points
2023-10-03T14:38:41.0066667+00:00

HI ,

i have been recently working on the c# code to pull the data from sql server database but have been stuck at creating a generic method which can retrieve large data from database . The term generic is used because my application have 20 forms and most of them will open , close , or edit data multiple times that i don't know . i want to escape myself from the overhead of creating variables and instances again and again . my other question is i had seen that some developers using generic types to save the datatable inside them (instead of datareader ) and then use it to show the data inside datagridview . the reason they say is performance . (i dont have the link right now) . So pls share some indepth knowledge as my applicationhas multiple datagridview's with editing and updating data all the time .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,908 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,113 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,456 Reputation points
    2023-10-03T18:22:26.2833333+00:00

    There really is no generic way to do this other than creating base generic repositories which I do not have time to provide you with code samples. Your best option is to have classes that the forms use to read and update your data so everything for CRUD is in one or more classes depending on how you decide to organize data.

    Generic repositories start out with an interface such as below

    public interface IBaseRepository<T> : IOperations<T> where T : class
    {
    
    }
    public interface IOperations<T> where T : class
    {
        IEnumerable<T> GetAll();
        Task<List<T>> GetAllAsync();
        T GetById(int id);
        T GetByIdWithIncludes(int id);
        Task<T> GetByIdAsync(int id);
        Task<T> GetByIdWithIncludesAsync(int id);
        bool Remove(int id);
        void Add(in T sender);
        void Update(in T sender);
        int Save();
        Task<int> SaveAsync();
        public T Select(Expression<Func<T, bool>> predicate);
        public Task<T> SelectAsync(Expression<Func<T, bool>> predicate);
    }
    

    Than for each table you might have

    public class ProductsRepository : IBaseRepository<Product>, IDisposable
    {
        private Context _context;
    
        public ProductsRepository(Context context)
        {
            _context = context;
        }
    
        public IEnumerable<Product> GetAll()
        {
            return _context.Products.ToList();
        }
    
        public Task<List<Product>> GetAllAsync()
        {
            return _context.Products.ToListAsync();
        }
    
        public Product GetById(int id)
        {
            return _context.Products.Find(id);
        }
    
        public Product GetByIdWithIncludes(int id)
        {
            throw new NotImplementedException();
        }
    
        public async Task<Product> GetByIdAsync(int id)
        {
            return await _context.Products.FindAsync(id);
        }
    
        public Task<Product> GetByIdWithIncludesAsync(int id)
        {
            throw new NotImplementedException();
        }
    
        public bool Remove(int id)
        {
            var product = _context.Products.Find(id);
            if (product is { })
            {
                _context.Products.Remove(product);
                return true;
            }
    
            return false;
        }
    
        public void Add(in Product sender)
        {
            _context.Add(sender);
        }
    
        public void Update(in Product sender)
        {
            _context.Entry(sender).State = EntityState.Modified;
        }
    
        public int Save()
        {
            return _context.SaveChanges();
        }
    
        public Task<int> SaveAsync()
        {
            return _context.SaveChangesAsync();
        }
    
        public Product Select(Expression<Func<Product, bool>> predicate)
        {
            return _context.Products.WhereNullSafe(predicate).FirstOrDefault();
        }
    
        public async Task<Product> SelectAsync(Expression<Func<Product, bool>> predicate)
        {
            return await _context.Products.WhereNullSafe(predicate).FirstOrDefaultAsync();
        }
        private bool _disposed = false;
    
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }
            _disposed = true;
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
    

    The above uses EF Core. Now we can go deeper and create a base core generic class project but if you are asking about generics than I suggest doing more research as at some level you may need to get into reflection.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.