异常与临时类型.什么可能会倒下? [英] Exception vs ad-hoc type. What is likely to fall over?

查看:26
本文介绍了异常与临时类型.什么可能会倒下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在研究 MVC 3 项目时正在阅读一本关于企业应用程序开发的书.我目前正在决定如何处理异常.以前我会让异常在堆栈中冒泡,然后在最高层处理.

I'm reading a book development of enterprise applications while working on MVC 3 project. I'm currently deciding on how to handle exceptions. Previously I would let the exception bubble up the stack and then handle it at the highest layer.

这本书建议在域模型中创建一个临时类并返回它.例如

The book suggests to create an ad-hoc class in a domain model and return that. E.g.

public sealed class MissingCustomer: Customer
{

}

// On method failure return new MissingCustomer();

我可以理解这个想法,但我正在努力证明有必要这样做.代码方面我同意返回一个新的失踪客户而不是抛出异常要简洁得多.

I can see the idea but I'm struggling to justify a need for that. Code wise I agree that it's a lot neater to return a new missing customer, instead of throwing an exception.

您如何看待这种方法,您是否遇到过这种方法产生重大影响的情况?

What do you think about this approach and have you come across scenario where this made a significant difference?

如果我们假设客户必须始终存在,那么抛出异常说嘿,客户应该始终存在,但由于某种原因它不存在,所以我将通知用户说有些异常发生了".另一方面,我可以假设客户有可能被另一个人移除,因此我需要优雅地处理这个问题.

If we assume that a customer must always exist, then it makes sense to throw an exception saying "Hey, customer should always exist and for some reason it doesn't, so I'm going to notify user saying that something exceptional happened". On other hand I can assume that it's possible for a customer to be removed by another person, therefore I need to handle this gracefully.

无论哪种方式,我认为我们都需要一个 MissingCustomer 类或一个 MissingCustomerException,因为客户是一个在整个系统中使用的非常常见的实体.如果视图模型需要一个客户并且我们返回一个 MissingCustomer - 这很好,因为继承将使这个工作.

Either way I think that we'll need a MissingCustomer class or a MissingCustomerException as customer is a very common entity which is used throughout the system. If the view model expects a customer and we return a MissingCustomer - it's fine as inheritance will get this working.

例如,我有一个返回 OrderViewModel 的操作方法.此操作方法需要对客户的引用.

For example I have an action method that returns OrderViewModel. This action method requires a reference to a customer.

Customer customer = CustomerRepository.Find(10);
if(customer == null)
{
    return new MissingCustomer();
}

这会失败,因为 action 方法将返回 OrderViewModel 类型的视图模型,所以现在我更倾向于使用异常,而不是 MissingCustomer 对象.

This is going to fall over because action method will return a view model of type OrderViewModel, so now I'm more inclined towards using an exception, instead of a MissingCustomer object.

编辑

此外,MissingCustomer 类型对象将从 Customer 类型继承属性.不需要这些属性,因为我们唯一想做的就是通知用户找不到客户.

Additionally, a MissingCustomer type object would inherit properties from Customer type. These properties are not needed as the only thing we want to do, is notify users that customer can't be found.

谢谢

推荐答案

我总是会从用于获取客户的方法中返回 null.仅查看名为 GetCustomer 的函数,您永远不会期望它可以返回一个不是真正客户的客户对象.

I would always return null from methods used to fetch customers. By just looking at a function named GetCustomer one would never expect that it can return a customer object which is not really a customer.

使用类似 var customer = repos.GetCustomer(1) ??Customer.Empty 如果可以的话.意图很明确,代码更不容易出错.

It's far better that you use something like var customer = repos.GetCustomer(1) ?? Customer.Empty if you can. The intent is clear and the code becomes less error prone.

如果您的方法希望始终获得客户,那么您之前未能验证输入.与其创建解决方法来使代码正常工作,不如尝试更早地修复它.也许检查一下客户是否存在?

If your method expects to always get a customer you have failed to validate the input earlier on. Instead of creating a workaround to get the code working, try to fix it earlier on. Maybe a check to see if the customer exists?

更新

我现在注意到问题确实是针对您的视图模型.在其中使用这种逻辑完全没问题.视图模型毕竟用于调整 MVC 中的M"以适应视图(因此从视图中删除逻辑).

I noticed now that the question really was for your view model. It's perfectly fine to use that kind of logic in it. The view model after all used to adapt the "M" in MVC to suit the view (and therefore remove logic from the view).

我会用

public class YourViewModel
{
    public Customer Customer { get { return _customer ?? Customer.Empty; }}
}

这篇关于异常与临时类型.什么可能会倒下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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