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?
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.
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).
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.
Thanks for the advice guys.
In the end I went for throwing an exception and catching it at the UI end.
Copyright (c) Marimer LLC