February 19, 2010 NHibernate, QuickTip No comments
February 19, 2010 NHibernate, QuickTip No comments
What do you see to be wrong with this code?
.Add(Restrictions.Eq(“r.Name”, ruleName))
//other restrictions
.List();
return result.Count > 0;
}
At first I did not see any issues with it so I copied it and changed a bit for my another query. But current method is wrong. I discovered this with UTs.
First, Projections.RowCount() generates COUNT(*) in select statement.
This is query, which I got from my new Unit Tests: RuleExists_HitsDatabase_ThereIsNoRule.
Result of this query is number of rows like on picture below:
So, verification return result.Count > 0; is absolutely incorrect.
I’ve chagned it to return (int)result[0] > 0;
Moral:
Do not be lazy to write Unit Tests both for success and failure sceneries.
February 15, 2010 NHibernate, QuickTip No comments
ORDER BY
To add “order by” to your criteria you need this statement.
.AddOrder(Order.Asc(“Priority”))
TOP
To add “top 10” to your criteria you need this statement
.SetMaxResults(10)
Criteria
So code about which I’m talking could look like:
SetResultTransformer or “Why did I get 5 results instead of 10?”
So you expect to have top 10 priority Customers with status Created.
In scope of my current task it was needed to add priority to this query, so I decided to unit test it of course.
In Debug I found that there are actually 5 results in resulting collection.
That is because generated SQL generates result which contains duplicated CUSTOMER_IDs, that is because I have join-s there. But then why did not I get 10 duplicated Customers? Because query has ResultTransformer which is applied after SQL has been ran. (That is 100% since I took a look at generated SQL via NHibernateProfiler).
So .SetResultTransformer(new DistinctRootEntityResultTransformer()) is removing of all duplicated entries of my root entity (Customer).
Good explanation to this you can find here.
January 26, 2010 QuickTip, UI, WindowsForms No comments
Do you want to see your button to be in pressed or unpressed state when you hit it once?
Did you catch that this behavior is not really button’s behavior – it is more checkBox’s behavior.
So to accomplish your goal you need move checkBox to your form and set appearance from “Normal” to “Button”. Like here:
Kick me if you don’t want to see such posts on my blog. :)