1

I am not able to find which namespace contains what for methods.

  • e.g. NHibernate.IQueryOver does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type .

Visual studio is not helping to get appropriate method on using because of extension method.

How can I know which methods, namespaces should be included?

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
dkt
  • 144
  • 2
  • 13
  • Are you sure there is such a method? – Oskar Berggren Apr 19 '15 at 12:29
  • http://stackoverflow.com/a/24817491/4803619 – dkt Apr 19 '15 at 12:34
  • @veshu In the example given, Add is a method of the object returned by `Restrictions.Disjunction()`, it isn't part of the "standard" syntax of QueryOver – xanatos Apr 19 '15 at 12:37
  • My problem is if I copy paste the same code on my vs, it shows there is no where method. Its not just about Add. When I type queryOVer and dot there I can't see any method like where, selectList, select etc.. – dkt Apr 19 '15 at 12:39
  • @veshu the namespace for `QueryOver` is `NHibernate.Criterion` – xanatos Apr 19 '15 at 12:45

3 Answers3

2

In case, that we want to pass QueryOver into another method, and execute some filtering over it, we MUST know the type, which is passed.

Below snippet shows, that we have some known interface IBusinessObject, which has ID and Name. That could help use to create where conditions for our generic params T and U - and apply some stuff related to that interface:

using NHibernate.Criterion;

namespace MyNamespace
{
    public interface IBusinessObject
    {
        int ID { get; }
        string Name { get; }
    }

        public static QueryOver<T, U> AddSomeFilter<T, U>(QueryOver<T, U> queryOver)
            where T: IBusinessObject
            where U: IBusinessObject
        {
            // here we can play with ID, and Name
            // because we know that U is of a type IBusinessObject
            queryOver
                .Where(x => x.ID > 1)
                .Where(x => x.Name == "Abc");

            return queryOver;
        }
    }
}

This could be fine, but it could lead to some dependency. That's why I honestly do like to use Criteria API. We can pass some Metadata, and create more dynamic processors:

public static class MyExtension
{
    public static ICriteria AddLike(ICriteria criteria, string property, string likeValue)
    {
        if (property.IsNotEmpty())
        {
            criteria.Add(Restrictions.Like(property, likeValue));
        }
        return criteria;
    }

To process the method in the comment, we can do it like this:

public class SearchCriteria
{
    public string PropertyName { get; set; }
    public string LikeValue { get; set; }
}

public static class MyExtension
{
   public static IQueryOver<Employee, Employee> ConstructQueryConditions(
        this IQueryOver<Employee, Employee> query
        , SearchCriteria criteria)
    {
        if (criteria.PropertyName.IsNotEmpty())
        {
            query.Where(Restrictions.Like(criteria.PropertyName, criteria.LikeValue));
        }
        return query;
    }
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • IQueryOver ConstructQueryConditions(IQueryOver query, SearchCriteria criteria). Can you please update the answer with this signature. – dkt Apr 19 '15 at 13:25
  • I hope, that extended answer is clear. We have some DTO SearchCriteria... which we can easily process in our ConstructQueryConditions... Again, we rather use "string driven" Criteria API (Restrictions) to add Like clause ... – Radim Köhler Apr 19 '15 at 13:30
  • One more thing how to do OR. eg. in contructQeryConditions method i can do : query.And(Criterion instance) but there is no OR method, IQueryOver And(ICriterion expression); but why not IQueryOver OR(ICriterion expression);, how to accomplish OR? – dkt Apr 19 '15 at 15:21
  • yes I looked that ans, but it takes lhs and rhs parameter but what I have is criteria using different SearchCriteria DTO. I also get an extension method from http://www.methodicmadness.com/2012/04/extending-queryover-with-or.html. Do you think it is good? thanks – dkt Apr 19 '15 at 16:21
  • Wish to assist, but not sure what is not working. If you think the links are not working for you, rise new question and describe what you have and what is not working. The `OR` statement is really not so complicated.. just requires help of Restrictions object... – Radim Köhler Apr 19 '15 at 16:24
  • @veshu I also fixed one bug in my answer: http://stackoverflow.com/a/24817491/1679310 and assured that now it is working – Radim Köhler Apr 19 '15 at 16:34
  • few more things, like 1. while creating product list , i don't want to create products in db, instead products will be already saved in db and later we will be creating product lists. so for this I have used cascade, am I using them properly? 2. also not able to configure dynamic component in product class(properyt is attributes) I want to save them in productExtension table. Thanks. – dkt Apr 20 '15 at 18:50
  • Ok, but actually I was going to submit that code today within 3 hours. Anyway it can be reference for me for future :-( – dkt Apr 21 '15 at 06:39
  • It is really out of my abilities to provide you more - then the summary in this answer - http://stackoverflow.com/a/29765136/1679310. Hope this helps. Simply, you should read more. Go through the documentation and try to read it, to get idea what we can use. – Radim Köhler Apr 21 '15 at 07:04
1

To create QueryOver for entity like Employee, we would need only ISession, and reference to entity

// using for below query
using System;
using MyProject.Entity;
using MyProject.Data; // to get session

with above using, we can have this query

...
var session = ... // get session
Employee empl = null;

var employee = session
    .QueryOver<Employee>()
    .Where(x => x.ID > 1)
    .SelectList(list => list
        .Select(x => x.ID)
        .Select(x => x.FirstName)
        .Select(x => x.LastName)
    )
    .Skip(10)
    .Take(10)
    .List<Employee>();

To use helpers like Restrictions, we need

using NHibernate.Criterion;

which will give us access to Restrictions, Projections, QueryOver.Of()

var disjunction = Restrictions.Disjunction();
var projection = Projections.Sum("Age");
var detachedQuery = QueryOver.Of<Employee>();

Summary:

  1. to get access to helpers, we need NHibernate.Criterion.
  2. To have access to QueryOver API - we need just QueryOver instance. Because these methods are not extensions, they are methods of it...

In case, we want to pass QueryOver, we just have to reference Criterion:

using NHibernate.Criterion;

namespace MyNamespace
{
    public static class MyExtension
    {
        public static QueryOver<T, U> AddPaging<T, U>(QueryOver<T, U> queryOver)
        {
            queryOver
                .Skip(10)
                .Take(10);

            return queryOver;
        }
    }
}
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Nice, got this. What if, say you want to pass the queryOver instance to a method which will add some criteria and return. How would we do that ? – dkt Apr 19 '15 at 13:02
  • I just updated my comment, can you please check once. – dkt Apr 19 '15 at 13:05
  • If we want to work with `QueryOver` we can just add one namespace `using NHibernate.Criterion;` That's it. From that moment we can fully operate with QueryOver, passed as a reference. – Radim Köhler Apr 19 '15 at 13:05
  • Check my extended answer... API of `QueryOver` is powerful, and could be passed. **BUT Project MUST be referencing NHibernate.** Then - only `NHibernate.Criterion;` will do – Radim Köhler Apr 19 '15 at 13:09
  • Just two small things: `var session = ... // get session` should probably be `ISession session` to make clear the type of session, and in the necessary `using`, probably `using NHibernate` to be able to use `ISession` – xanatos Apr 19 '15 at 13:12
  • awesome you just read my mind. that's what I was going to ask and was writing in forum. ;-) Ok I can do skip and take but why not selectList /select or where or add in this method. like queryOVer.Where(c=>1==1). – dkt Apr 19 '15 at 13:15
  • This is finally the CORE question... So, the answer is... so complex, that I added new answer with more details – Radim Köhler Apr 19 '15 at 13:17
0

If I can't find a method that I know is somewhere, what I normally do is use ILSpy to take a peek at the dll. You run it, remove all the "default" assemblies, drag-n-drop only the asseblies you need (for example the ones of nhibernate), then View->Search and if you know the type name you select Type in the combo box, if you know the method name you select Member.

xanatos
  • 109,618
  • 12
  • 197
  • 280