Nhibernate 4 API文档

时间:2015-04-19 12:12:17

标签: c# visual-studio-2012 nhibernate queryover nhibernate-4

我无法找到哪个命名空间包含方法的内容。

  • e.g。 NHibernate.IQueryOver不包含“添加”的定义 没有扩展方法'添加'接受第一个类型的参数。

由于扩展方法,Visual Studio无法获得适当的使用方法。

我如何知道应该包含哪些方法名称空间?

3 个答案:

答案 0 :(得分:2)

如果我们想将QueryOver传递给另一个方法,并对其执行一些过滤,我们必须知道传递的类型。

在以下代码段显示,我们有一些已知的界面 IBusinessObject ,其中包含ID和名称。这可能有助于为我们的通用参数T和U创建where条件 - 并应用与该接口相关的一些内容:

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

这可能没问题,但可能导致一些依赖。这就是我老实说喜欢使用Criteria API的原因。我们可以传递一些元数据,并创建更多动态处理器:

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

要在评论中处理方法,我们可以这样做:

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

答案 1 :(得分:1)

要为 QueryOver 等实体创建Employee,我们只需要ISession,并引用实体

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

以上使用,我们可以有这个查询

...
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>();

要使用限制条件等帮助程序,我们需要

using NHibernate.Criterion;

可让我们访问RestrictionsProjectionsQueryOver.Of()

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

摘要:

  1. 要获得帮助,我们需要NHibernate.Criterion。
  2. 要访问QueryOver API,我们只需要QueryOver个实例。因为这些方法是扩展,所以它们就是它的方法......
  3. 如果我们要传递QueryOver,我们只需要引用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;
            }
        }
    }
    

答案 2 :(得分:0)

如果我找不到某种我认识的方法,我通常会使用ILSpy来查看dll。你运行它,删除所有&#34;默认&#34;程序集,只拖放你需要的组件(例如nhibernate),然后查看 - >搜索,如果你知道你在组合框中选择 Type 的类型名称,如果你知道你选择的方法名称​​会员

相关问题