MVC 3 我应该如何设计我的存储库/DAL?

发布于 2024-12-26 05:37:13 字数 299 浏览 3 评论 0原文

使用以下域模型: 世界->特许经营->办公室->代理

我知道存储库应该返回聚合根,这些域实体中的哪一个是聚合根?或全部(我需要为每个都提供一个存储库)吗?

我希望能够通过 url 传递的 id 获取实体。我应该把这些 GetById 方法放在哪里?

如果我想获取特许经营权中名字部分为“Jo”的所有代理商的列表,如何将其构建到系统中?我应该: Franchise 实体中有一个基本上是 .SelectMany linq 语句的方法吗? 或者有一个具有 GetByPartialFirstName() 方法的代理存储库?

With the following domain model:
World -> Franchise -> Office -> Agent

I understand that repositories should return aggrigate roots, which of these domain entities is the aggrigate root? or all (do I need a repository for each)?

I want to be able to get entities via their id passed by url. Where do I put these GetById methods?

If I want to get a list of all agents within a franchise with a partial first name of "Jo", how do I build that into the system? Should I:
Have a method in the Franchise entity that's basically a .SelectMany linq statement?
or have a repository for Agent that has GetByPartialFirstName() method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿萝 2025-01-02 05:37:13

取决于域逻辑,如果实体要被自己使用,那么它是一个聚合根,如果您想获取“Agent”的实例并直接从您的消费者代码调用方法,那么它应该有自己的存储库,否则,如果它将作为“Office”的一部分使用并且从其根“Office”调用其方法,那么它不是聚合根,您不需要从存储库独立获取它。

将此规则应用于所有实体,以决定为其创建存储库。

Depends on the domain logic, if the entity is to be consumed by its own then it is an aggregate root, if you want to get an instance of "Agent" and call methods directly from your consumer code then it should have it's own repository, otherwise if it is to be consumed as a part of "Office" and its methods are called from its root "Office" then it is not an aggregate root and you dont need to fetch it independently from repository.

Apply this rule to all the Entities to decide which to create a repository for.

蛮可爱 2025-01-02 05:37:13

我没有存储库方面的经验,但我建议,如果您计划使用多种方式来搜索代理,我建议创建一个标准接口/类。这将允许您通过单个方法调用执行多种类型的组合搜索(名字/姓氏、名字/城市等)。

(我喜欢使用接口来保持应用程序和数据源的解耦)

public IEnumerable/IQueryable<IAgent> Get(IAgentCriteria Criteria)
{
  if (Critiria == null) throw new ArgumentNullException();

  var result = entities.Agents;

  if (!string.IsNullEmptyOrWhiteSpace(Criteria.FirstNameStartsWith))
  {
    result = result.Where(x => x.FistName.StartsWith(Criteria.FirstNameStartsWith));
  }

  // etc etc
}

I don't have experience with repositories, but I would suggest that if you plan on having multiple ways to search for your agents, I would recommend creating a criteria interface/class. This would allow you to do multiple types of combination searches with a single method call (firstname/lastname, firstname/city, etc).

(I like using interfaces to keep my application and datasource decoupled)

public IEnumerable/IQueryable<IAgent> Get(IAgentCriteria Criteria)
{
  if (Critiria == null) throw new ArgumentNullException();

  var result = entities.Agents;

  if (!string.IsNullEmptyOrWhiteSpace(Criteria.FirstNameStartsWith))
  {
    result = result.Where(x => x.FistName.StartsWith(Criteria.FirstNameStartsWith));
  }

  // etc etc
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文