简体   繁体   中英

Nhibernate 4 API documentation

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

  • eg 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?

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;
    }

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;
        }
    }
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM