例外VS即席类型。什么是可能翻倒? [英] Exception vs ad-hoc type. What is likely to fall over?

查看:96
本文介绍了例外VS即席类型。什么是可能翻倒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读的企业应用程序开发的书上的MVC 3项目时。我目前正在决定如何处理异常。 previously我会让异常泡沫堆栈,然后在最高层处理。

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.

这本书提出创建一个域模型的ad-hoc类,并返回。例如。

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();

我可以看到的想法,但我竭力辩解需要这样。 code明智的我同意,这是一个很大整洁返回一个新的失踪客户,而不是抛出异常。

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();
}

这是要摔倒,因为操作方法将返回类型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类型的对象将继承从客户类型属性。这些特性并不需要,因为我们想要做的唯一事情,就是通知用户客户无法找到。

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.

感谢您

推荐答案

我总是会从用来获取客户的方法返回。通过看一个名为函数 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的客户= repos.GetCustomer(1)? Customer.Empty 如果你能。这样做的目的是明确的,code变得不那么容易出错。

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.

如果你的方法希望总是得到你未能早些时候验证输入一个客户。而不是创建一个解决方法,以获得$ ​​C $ C工作的,尝试修复它早些时候。也许一个检查,看看是否存在客户?

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; }}
}

这篇关于例外VS即席类型。什么是可能翻倒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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