QuickTip

NHibernate: Projections.RowCount()

February 19, 2010 NHibernate, QuickTip No comments

 What do you see to be wrong with this code?

        public bool RuleExists(string ruleName, string documentName)
        {
            var result = Session.CreateCriteria(typeof(Rule)“r”)
                //some aliaces 
             
                .SetProjection(Projections.RowCount())

                .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.

SELECT count(* ) AS y0_
FROM   TBL_RULE this_
       INNER JOIN — some joins here
     
WHERE  this_.NAME = ‘absolultely_incorrect_rule_name’ /* @p0 */
       AND — other conditions 

Result of this query is number of rows like on picture below:

So, verification return result.Count > 0is 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.


No comments


Few NHibernate hints on Query Criteria

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:

        public IList<Customer> FetchTopPriorityCustomers()
        {
            var result = Session.CreateCriteria(typeof(Customer))
                .SetFetchMode(“CustomerStatusType”FetchMode.Eager)
                .Add(Expression.Eq(“CustomerStatusType.CustomerStatusTypeID”, (Int32)CustomerStatusType.Created))
                .SetResultTransformer(new DistinctRootEntityResultTransformer())
                .AddOrder(Order.Asc(“Priority”))
                .SetMaxResults(10)
                .List<Customer>();
            return result;
        }

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.


No comments


Button in pressed/unpressed state in System.Windows.Forms

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:

System.Windows.Forms.CheckBox checkBox1 = new System.Windows.Forms.CheckBox();
checkBox1.Appearance = System.Windows.Forms.Appearance.Button;

Kick me if you don’t want to see such posts on my blog. :)


No comments