gpt4 book ai didi

c# - 使用 NHibernate 在我的模型上实现 IPagedList

转载 作者:太空狗 更新时间:2023-10-29 21:48:10 38 4
gpt4 key购买 nike

我发现在使用 NHibernate 并在一个对象上创建一对多关系时,当 many 变得非常大时,它会急剧减慢。现在我的存储库中确实有用于收集该类型的分页 IList 的方法,但是我更希望在模型上也有这些方法,因为这通常是其他开发人员首先查看以收集子对象列表的地方。

例如

RecipientList.Recipients 将返回列表中的每个收件人。

我想实现一种方法,将我所有的分页添加到我的模型中的许多关系中,最好使用一个接口(interface),但实际上任何不会将类型化关系强加到模型上的东西。例如,拥有以下界面会很好:

public interface IPagedList<T> : IList<T>
{
int Count { get; }
IList<T> GetPagedList(int pageNo, int pageSize);
IList<T> GetAll();
}

然后能够在代码中使用它...

IList<Recipient> recipients = RecipientList.Recipients.GetPagedList(1, 400);

我一直在尝试想办法在不让模型意识到分页的情况下做到这一点,但现在我的头撞到了砖墙上。

无论如何,我是否可以用与 NHibernate 目前为 IList 和延迟加载所做的类似的方式来实现接口(interface)?我对 NHibernate 的了解不够。

实现这个是个好主意吗?你的想法将不胜感激,因为我是内部唯一的 .NET 开发人员,我没有人可以反驳想法。

更新

下面的帖子向我指出了 NHibernate 的 custom-collection 属性,它可以很好地工作。但是我不确定解决这个问题的最佳方法是什么,我尝试从 PersistentGenericBag 继承,以便它具有与 IList 相同的基本功能而无需太多工作,但是我不确定如何收集基于 ISessionImplementor 的对象列表.我需要知道如何:

  • 获取我要填充的当前 IList 的某种 ICriteria 详细信息
  • 获取与 IList 关联的特定属性的映射详细信息,以便我可以创建自己的 ICriteria。

但是我不确定我是否可以执行上述任一操作?

谢谢

最佳答案

好的,我将把它作为答案发布,因为它主要是在做我想做的事情。但是,我想要一些反馈,也可能想要回答我迄今为止对解决方案的一个警告:

我创建了一个名为 IPagedList 的接口(interface)。

public interface IPagedList<T> : IList<T>, ICollection
{

IList<T> GetPagedList(int pageNo, int pageSize);

}

然后创建了一个继承自 IPagedList 的基类:

public class PagedList<T> : IPagedList<T>
{

private List<T> _collection = new List<T>();

public IList<T> GetPagedList(int pageNo, int pageSize)
{
return _collection.Take(pageSize).Skip((pageNo - 1) * pageSize).ToList();
}

public int IndexOf(T item)
{
return _collection.IndexOf(item);
}

public void Insert(int index, T item)
{
_collection.Insert(index, item);
}

public void RemoveAt(int index)
{
_collection.RemoveAt(index);
}

public T this[int index]
{
get
{
return _collection[index];
}
set
{
_collection[index] = value;
}
}

public void Add(T item)
{
_collection.Add(item);
}

public void Clear()
{
_collection.Clear();
}

public bool Contains(T item)
{
return _collection.Contains(item);
}

public void CopyTo(T[] array, int arrayIndex)
{
_collection.CopyTo(array, arrayIndex);
}

int Count
{
get
{
return _collection.Count;
}
}

public bool IsReadOnly
{
get { return false; }
}

public bool Remove(T item)
{
return _collection.Remove(item);
}

public IEnumerator<T> GetEnumerator()
{
return _collection.GetEnumerator();
}

int ICollection<T>.Count
{
get { return _collection.Count; }
}

IEnumerator IEnumerable.GetEnumerator()
{
return _collection.GetEnumerator();
}

public void CopyTo(Array array, int index)
{
T[] arr = new T[array.Length];
for (int i = 0; i < array.Length ; i++)
{
arr[i] = (T)array.GetValue(i);
}

_collection.CopyTo(arr, index);
}

int ICollection.Count
{
get { return _collection.Count; }
}

// The IsSynchronized Boolean property returns True if the
// collection is designed to be thread safe; otherwise, it returns False.
public bool IsSynchronized
{
get
{
return false;
}
}

public object SyncRoot
{
get
{
return this;
}
}
}

然后我为 NHibernate 创建一个 IUserCollectionType 用作自定义集合类型,并为继承自 PersistentGenericBag 的 NHPagedList 创建一个 IUserCollectionType,IPagedList 作为实际集合本身。我为它们创建了两个单独的类,因为 IUserCollectionType 的使用似乎对要使用的实际集合根本没有影响,所以我将这两个逻辑部分分开。以上两者的代码如下:

public class PagedListFactory<T> : IUserCollectionType
{

public PagedListFactory()
{ }

#region IUserCollectionType Members

public bool Contains(object collection, object entity)
{
return ((IList<T>)collection).Contains((T)entity);
}

public IEnumerable GetElements(object collection)
{
return (IEnumerable)collection;
}

public object IndexOf(object collection, object entity)
{
return ((IList<T>)collection).IndexOf((T)entity);
}

public object Instantiate(int anticipatedSize)
{
return new PagedList<T>();
}

public IPersistentCollection Instantiate(ISessionImplementor session, ICollectionPersister persister)
{
return new NHPagedList<T>(session);
}

public object ReplaceElements(object original, object target, ICollectionPersister persister,
object owner, IDictionary copyCache, ISessionImplementor session)
{
IList<T> result = (IList<T>)target;

result.Clear();
foreach (object item in ((IEnumerable)original))
{
result.Add((T)item);
}

return result;
}

public IPersistentCollection Wrap(ISessionImplementor session, object collection)
{
return new NHPagedList<T>(session, (IList<T>)collection);
}

#endregion
}

NHPagedList 下一个:

public class NHPagedList<T> : PersistentGenericBag<T>, IPagedList<T>
{

public NHPagedList(ISessionImplementor session) : base(session)
{
_sessionImplementor = session;
}

public NHPagedList(ISessionImplementor session, IList<T> collection)
: base(session, collection)
{
_sessionImplementor = session;
}

private ICollectionPersister _collectionPersister = null;
public NHPagedList<T> CollectionPersister(ICollectionPersister collectionPersister)
{
_collectionPersister = collectionPersister;
return this;
}

protected ISessionImplementor _sessionImplementor = null;

public virtual IList<T> GetPagedList(int pageNo, int pageSize)
{
if (!this.WasInitialized)
{
IQuery pagedList = _sessionImplementor
.GetSession()
.CreateFilter(this, "")
.SetMaxResults(pageSize)
.SetFirstResult((pageNo - 1) * pageSize);

return pagedList.List<T>();
}

return this
.Skip((pageNo - 1) * pageSize)
.Take(pageSize)
.ToList<T>();
}

public new int Count
{
get
{
if (!this.WasInitialized)
{
return Convert.ToInt32(_sessionImplementor.GetSession().CreateFilter(this, "select count(*)").List()[0].ToString());
}

return base.Count;
}
}

}

您会注意到它会检查集合是否已初始化,以便我们知道何时检查数据库中的分页列表或何时只使用内存中的当前对象。

现在您已准备就绪,只需将模型上当前的 IList 引用更改为 IPagedList,然后将 NHibernate 映射到新的自定义集合,使用下面的 Fluent NHibernate,您就可以开始了。

.CollectionType<PagedListFactory<Recipient>>()

这是此代码的第一次迭代,因此需要进行一些重构和修改才能使其完美。

目前我唯一的问题是它不会按照映射文件为父子关系建议的顺序获取分页项目。我在 map 上添加了一个 order-by 属性,它就是不注意它。每个查询中的任何其他 where 子句都没有问题。有谁知道为什么会发生这种情况以及周围是否存在这种情况?如果我不能解决这个问题,我会很失望。

关于c# - 使用 NHibernate 在我的模型上实现 IPagedList<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/876976/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com