NHibernate IList 到列表 [英] NHibernate IList to List

查看:21
本文介绍了NHibernate IList 到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试返回建筑域的集合.

Hi I am trying to return a collection of building domain.

private long _id;
private string _buildingName;
private IList<BasicRoom> _rooms;

我正在使用 NHibernate 和房间映射

I am using NHibernate and this mapping for the rooms

<bag name="rooms" table="tb_rooms" lazy="false">
<key column="buildingID"/>
<one-to-many class="Room"/>
</bag>

我是这样调用数据库的;

And I am calling the db like this;

Buildings = (List<Building>)session.CreateCriteria(typeof(Building)).AddOrder(Order.Asc("buildingName")).List<Building>();

问题是我不希望 _rooms 集合是一个 IList,但我需要它是一个列表.唉,NHibernate 要求我为集合使用接口.任何想法如何做到这一点.我是 .Net 的新手,我认为放弃 NHibernate 可能是前进的方向.我只需要将集合输入为列表,这样我就可以继续了.非常感谢任何帮助.

The problem is I don't want the _rooms collection to be an IList but I need it to be a List. Alas, NHibernate requires that I use an Interface for collections. Any idea how to do this. I am new to .Net and thinking that maybe dropping NHibernate might be the way forward. I just need to get the collection typed as an List so I can move on. Any help much appreciated.

推荐答案

NHibernate 不是这样工作的.您无法告诉它域模型对象的具体类型是什么.您只需告诉它基本类型和接口是什么.然后它就可以选择如何有效地实现域类型.

That's not how NHibernate works. You don't get to tell it what the concrete types of your domain model objects are. You only get to tell it what the base types and interfaces are. Then it gets to choose how to implement the domain types efficiently.

例如,NHibernate 使用它自己的 IList 内部实现,它知道如何进行延迟加载.

For example, NHibernate uses its own internal implementation of IList<T> that knows how to do lazy loading.

此外,NHibernate 使用它自己的域模型中Building 的内部实现.您将与 NHibernate 一起使用的所有 Building 对象不一定都具有作为它们的具体类型 Building 的情况.无论 Building 是对象的具体类型,还是对象的具体类型的基本类型,它们都是 Building 才是正确的.

Moreover, NHibernate uses its own internal implementation of Building from your domain model. It is not necessarily the case that all the Building objects you will be using with NHibernate will have, as their concrete type, Building. It is only true that they will be Buildings, whether Building is the concrete type of your object, or whether it is the base type of the concrete type of your object.

但这是正确的做法,因为无论如何你都应该使用接口和基类型而不是具体类型.NHibernate 可帮助您使用所有正确的编程方法.

But this is the right thing to do, since you are supposed to be using interfaces and base types instead of concrete types anyway. NHibernate helps you use all the right approaches to programming.

现在,为什么需要 List 本身?为什么 IList 不够用?

Now, why do you need a List<Building> per se? Why will IList<Building> not suffice?

这篇关于NHibernate IList 到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆