简体   繁体   English

大模型存储库服务模式的正确实现

[英]Correct Implementation of the repository service pattern for big model

I have a dilema implementing the repository service pattern in my C# WPF MVVM project where I have a big dataset in my model.我在我的 C# WPF MVVM 项目中实现存储库服务模式时遇到了困境,我的模型中有一个大数据集。

I used EntityFramework database first where I have created about 40 entities, so my model is quite large and for me it does not make sense to implement 40 different repositories, due to this I made use of the generic repository service pattern.我首先使用 EntityFramework 数据库,在那里我创建了大约 40 个实体,所以我的模型非常大,对我来说实现 40 个不同的存储库没有意义,因此我使用了通用存储库服务模式。

My Repository looks like the following:我的存储库如下所示:

  public interface IEntityRepository<T> : IDisposable where T : class
  {
    IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
    IEnumerable<T> getEntities();
    T RetrieveEntity(int entityID);
    void CreateEntity(T entity);
    void UpdateEntity(T entity);
    void DeleteEntity(T entity);
    void Save();
  }

And here is the signature of the class implementing the methods.这是实现方法的类的签名。

    public class EntityRepository<T> :
     IEntityRepository<T> where T : class
    {
      private DbContext context;
      public EntityRepository(DbContext context)
      {
        this.context = context;
      }          

       .....

    }

My question now is, how would the service look like so that I don't have to implement it for every single entity in my model.我现在的问题是,服务会是什么样子,这样我就不必为模型中的每个实体都实现它。 I'm looking for something like that I can dymanically create the service by the type name of the entity.我正在寻找类似的东西,我可以通过实体的类型名称动态地创建服务。

Does anyone knows a solution for my problem?有谁知道我的问题的解决方案? Thanks!谢谢!

I'm not convinced the repository pattern is a useful layer if your repository classes map one to one with your physical database entities.如果您的存储库类与您的物理数据库实体一对一映射,我不相信存储库模式是一个有用的层。

You might be better with a service layer or other type of facade.使用服务层或其他类型的外观可能会更好。 You might also find success using the query pattern if you insist on a data access layer on top of the entity framework.如果您坚持在实体框架之上使用数据访问层,您也可能会发现使用查询模式会取得成功。 In this pattern each query class could have a parameter object defining the parameters you can query your model on.在这种模式中,每个查询类都可以有一个参数对象,定义您可以查询模型的参数。 Where the returned data is structured more suitable for consumption in the business or user interface layer.其中返回的数据结构更适合在业务或用户界面层使用。 Each query object is then relevant to some part of your business or user interface layer.然后,每个查询对象都与您的业务或用户界面层的某些部分相关。 The query pattern can then be used when you have to query your data beyond accessing single entities.当您必须查询数据而不是访问单个实体时,可以使用查询模式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM