如果.Create()不能实例化,应该返回空对象,null还是抛出异常? [英] If .Create() can't instantiate, should it return empty object, null, or throw an exception?

查看:118
本文介绍了如果.Create()不能实例化,应该返回空对象,null还是抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用这种代码来实例化我的应用程序中的任何对象:

  SmartForm smartForm = SmartForm。 Create(id = 23); 
客户客户= Customer.Create(id = 222);

如果该对象不存在,我正在讨论Create()应该返回的内容。 >


  • 如果Create()返回一个空对象,那么这是一种空模式,而我仍然可以传递该对象在我的应用程序和调用方法,这使得编程与此模型方便和简单


  • 如果Create()返回 null ,那么如果对象等于null,则需要在每个实例化之后检查,这使得编程有点更繁琐但更明确。这样做的一个问题是,如果您忘记检查null,那么您的应用程序可能会工作很长时间,而不知道您没有检查为null,然后突然断开


  • 如果Create()引发异常,它基本上与返回null相同,但是通过让您创建一个尝试,使编程变得更加乏味,接下来,最后阻止每个实例化,但是您可以抛出各种类型的异常(您无法使用null解决方案),这可能会冒泡,以便您可以更明确地处理UI上的深层错误,因此我认为是最强大的解决方案,尽管产生try / catch代码膨胀




所以似乎是一个亮度/鲁棒性的折衷是否有任何人有根据这些决定作出决定的经验,因为您有这样的决定吗?

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;

命名空间TestFactory234.Models
{
public class SmartForm:Item
{
public string IdCode {get;组; }
public string标题{get;组; }
public string描述{get;组; }
public int LabelWidth {get;组;

public SmartForm(){}

private SmartForm(string loadCode)
{
_loadCode = loadCode;
TryToInstantiateFromDatabase();
}

public static SmartForm Create(string loadCode)
{
SmartForm smartForm = new SmartForm(loadCode);

if(!smartForm.IsEmpty())
{
return smartForm;
}
else
{
返回null;
}
}
}
}


解决方案

这取决于 - 如果它失败了,因为某些东西绝对是错误的,那么一个异常使编程更容易 - 你不会在每个呼叫周围写一个try / catch,你只是让异常起泡。比较,检查一个空/空返回值和然后抛出异常。



这听起来像是正确的时间使用 ArgumentException ,IMO。



如果您发现自己创建了try / catchbloat,请查看为什么你真的需要抓住例外,而不是让他们起泡。


I want to be able to instantiate any object in my application with this kind of code:

SmartForm smartForm = SmartForm.Create("id = 23");
Customer customer = Customer.Create("id = 222");

I'm now debating what Create() should return if that object does not exist.

  • if Create() returns an empty object, then this is kind of a "null pattern" and I can still pass that object around my application and call methods on it, which makes programming with this model convenient and easy

  • if Create() returns null, then I need to check after every instantiation if the object equals null or not, which makes programming a bit more tedious but more explicit. A problem with this is that if you forget to check the null, your application could work for a long time without you knowing that you're not checking null, then break all of a sudden

  • if Create() throws an exception, it is basically the same as returning null but makes programming even more tedious by making you create a try, next, finally block for each instantiation, but you could throw various types of exceptions (which you can't with the null solution) which could bubble up so that you could more explicitly handle the deep errors on your UI, so this I would think is the most robust solution although would produce try/catch code bloat

So it seems to be a lightness/robustness trade off. Does anyone have any experience of making a decision along these lines where you experienced advantages or disadvantages because of that decision?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestFactory234.Models
{
    public class SmartForm : Item
    {
        public string IdCode { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int LabelWidth { get; set; }

        public SmartForm() { }

        private SmartForm(string loadCode)
        {
            _loadCode = loadCode;
            TryToInstantiateFromDatabase();
        }

        public static SmartForm Create(string loadCode)
        {
            SmartForm smartForm = new SmartForm(loadCode);

            if (!smartForm.IsEmpty())
            {
                return smartForm;
            }
            else
            {
                return null;
            }
        }
    }
}

解决方案

It depends - if it fails because something is definitely wrong, then an exception makes programming easier - you don't write a try/catch around each call, you just let the exception bubble up. Compare that with checking for a null/blank return value and then throwing an exception.

This sounds like the right time to use ArgumentException, IMO.

If you find yourself creating try/catch "bloat", have a look at why you really need to catch the exceptions rather than just letting them bubble up.

这篇关于如果.Create()不能实例化,应该返回空对象,null还是抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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