Nhibernate 4 API documentation

耗尽温柔 提交于 2019-12-11 10:36:18

问题


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?


回答1:


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



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/29729683/nhibernate-4-api-documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!