synnefo/delphi-orm

TSession.FindOne return nil instead of raising exception

Closed this issue · 2 comments

TSession.FindOne raises an exception whenever the query return zero or more 
than one object.

I agree that when it returns more than one object, an exception must be raised 
because it's realy an exceptional case.

But when no object is found, I think it's better to return nil instead, because 
this is a normal case. For exemple, during login I can search for the user on 
the database based on the user login:
User := FSession.FindOne<TUser>(TdormCriteria.Create.Add('Login', Equal, 
TValue.From(EditLogin.Text)));

And it's a pretty normal situation if the object that I'm looking for doesn't 
exist.

But when that happens, with the current behaviour, I can't properly handle the 
situation because the same exception is raised both when no record is found and 
when more than one record is found.

Also, I noticed that the two overloads of FindOne have different behaviours 
when the search returns no object.
The overload that takes a Type Info parameter seems to return nil when no 
object is found
But the overload that takes a generic argument raises the exception when no 
object is found.
Is it the designed behaviour of both methods? I thought that the overload with 
the generic argument existed only to free us from doing type casts

Original issue reported on code.google.com by magn...@gmail.com on 17 Nov 2011 at 8:35

I'm sending a patch, which includes:
1. Changed TSession.FindOne<T>() to delegate to TSession.FindOne(). This 
reduced a bit of duplicated code (as both methods were almost identical), and 
will ensure that they will always behave the same.
2. Because of "1", now TSession.FindOne<T>() return nil instead of raising an 
exception when no object is found
3. Tests for both methods
4. Changed the exception message raised on TSession.FindOne

Original comment by magn...@gmail.com on 18 Nov 2011 at 2:09

Attachments:

Thank you Magno. I' applied the path to the trunk. Asap I'll test it on the dev 
branch.

Original comment by daniele....@gmail.com on 22 Nov 2011 at 5:28

  • Changed state: Fixed