C# 通用NHibernate存储库包含(TId id)方法

C# 通用NHibernate存储库包含(TId id)方法,c#,nhibernate,C#,Nhibernate,我有一个使用NHibernate的通用存储库,其中ID的类型也是通用参数: /// <summary> /// Represents a common base class for repositories. /// </summary> /// <typeparam name="TEntity"> The type of the entity. </typeparam> /// <typeparam name="TId"> The t

我有一个使用NHibernate的通用存储库,其中ID的类型也是通用参数:

/// <summary>
/// Represents a common base class for repositories.
/// </summary>
/// <typeparam name="TEntity"> The type of the entity. </typeparam>
/// <typeparam name="TId"> The type of the ID of the entity. </typeparam>
public abstract class RepositoryBase<TEntity, TId> : IRepository<TEntity, TId> where TEntity : EntityBase<TEntity, TId>
更新:


就我而言,NHibernate已经关闭了lazyload。

正如评论中所指出的。。。使用“id”特殊属性,使用条件

public bool Contains(TId id)
{
    using (var session = NHibernateHelper.OpenSession())
    { 
        return session.CreateCriteria(typeof(TEntity))
            .Add(Expression.Eq("id", id))
            .SetProjection( Projections.Count("id"))
            .UniqueResult() > 0
    }
}

我认为您必须重写实体基类中的
Eqauls
,以及其他比较运算符,例如:

public abstract class TEntity
{
    public override bool Equals(object entity)
    {
        return entity != null
            && entity is EntityBase
            && this == (EntityBase)entity;
    }

    public static bool operator ==(EntityBase base1, 
        EntityBase base2)
    {
        if ((object)base1 == null && (object)base2 == null)
        {
            return true;
        }

        if ((object)base1 == null || (object)base2 == null)
        {
            return false;
        }
        if (base1.Key != base2.Key)
        {
            return false;
        }

        return true;
    }
    public static bool operator !=(EntityBase base1, 
        EntityBase base2)
    {
        return (!(base1 == base2));
    }
}

那会是什么样子?TId是一个通用参数…标准标准api采用了一个通用对象。我认为您可以使用WhereRestrictionOn()和Restrictions.Eq()重新格式化它,以避免提及Equals。顺便说一句,在每个存储库方法中打开一个新会话通常是一个大禁忌,因为它会阻止正确使用缓存、延迟加载和事务。嗯,我还不太熟悉NHiberntate的基础,但它怎么能不点击数据库来检查实体是否存在呢?只有DB拥有所有的实体。另外:GetById不会花费更多的时间,因为它会从DB加载整个实体,而实际上它需要做的只是检查ID是否存在。GetById是这样实现的:
session.Get(ID)
这意味着它将获取所有连接表的所有列,并实例化聚合的所有连接子对象。只是为了检查实体(ID)的存在性,这不是太过分了吗?在我第一次回答时,关于延迟加载的编辑不在那里。我想我不希望使用
bool-Contains(TEntity-entity)
,而是
bool-Contains(TId-ID)
,因为如果NHibernate需要使用我的实体类的.Equals,在最坏的情况下,它将不得不从数据库加载所有实体,并为每个实体调用
tenty.Equals
public abstract class TEntity
{
    public override bool Equals(object entity)
    {
        return entity != null
            && entity is EntityBase
            && this == (EntityBase)entity;
    }

    public static bool operator ==(EntityBase base1, 
        EntityBase base2)
    {
        if ((object)base1 == null && (object)base2 == null)
        {
            return true;
        }

        if ((object)base1 == null || (object)base2 == null)
        {
            return false;
        }
        if (base1.Key != base2.Key)
        {
            return false;
        }

        return true;
    }
    public static bool operator !=(EntityBase base1, 
        EntityBase base2)
    {
        return (!(base1 == base2));
    }
}