It's a girl

Let us take the following form, the like of which has been known to cause grown man to cry. The client want something "very simple": I just want to be able to search on my policies, and this is the stuff that I think that I need now, and I'll need more in the future.

Just a hint on the data model as relevant to this search form, it goes something like this:

Polic:

M:M - Clients:

1:1 Primary Address

1:M - Claims:

1:M Payments

Account:

1:M - Policies

Note that I make no promises that this is a viable model for insurance, I just needed a complex domain and I can't use my usual Employees > Salaries example, since it would hit too close to the real issue that I did.

Anyway, the problem with this kind of fomrs is that they are complex beasts. I have seen search forms that were two pages long, and were accompanied with a manual (just for the search form) that was bigger than the entire system specification. The real kicker here is that there isn't a single path that the user is going through, the system should be able to handle any combination of search terms, and ignore any that isn't relevant.

There is additional complexity added by the fact that this data is not sitting in the same table, actually, just from the rough data modle above, it looks like it is sitting in no less than 6 tables.

This is also the place where the Stored Procedure approach hurts the most, in my experiance.

[Despite its size, this is still just first attempt, with no checks for user input, etc]

What we have here is a Finder object, which the UI uses. It is not the job of the UI to make searches, especially not searches this complex. If this is more than a one liner that can be expressed with NHQG, I tend to create a finder object for this, which will contain all the logic for the search. All the properties on finder are nullables, since they are all optionals...

The interesting part here is in the end, where we use a subquery to check for the existance of a client that match the descriptions.

privatevoidAddPolicyQuery(DetachedCriteriaquery)

{

if(policyType.HasValue)

{

query.Add(Expression.Eq("Type", policyType.Value));

}

if(policyStatus.HasValue)

{

query.Add(Expression.Eq("Status", policyStatus.Value));

}

}

Nothing interesting above, but the next one is interesting:

privatevoidAddClaimsQuery(DetachedCriteriaquery)

{

DetachedCriteriaclaimCriteria = null;

if(claimNumber.HasValue)

{

claimCriteria = query.CreateCriteria("Claims");

claimCriteria.Add(Expression.Eq("Id", claimNumber.Value));

}

if(claimStatus.HasValue)

{

claimCriteria = claimCriteria ?? query.CreateCriteria("Claims");

claimCriteria.Add(Expression.Eq("Status", claimStatus.Value));

}

if(paymentNumber.HasValue)

{

claimCriteria = claimCriteria ?? query.CreateCriteria("Claims");

claimCriteria

.CreateCriteria("Payments", "payment")

.Add(Expression.Eq("Id", paymentNumber.Value));

}

}

Notice the use of CreateCriteria to join to other tables with ease, so we can check them as well.

privatevoidAddAccountQuery(DetachedCriteriaquery)

{

if(accountSize!=AccountSize.None)

{

ICriterionaccountSizeExpr=null;

foreach (AccountSizevalueinEnum.GetValues(typeof(AccountSize)))

{

if((accountSize & value ) == value)

{

if(accountSizeExpr==null)

{

accountSizeExpr = Expression.Eq("Size", value);

}

else

{

accountSizeExpr = Expression.Or(

accountSizeExpr,

Expression.Eq("Size", value));

}

}

}

query.CreateCriteria("Account")

.Add(accountSizeExpr);

}

}

And this example shows the fun you can have with a search API :-) Now all we have to do is...

privatevoidAddDateRangeQuery(DetachedCriteriaquery)

{

if(startDate.HasValue)

{

query.Add(Expression.Le("Range.Start", startDate.Value));

}

if(endDate.HasValue)

{

query.Add(Expression.Ge("Range.End", endDate.Value));

}

}

And we are basically done. We still need to test this finder, and to see that it give us the correct results, of course (which I hadn't done in this case, by the way). The advantages of this approach is that the finder is easily extensible in the future, if the customer would like me to give him additional fields, I can simply add it to the UI and then add additional querying to the finder. The nice thing about it is that I am not concating strings, so I can just play with it just about everywhere I want, and let NHibernate figure out the final query.

One thing to be aware of is that the final SQL query is probably going to be fairly involved if all the filterring are going to be used, but that is not an expected usage for this kind of screen.