在具有 3 层架构的 ASP.NET MVC 应用程序中验证业务规则的更好方法是什么? [英] What is better way to validate business rules in ASP.NET MVC application with 3 layer architecture?

查看:13
本文介绍了在具有 3 层架构的 ASP.NET MVC 应用程序中验证业务规则的更好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有 3 层经典架构的 ASP.NET MVC 应用程序1. 数据访问(Repositories)2. 业务逻辑(服务)3.应用层(MVC控制器类)任务是跟随有域类Learner并且学习者可以参加考试,参加考试会产生一个订单(Order类),在学习者参加考试之后,我们需要为每个学习者发布考试结果(这意味着给一个分数和等级)并且有一些需要验证的业务规则1.结果还未公布2.所有有状态的学习者都应该有标记3. 确定等级界限(考试分数和等级)当用户发布结果时,所有这些规则都应验证,如果某些规则不满足,则应显示错误消息.我决定所有与验证业务规则相关的逻辑都保留在 Service 类中,如果有任何规则未通过抛出特定异常,则在控制器类中,此异常将捕获并向客户端显示错误.这是代码

I'm developing a ASP.NET MVC application with 3 layer classic architecture 1. data access (Repositories) 2. Business logic (Services ) 3. Application layer (MVC Controller classes) The task is follow there is domain class Learner and learners can take exams, taking an exam produce an order (Order class), after that the learner take an exam, we need to release results of exam for each learner(this mean give a mark and grade) and has some business rule that need to be verified 1. the results hasn't been released yet 2. all learner who has status present should has mark 3. grading boundary should be confirmed (marks and grade for exam) When user do release results all this rules should validate and if some rule doesn't satisfied should display an error message. I decided that all logic related to validation business rules keep in the Service class and if any rule not pass throw specific exception, in the controller class this exception will catch and display error to the client. Here is the code

服务类

    public void ReleaseResults(long orderId)
    {
        var order =orderRepository.Get(orderId);

        Check.Require(order != null, "Order was not found");


        if (IsOrderReleased(order))
        {
            throw new ReleaseResultsException("The results has been already released", order.OrderNo);
        }

        if (AllLearnersHasStatusPresentAndMark(order))
        {
            throw new ReleaseResultsException("One or more learners unmarked", order.OrderNo);
        }
        if (!GradingBoundaryConfirmed(order))
        {
            throw new ReleaseResultsException("The Grading boundary needs to be confirmed", order.OrderNo);
        }



        foreach (var learnerDetail in order.LearnerDetails)
        {
            if (HasNotStatusPresent(learnerDetail))
            {
                continue;
            }
            learnerDetail.SetReleasedResults();

        }

        orderRepository.SaveOrUpdate(order);
    }

控制器类

        public ActionResult Release(EncryptedId orderId)
    {
        Check.Require(orderId != null, "The parameter orderId was null");

        try
        {
            orderReleaseResultsService.ReleaseResults(orderId);
        }
        catch (ReleaseResultsException e)
        {
            return Content(string.Format("Error: {0}", e.Message));
        }

        return Content(MUI.TheResultsHasBeenReleased);
    }

我不确定这是否是验证业务规则的最佳方式,有人可以帮我提供建议或更好地解决此问题吗?提前致谢!

I am not sure if this is best way to validate business rules, can anyone help me with suggestions or better solution of this problem? Thanks in advance!

推荐答案

我会避免将异常用于验证目的,而是使用返回 true/false 的方法.显然,对于验证是数据层数据的某些任务(例如强制执行数据库约束),您可以使用异常.

I would avoid using exceptions for validation purposes but rather have methods that return true/false. Obviously for some tasks where validation is data at the data tier (for example enforcing database constraints) you could use exceptions.

你可以看看下面关于的教程在服务层验证.

这篇关于在具有 3 层架构的 ASP.NET MVC 应用程序中验证业务规则的更好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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