This project has moved and is read-only. For the latest updates, please go here.

Work Item # 854 Criteria class mode issue

Jan 25, 2012 at 11:07 PM

Hi Richard,

How come you have two Fetch methods? Are you using two different get criteria? Why don't you use one SearchCriteria with multiple properties? CSLA.NET doesn't need a proper criteria to pass to the DataPortal. As long as the CslaGenFork criteria has only one parameter, that parameter is passed to the data portal on its own. Criteria are only used when you have more than one parameter. On the later case, each parameter should be nullable so you can use one or the otherparameter or even all at the same time.

Jan 26, 2012 at 9:51 AM

Hi Tiago

Yes I am getting by Post Code and School Name in this instance - both strings.  I've just checked again and I can see that a criteria object is created when you have more than one criteria parameter.  But if I do need to have two same type search criteria are you suggesting then I have one SearchCriteria with multiple properties then branch (use logic code) to handle in the DataPortal.



Jan 29, 2012 at 9:36 AM
Edited Jan 31, 2012 at 9:32 PM

Hi Richard,

What I usually do is:

  1. (On the UI) I have a single "SearchCriteria"
  2. (On the UI) all criteria parameters are nullable types
  3. The SearchCriteria object is passed to the DataPortal Fetch method and all criteria properties are nullable
  4. The DataPortal Fetch method passes all properties/parameters to the stored procedure
  5. The stored procedure queries the database in a special way so that null parameters are ignored

The later point isn't a very good solution since it involves "null" handling. By my experience all "null" handling isn't very efficient. One of the reasons I want to have inline SQL queries instead of stored procedures is to solve this problem. Generated code would include the parameter in the query only if it's not null:

if (criteria.CountryID != null)
    cmd.CommandText += "AND CountryID = @CountryID" + Environment.NewLine;
    cmd.Parameters.AddWithValue("@CountryID", crit.CountryID.Value).DbType = DbType.Int32;

In what concerns "string" and "SmartDateTime" they aren't nullable types but can be null. They would be handled the exact same way as the example above.