简体   繁体   English

在ASP.NET Core中以现有存储库模式正确实现工作单元

[英]Proper Implementation of Unit of Work in Existing Repository Pattern in ASP.NET Core

Good day, 美好的一天,

I'm using repository pattern. 我正在使用存储库模式。 But someone advised me to use include Unit of Work. 但是有人建议我使用包括工作单元。 I read a lot of articles and honestly I found a docs that are too complicated to understand. 我读了很多文章,说实话,我发现文档太复杂了,难以理解。

Supposing I have a non-generic repository. 假设我有一个非通用存储库。

// My Interface 
public interface IProductRepository{
    IQueryable Products();
    IQueryable ProductById(int id);
    void InsertProduct(Product product);
    void UpdateProduct(Product product);
    void DeleteProductById(int id);
}

// Abstract Implementation

public class ProductRepository : IProductRepository{
    private readonly MyDbContext context;

    public ProductRepository(MyDbContext context){
        this.context= context;
    }

    public IQueryable Products(){  
        context.Product(); 
    }
    public IQueryable ProductById(int id){ 
        context.Product().Where(prod=>prod.Id == id);    
    }
    public void InsertProduct(Product product){
        context.Product.Add(product);
        context.SaveChanges();
    }
    public void UpdateProduct(Product product){
        context.Product.Update(product);
        context.SaveChanges();
    }
    public void DeleteProductById(int id){
        var product = ProductById(id);
        context.Product.Remove(product);
        context.SaveChanges();
    }
}

My question is, how can I use Unit of Work here? 我的问题是,如何在这里使用工作单元? Can you please show me a code below. 您能给我看下面的代码吗? It will be so much helpful for me to understand. 这对我理解非常有帮助。

In your IProductRepository add a Save method 在您的IProductRepository中添加一个Save方法

// My Interface 
public interface IProductRepository{
    // ...
    void Save();
}

Then in concrete ProductRepository, you do two things: 然后在具体的ProductRepository中,执行以下两项操作:

  1. Remove all calls to context.SaveChanges(); 删除所有对context.SaveChanges();的调用。
  2. In IProductRepository.Save implementation, call context.SaveChanges() 在IProductRepository.Save实现中,调用context.SaveChanges()

Do all operations on the dbcontext, and in the end call Save(). 在dbcontext上执行所有操作,最后调用Save()。

static void main() {
    // ...
    this.productRepository.InsertProduct(product1);
    this.productRepository.InsertProduct(product2);
    this.productRepository.InsertProduct(product3);
    this.productRepository.Save();
}

That's the gist of unit of work. 这就是工作单元的要旨。

The main reason why you need unit of work in your project is you need to make sure all your repository have same context. 在项目中需要工作单元的主要原因是,需要确保所有存储库都具有相同的上下文。 The relationship between unit of work and repository like the relationship between DbContext and DbSet in Entity framework. 工作单元和存储库之间的关系,例如Entity框架中的DbContext和DbSet之间的关系。

If you want to implement your UnitOfWork you can ref this link: https://codereview.stackexchange.com/questions/47879/unit-of-work-and-repository-with-entity-framework-6 如果要实现UnitOfWork,则可以引用以下链接: https : //codereview.stackexchange.com/questions/47879/unit-of-work-and-repository-with-entity-framework-6

This is really more of an answer to your questions in the comments about EF doing the work 这实际上是对有关EF开展工作的评论中的问题的答案
If you look at how EFCore does this, you actually define a context and define what classes you want inside that context. 如果您查看EFCore的工作方式,则实际上定义了一个上下文,并定义了在该上下文中需要哪些类。 From https://docs.microsoft.com/en-us/ef/core/ they define a BloggingContext that inherits from DbContext. 他们从https://docs.microsoft.com/zh-cn/ef/core/定义了一个继承自DbContext的BloggingContext。 The example below is from microsoft: 下面的示例来自microsoft:

    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=localdb)\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;");
    }
}

They have a Blog class and a Post class in this example that get created into tables when a migration and update are ran on the database( https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/ ). 在此示例中,它们具有Blog类和Post类,当在数据库上运行迁移和更新时,它们将创建到表中( https://docs.microsoft.com/zh-cn/ef/core/managing-schemas/迁移/ )。 Now that you have Blog and Post defined inside the BloggingContext you can create an instance(good to use dependency injection you can read more about that here if you like https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext ) of the BloggingContext and use linq to your hearts content, no need for a custom repository or unit of work. 现在您已经在BloggingContext中定义了Blog和Post,您可以创建一个实例(很好地使用依赖项注入,如果您喜欢https://docs.microsoft.com/zh-cn/ef/core/ ,可以在此处阅读有关此内容的更多信息。 BloggingContext的杂项/配置-dbcontext ),并使用linq满足您的需求,无需自定义存储库或工作单元。 EF does the work once you have defined what classes you want in what context. 一旦定义了在什么上下文中想要的类,EF就会开始工作。 With that said, we do use repository and unit of work where I work, if there is a reason to use the patterns great, but past that or learning it for yourself, EF does really well. 话虽如此,如果确实有理由使用很棒的模式,那么我们确实会在我工作的地方使用存储库和工作单元,但是除此之外,还是自己学习,EF确实做得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于发布实体的 Asp.Net 核心存储库和工作单元模式 - Asp.Net core Repository and Unit Of Work Pattern for Posting an entity 如何在我的 ASP.NET 核心 Web API 项目中使用工作单元模式和通用存储库模式? - How can I use unit of work pattern with generic repository pattern in my ASP.NET Core Web API project? 在ASP.NET Core中正确实现NRT - Proper implementation NRT in ASP.NET Core 带有存储库和工作单元的 ASP.NET 标识 - ASP.NET Identity with Repository and Unit of Work ASP.NET 内核上的身份工作单元 - Unit of Work with Identity on ASP.NET Core ASP.NET MVC - 如何在存储库模式中单元测试边界? - ASP.NET MVC - How to Unit Test boundaries in the Repository pattern? 如何在 asp.net 核心中使用依赖注入以工作单元模式延迟注入存储库 - How to lazy inject repositories in unit of work pattern using dependency injection in asp.net core 实体框架和 ASP.NET Core UOW 存储库模式 - Entity Framework and ASP.NET Core UOW Repository pattern ASP.Net核心MVC存储库模式意外处置 - ASP.Net Core MVC Repository Pattern Unexpectedly disposing Asp.net核心通用存储库模式,继承不起作用 - Asp.net core generic repository pattern with inheritance not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM