I have the same problem. nHibernate just complicates things imho and there's almost no documentation of how Able Gold has implemented it. So we're left with begging for scraps of wisdom or hoping someone else solved the problem for us AND had the courage to post it in a public forum.
Working examples speak volumes to me. Here's an example of how my Quickbooks module pulls Order data using nHibernate criteria. Based on what I've seen thus far in Gold, this concept will work with all the Able datasource classes.
First you must have the correct references at the top of your page. I always use:
Code: Select all
using CommerceBuilder.DomainModel;
using NHibernate;
using nhc = NHibernate.Criterion;
Why do I always use that? Because that's what I was told. I have no clue why every single page in a data-driven software application still has to manually include references to the data access engine. You'd think it would be included by default. /shrug
Once you've got the correct references, now you're ready to build a criteria object. The criteria object holds all the search rules you want to pass to nHibernate. Again this structure should work for most every able data object class. So if you want to do this on a different object class, just change <Order> to <ClassName>.
Code: Select all
NHibernate.ICriteria criteria = NHibernateHelper.CreateCriteria<Order>();
Now you've got a criteria object, but nothing else. So we need to add some rules to the object. nHibernate probably calls them something else. I call them rules. The first rule I use is global i.e. it applies to the entire query.
Code: Select all
criteria.Add(nhc.Restrictions.Eq("Exported", false));
So the above statement is the equivalent SQL of "SELECT * FROM ac_Orders WHERE Exported = false"
The nhc.Restrictions is where the action occurs. .Eq means "EQUAL". There are plenty of others and you can google for examples. I liked this page because it compared nHibernate queries to the actual generated SQL for easy reference:
http://ayende.com/blog/4023/nhibernate-queries-examples
Now that I know I've included all orders with Exported = false, I add a few conditional rules based on configuration settings in my Quickbooks module. Note how the rules are added as items in a collection.
nHibernate discriminates OR and AND with using CONJUNCTION and DISJUNCTION. The default is CONJUNCTION (AND), so if you don't load the rule into a DISJUNCTION then it's going to assume it's an AND relationship. Lovely terminology eh?
This example below shows how I load both an AND relationship and optionally load both an additional AND with an OR relationship using the DISJUNCTION (case 2:).
Code: Select all
// check store setting for respecting paid/shipped flags
switch (ConfigSettings.RespectPayShip)
{
case 0: // when placed
break;
case 1: // when paid
criteria.Add(nhc.Restrictions.Eq("PaymentStatusId", (byte)2));
break;
case 2: // when paid and shipped
criteria.Add(nhc.Restrictions.Eq("PaymentStatusId", (byte)2));
criteria.Add(nhc.Restrictions.Disjunction()
.Add(nhc.Restrictions.Eq("ShipmentStatusId", (byte)2))
.Add(nhc.Restrictions.Eq("ShipmentStatusId", (byte)3))
);
break;
}
Finally, you are ready to run the actual query. That part is simple.
Code: Select all
// execute your query
IList<Order> _Orders = criteria.List<Order>();
There's no doubt that the implementation of nHibernate complicates things. And it makes the exception errors look enormous and difficult to interpret. Hopefully Able will start posting some working code examples to give everyone a chance to succeed with their own customizations.
I hope this helps you along with your project.