抽象ef核心2 dbContext [英] Abstracting ef core 2 dbContext

查看:51
本文介绍了抽象ef核心2 dbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在数据库上下文层(EntityFramework 2.0)上进行抽象.

I'm trying to make an abstraction over my DB Context layer (EntityFramework 2.0).

Car.DataContext
 ------------------- 
 public abstract class BaseCarContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Car>(e =>
            {
                e.ToTable("Car");             
            });
            modelBuilder.Entity<Car>(e => { e.ToTable("Cars"); });
        }
    }

     public class CarContext : BaseCarContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (optionsBuilder.IsConfigured)
                return;

            optionsBuilder.UseSqlServer(@"Server = xxxx; Database = xxxx; Trusted_Connection = True;");
        }

        public DbSet<Car> Cars { get; set; }        
    }

 Car.Logic
 ----------------
 public interface ICarService
 {
    GetCarResponse RetrieveCar(int id);
    void Save(int id);
    ...
 }

 public class CarService : ICarService
 {
    private readonly ICarService service;   
    // dbContext interface

    public CarService(ICarService service){
        this.service = service; 
        // injecting db context interface
    }

    public void Save(int id){
        ... saving using injected db context
        // injected db context.Insert(new Car{ Name = "Honda" });
    }
    ...
 }

我如何抽象这个ef核心2 CarContext 以便使用 dbContext save

How can I abstract this ef core 2 CarContext in order to use dbContext save

我试图制作一个由 CarContext 实现的接口 IDbContext 但是那样我不能使用 dbContext.Cars.Insert ,因为我没有实现dbContext汽车集合,因此无法访问ef核心方法和属性.

I tried to make an interface IDbContext which is implemented by CarContext but that way I cannot use dbContext.Cars.Insert because I'm not implementing dbContext cars collection don't have access to ef core methods and properties.

我当然可以使用具体的实现,但是我正在尝试进行抽象,以便可以使用单元测试,...

I can use of course concrete implementation but I'm trying to make an abstraction so I can use unit tests, ...

您将如何做?

推荐答案

首先,您不需要抽象即可进行单元测试.EF Core是100%测试友好的.第二,在我看来,对于EF(或实际上是 any ORM),唯一真正可接受的抽象是微服务或CQRS/事件源模式.那些实际上增加了价值的地方是,它们要么完全抽象了依赖关系和/或解决了实际的业务问题.但是,这些模式也需要大量的工作才能正确实现,因此通常保留给大型,复杂的应用程序.

First, you don't need an abstraction to unit test. EF Core is 100% test-friendly. Second, the only truly acceptable abstractions, in my opinion for EF (or really any ORM) is either a microservice or the CQRS/event sourcing patterns. Those actually add value in that they either fully abstract the dependency and/or solve real line-of-business problems. However, those patterns also require a significant amount of effort to implement correctly, and as such, typically are reserved for large, complex applications.

长短不一,除非您有充分的理由不这样做,否则直接使用EF.测试不是一个很好的理由.

Long and short, just use EF directly unless you have a truly good reason not to. Testing is not a good reason.

这篇关于抽象ef核心2 dbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆