【问题标题】:Put this logic into a Repository or a Service将此逻辑放入存储库或服务中
【发布时间】:2013-03-12 16:39:16
【问题描述】:

我应该将此方法放入我的 ISchoolclassCodeRepository 还是我的 ISchoolclassService 中?

/// <summary>
        /// The client passes newly created and existing schoolclass codes.
        /// The newly created schoolclass codes are returned.
        /// </summary>
        /// <param name="newAndExistingSchoolclassCodes">All schoolclass codes and the newly created by the user</param>
        /// <param name="schoolyearId">The related schoolyear for a schoolclass code</param>
        /// <returns>The newly created schoolclass codes</returns>
        public IEnumerable<SchoolclassCode> GetNewCreatedSchoolclassCode(IEnumerable<SchoolclassCode> newAndExistingSchoolclassCodes, int schoolyearId)
        {
            var existingSchoolclassCodes = _uniOfWork.SchoolclassCodeRepository.GetSchoolclassCodes(schoolyearId).ToList();
            var newSchoolclassCodes = existingSchoolclassCodes.Except(newAndExistingSchoolclassCodes,new SchoolclassCodeComparer());
            return newSchoolclassCodes;
        }

【问题讨论】:

    标签: c# repository-pattern service-layer


    【解决方案1】:

    这似乎更像是应用程序逻辑,在您的情况下,这将在您的服务中实现。但是,这通常会进入应用程序层。 Here's some more information 解释了各个层,也许您想采用其中的一些概念。

    存储库仅保存数据访问逻辑:这意味着类似 CRUD 的操作。

    【讨论】:

    • 是的,我将其用于我的服务。存储库有在服务中调用的 FetchById 方法,我在其中执行 linq,除了东西。
    【解决方案2】:

    我将投票给ISchoolclassService,如果你有的话。但是,我不会为此目的创建一个。

    这也取决于SchoolclassCodeComparer 做了什么。如果它有自己的数据访问权限,那么它会调用同一个存储库来获取学校代码吗?

    如果有一种方法可以将SchoolclassCodeComparer 施加的约束传递给查询,那么我会在存储库中使用它。使用各种规则对结果集进行后处理可以驻留在服务中。

    我更喜欢让存储库层处理 DataAccess。 Dataaccess 中的逻辑将仅限于构造参数和创建返回结果的投影(如果需要)。

    基于任何其他业务逻辑来处理/过滤数据集可能是服务的工作。

    【讨论】:

    • 没有参数构造或结果(集)?!我使用 ORM 工具。
    • 不确定我的理解是否正确,但我的意思是说您有一个要过滤的SchoolCode 列表,您可以将它作为“不在”过滤器传递给您的数据库。这意味着将列表转换为逗号分隔的字符串,此逻辑可以位于存储库本身中。使用 ORM,您可以省去这个麻烦,并且还可以从 LINQ 中受益。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 2011-03-29
    • 2013-07-05
    • 2013-11-29
    • 2013-07-26
    • 2015-09-17
    相关资源
    最近更新 更多