允许通过互操作进行 CRUD 操作的数据库存储库模式 [英] Repository pattern for database that allows CRUD operations through Interops

查看:62
本文介绍了允许通过互操作进行 CRUD 操作的数据库存储库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前面临的情况是模型实体和数据库逻辑紧密交织在一起,这使得单元测试变得不可能.所以,我决定设计一个 Repository 模式.我们通过Com交互看到的存储模型的基本结构是Root-Children[每个孩子都是另一个Root].这是一个树状结构.理解了这一点,我们设计的 Repository 是用于 root 上 CRUD 操作的 RootRepository 和 RootRepository 内部的 ChildRepository 用于子级 CRUD 操作.我们决定 Create 只创建一个实体而不是持久化它,但更新将仅在数据库中找不到实体是键时才插入,或者在找到时更新.Read 将通过键获取实体.因此,在与 Repository API 交互时,我们决定首先使用 key 获取实体,如果它为 null,则调用 Create basic entity(Repository 使用工厂),如果需要,可以更新它并使用 update 持久化回 DB.因为它是一个指向另一个实体的 Value 对象,所以没有孩子可以被持久化.要持久化子项,我们必须先持久化子引用实体,然后请求根存储库创建子对象,然后可以将其添加到父子项集合中,并调用父级持久性,这样子项将与父项一起持久化.

The situation we are facing currently is Model Entities and database logic are tightly interweaved together which is making unit tests impossible. So, I decided to go with designing a Repository pattern. The basic structure of storage model what we see through Com interactions Root-Children[each child is another Root]. It's a tree structure. Understanding this, the Repository we designed is RootRepository for CRUD operations on root and ChildRepository internal to RootRepository for CRUD operations for child. We decided Create for only creating an entity but not to persist it but update will either insert only if no entity is key is not found in DB or to update upon found. Read will fetch the entity by key. So while interacting with the Repository API we decided to get entity first using key and if it is null then call Create basic entity(Repository uses factory) and it can be updated if it is required and persisted back to DB using update. No child can be persisted itself since it is an Value object pointing to another entity. To persist child, we have to persist the child referring entity first and later request root Repository to create child object and upon can be added to parent children collection and parent persistence is called so than children will be persisted along with parent.

所以,我想知道我们遵循的方法论,设计模式真的符合标准.据我们所知,这是我们可以通过最少的数据模拟获得单元测试和测试支持的唯一方法.我在网上四处寻找构建存储库的想法,但没有任何帮助.我的大部分问题都将在我们的单元测试中解决,但我想知道是否已经存在任何设计模式.在这个早期阶段,很容易迁移到任何存在的标准框架,我希望我能从你们那里得到任何指导.

So, I wonder about the methodology we are following and design pattern is really upto standard. As far as we know this is the only way we can get support for unit testing and test with minimal data mocks. I looked all around on web for getting ideas for building repositories but nothing helped me. Most of my questions here will be addressed in our unit tests but I wonder if there is any design pattern exist already. At this early stage it is easy to migrate to any standard framework if exist and I hope I will get any directions from you guys.

推荐答案

实现以下代码,您将能够在所有实体中重复使用它.

Implement the code below, and you'll be able to re-use it across all of your entities.

public interface IRepository<T> where T : class
{
    T Find(params object[] id);
    IQueryable<T> Where(Expression<Func<bool, T>> predicate);
    T Add(T entity);
    T Update(T entity);
    void Delete(T entity);
}

public class Repository<T> where T : class
{
    private DbSet<T> dbSet;
    public Repository(ApplicationContext context)
    {
        this.dbSet = context.Set<T>();
    }

    public T Find(params object[] id) { throw new NotImplementedException(); }
    public IQueryable<T> Where(Expression<Func<bool, T>> predicate) { throw new NotImplementedException();}
    public T Add(T entity){ throw new NotImplementedException();}
    public T Update(T entity){ throw new NotImplementedException();}
    public void Delete(T entity){ throw new NotImplementedException();}
}

这篇关于允许通过互操作进行 CRUD 操作的数据库存储库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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