NorthwindTraders icon indicating copy to clipboard operation
NorthwindTraders copied to clipboard

[Discussion] Should DbSet<T> be abstracted away just like DbContext

Open hcvo opened this issue 5 years ago • 2 comments

Hi Jason,

Will you consider abstracting away the DbSet<T> in INorthwindDbContext so that INorthwindDbContext will be completely independent from EF? And thus we can swap out SqlServer provider to use any data provider that is not supported by EF such as NoSql Db providers?

Exemple code belows:

    public interface INorthwindDbSet<T>
    {
         void AddRange(IEnumerable<T> entities);
         ….
    }
    public class NorthwindDbSet<T> : DbSet<T>, INorthwindDbSet<T>, IEnumerable<T>, IQueryable<T> 
    {
        .....
    }


    public interface INorthwindDbContext
    {
        IDbSet<Category> Categories { get; set; }

        IDbSet<Customer> Customers { get; set; }

        IDbSet<Employee> Employees { get; set; }

        IDbSet<EmployeeTerritory> EmployeeTerritories { get; set; }

        IDbSet<OrderDetail> OrderDetails { get; set; }

        IDbSet<Order> Orders { get; set; }

        IDbSet<Product> Products { get; set; }

        IDbSet<Region> Region { get; set; }

        IDbSet<Shipper> Shippers { get; set; }

        IDbSet<Supplier> Suppliers { get; set; }

        IDbSet<Territory> Territories { get; set; }

        Task<int> SaveChangesAsync(CancellationToken cancellationToken);
    }

hcvo avatar Dec 03 '19 14:12 hcvo

DbSet is an implementation of the repository pattern, you don't need to put it in another repository abstraction. The same for DbContext - it is an implementation of UnitOfWork.

Hellevar avatar Dec 05 '19 08:12 Hellevar

The point is to abstract away a dependency on entity framework, not to add an unnecessary wrapper around DbSet and UnitOfWork for the heck of it.

Right now your entire application's logic is still dependent on an external library, which degrades the infrastructure layer, as that is the main (or entire) reason the infrastructure layer exists in the first place.

I don't think it's a pragmatic issue since it seems highly unlikely you would ever make a switch from sql to no sql or even switch away from using Entity Framework to something else, but from a purist viewpoint, it would be nice to be able to say all external dependencies are part of the infrastructure layer.

CrispyDrone avatar Dec 20 '19 15:12 CrispyDrone

Thank you for your interest in this project. This repository has been archived and is no longer actively maintained or supported. We appreciate your understanding. Feel free to explore the codebase and adapt it to your own needs if it serves as a useful reference. If you have any further questions or concerns, please refer to the README for more information.

jasontaylordev avatar Jul 01 '23 22:07 jasontaylordev