by Matthew Noonan
9. November 2006 20:02
This always bugged me at the back of my mind, but I could never put my finger on it. When writing LIKE queries in EasyObjects, how come I have to include the wildcard character in the value? See this example:
obj.Where.LastName.Value = "J%";
obj.Where.LastName.Operator = WhereParameter.Operand.Like;
obj.Query.Load();
Isn't EO supposed to be database-neutral, with different dynamic query providers for each database? Won't my query break if I move to a platform that uses a different wildcard character?
Once I managed to identify the problem, the solution became immediately obvious: developers should not specify the wildcard character when doing LIKE queries. So I have added the following Operands to the 1.2 release of EasyObjects:
- StartsWith
- EndsWith
- Contains
- NotStartsWith
- NotEndsWith
- NotContains
So now, the first example looks like this:
obj.Where.LastName.Value = "J";
obj.Where.LastName.Operator = WhereParameter.Operand.StartsWith;
obj.Query.Load();
This is a far better solution for writing portable queries. The DynamicQueryProvider class will add the proper wildcard character for the database and in the correct position(s) to perform the query. The LIKE and NOT LIKE operands will still be supported for backwards compatibility, but you should move to the newer constructs so your queries can be more easily ported across platforms.
Note: For those of you quick on the uptake, yes, I know that Oracle and SqlServer both use the same wildcard character. Those will not be the only 2 platforms forever. I am working on it. 