How to handle fetching a non-existant object

How to handle fetching a non-existant object

Old forum URL: forums.lhotka.net/forums/t/6458.aspx


cds posted on Wednesday, February 18, 2009

I have a Root object that I try to fetch using some criteria, but it's not in the database.

At the moment, it returns an empty object, but how do I distinguish the empty object, from a valid object with empty fields. Is there a standard way of indicating this?

Thanks,

Craig

RockfordLhotka replied on Wednesday, February 18, 2009

There's a rather lengthy thread on this topic in the forum archive.

As a general rule I recommend throwing an exception when data doesn't exist for an object.

Other people return a null.

Other people return an empty object - and come up with a way to identify what is "empty".

I'd say that this third option is the least common, because it is nebulous and there's no global concept of "empty objects" other than null.

SonOfPirate replied on Thursday, February 19, 2009

To support both operations, in our base Criteria class, we've added a SuppressException property that is used to determine if an exception if thrown (when false) or if null is returned (when true).  This allows you to decide which implementation is appropriate for your needs on a class-by-class basis (or even a use-by-use basis).

 

JoeFallon1 replied on Thursday, February 19, 2009

This has been discussed many times.

I return a New object if an Existing one is not found.

But if I could start my project from scratch I would throw the exception and catch it in my UI and tell the user that the data they are looking for does not exist and ask them to change their search criteria to something else.

Joe

 

cds replied on Thursday, February 19, 2009

Thanks for the advice guys.

In the end I went for throwing an exception and catching it at the UI end.

Craig

Copyright (c) Marimer LLC