Example #1
0
        public void Does_not_add_null_valued_criteria()
        {
            Criterion nullCriterion = new Criterion("A1", null, ComparisonOperator.Equal);

            CriterionSet set1 = new CriterionSet();

            set1.AddCriterion(nullCriterion);

            Assert.That(set1.GetCriteria(), Is.EqualTo(new Criterion[] { nullCriterion }));
        }
Example #2
0
        public void Can_add_criterion()
        {
            CriterionSet set = new CriterionSet();

            Criterion criterion1 = new Criterion("A1", "V1");
            Criterion criterion2 = new Criterion("A2", "V2");

            set.AddCriterion(criterion1);
            set.AddCriterion(criterion2);

            Assert.That(set.GetCriteria(), Is.EquivalentTo(new Criterion[] { criterion1, criterion2 }));
        }
Example #3
0
        public IEnumerable <T> FindAll <T>(CriterionSet criterionSet)
        {
            using (ISession session = GetSession())
            {
                var persistentObjects = new List <T>();

                var criteria = session.CreateCriteria(typeof(T));
                criteria.SetCacheable(true);

                foreach (var criterion in criterionSet.GetCriteria())
                {
                    ICriterion expression;

                    if (criterion.Operator == ComparisonOperator.GreaterThan)
                    {
                        expression = Restrictions.Gt(criterion.Attribute, criterion.Value);
                    }
                    else if (criterion.Operator == ComparisonOperator.LessThan)
                    {
                        expression = Restrictions.Lt(criterion.Attribute, criterion.Value);
                    }
                    else if (criterion.Operator == ComparisonOperator.NotEqual)
                    {
                        if (criterion.Value == null)
                        {
                            expression = Restrictions.IsNotNull(criterion.Attribute);
                        }
                        else
                        {
                            expression = Restrictions.Not(Restrictions.Eq(criterion.Attribute, criterion.Value));
                        }
                    }
                    else
                    {
                        if (criterion.Value == null)
                        {
                            expression = Restrictions.IsNull(criterion.Attribute);
                        }
                        else
                        {
                            expression = Restrictions.Eq(criterion.Attribute, criterion.Value);
                        }
                    }

                    criteria.Add(expression);
                }

                if (criterionSet.OrderBy != null)
                {
                    if (criterionSet.SortOrder == SortOrder.Descending)
                    {
                        criteria.AddOrder(Order.Desc(criterionSet.OrderBy));
                    }
                    else
                    {
                        criteria.AddOrder(Order.Asc(criterionSet.OrderBy));
                    }
                }

                var list = criteria.List();
                foreach (T entity in list)
                {
                    persistentObjects.Add(entity);
                }

                return(persistentObjects);
            }
        }