如何处理与存储库模式表的关系? [英] How do I handle table relationships with the repository pattern?

查看:95
本文介绍了如何处理与存储库模式表的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施库模式作为一个ASP.NET MVC站点的一部分。我见过库的大多数例子是相当简单的。例如下面是一个典型的抽象库接口。

I'm implementing the repository pattern as part of an ASP.NET MVC site. Most examples I've seen of repositories are fairly simple. For example here's a typical abstract repository interface.

public interface IRepository<TEntity>
{
    IQueryable<TEntity> All();
    TEntity FindBy(int id);
    TEntity FindBy(Expression<Func<TEntity, bool>> expression);
    IQueryable<TEntity> FilterBy(Expression<Func<TEntity, bool>> expression);
    bool Add(TEntity entity);
    bool Update(TEntity entity);
    bool Delete(TEntity entity):
}

这是我清楚你将如何使用存储库这样的添加,更新,删除或获得实体单一类型的。但是,你如何处理创建和操作一个一对多或多对一的许多不同类型的?

It's clear to me how you would use a repository like this to add, update, delete, or get entities of a single type. But how do you handle creating and manipulating one-to-many or many-to-many relationships between different types?

假设你有一个项目键入其中每个项目被分配到类别。你将如何使通过资料库这项任务?如果这是到更新(C类)和/或更新(项目一)的方法来找出什么关系需要或从该元件被更新到进行?还是应该有一个明确的 AssignCategoryToItem(项目i,C类)的方法?

Say you have an Item type where each item is assigned to a Category. How would you make this assignment through the repository? Should this be up to the Update(Category c) and/or Update(Item i) methods to figure out what relationships need to be made to or from the element being updated? Or should there be an explicit AssignCategoryToItem(Item i, Category c) method?

如果它让我用流利的NHibernate的执行我的具体存储库的任何差异。

If it makes any difference I'm using Fluent NHibernate to implement my concrete repositories.

推荐答案

请问你的应用程序处理分配类别的项目?

How does your app handle assigning a category to an item?

做的:


  1. 允许用户查看的项目,然后为它分配一个类别

  2. 允许用户查看的类别,然后添加项目

让你的应用程序决定你选择哪一种方法。

Let your app dictate which method you pick.

这带来了有一个商业逻辑/服务层和处理的 总根 。在我的应用程序,我一直有一个服务层,所有的业务逻辑所在。我发现,这样做使我的应用程序更易于理解和维护/重构。 (注:我也用通用的存储库,并把单独的功能复杂的存储库中查找UPS在适当的服务类)

This brings up the idea of having a Business Logic/Services layer and dealing with aggregate roots. In my applications, I always have a services layer where all the business logic is at. I found that doing this has made my application easier to understand and maintain/refactor. (Note: I also use generic repositories and put the complex repository look ups in separate functions in the proper service class)

在你的服务层,你就会有一个功能叫做 AssignCategoryToItem 然后它会使用类别(中聚合根),然后您将添加项目到类别并保存更改 - 但我会preFER传入标识和更新之前,从数据库中提取的。

In your services layer, you'll have a function called AssignCategoryToItem which would then take the category (the aggregate root) and you would then add the item to that category and save the changes - although I would prefer passing in the IDs of the category and pulling it from the database before updating.

这篇关于如何处理与存储库模式表的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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