RockfordLhotka replied on Tuesday, October 10, 2006
yipchunyu: I read the book but can't understand fully. How should I make use of the data fetch with criteria.
For example, followings are the codes inside part of the ProjectList object. How should I e.g
1. Add some parameters?
2. Multiple set of criteria e.g. search by Name and Author / Just search by ID
The Criteria class defines your parameters. In ProjectList there are actually two different Criteria classes, because you can get the complete list (the Criteria with no fields) or you can get a filtered list (the Criteria with the name field for filtering).
If your object has different parameters, then you would create a Criteria class that contains those parameters.
Typically you will have one Criteria class for each different factory method, so you can clearly indicate which overload of DataPortal_Fetch() should be called by that factory method.
If you need
optional parameters, then you need to make your Criteria class a little more complex. There are many ways to do this, but I prefer to keep a Boolean flag along with each criteria field, using that flag to indicate whether the field has been set.
(though you could probably use Nullable<T> to track whether each field was set - that'd be interesting to try)
This way, in your DataPortal_Fetch() method, you can easily determine which criteria fields were set, and so you can create your WHERE clause or call the right stored procedure or whatever.
JoeFallon1 replied on Tuesday, October 10, 2006
In addition to what Rocky said, I would pass the filter to the SP or SQL Statement so that the Database returns a smaller rowset over the wire to the BO. Rather than retrieve the entire DB and then filter out those records I don't want. Rocky's sample app is just showing one way to do it - it isn't necessarily the best way to do it. <g>
Joe