只读集合属性NHibernate可以使用 [英] Readonly collection properties that NHibernate can work with

查看:246
本文介绍了只读集合属性NHibernate可以使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网域类别有如下的集合:

My domain classes have collections that look like this:

private List<Foo> _foos = new List<Foo>();
public virtual ReadOnlyCollection<Foo> Foos { get { return _foos.AsReadOnly(); } }

这为我提供了可以在类中修改的只读集合_foos)。

This gives me readonly collections that can be modified from within the class (i.e. by using the field _foos).

此集合映射如下(Fluent NHibernate):

This collection is mapped as follows (Fluent NHibernate):

HasMany(x => x.Foos).KeyColumn("ParentClassId").Cascade.All().Inverse().Access.CamelCaseField(Prefix.Underscore);

现在,当我尝试使用此集合时,我会得到:

Now when I try to use this collection, I get:

无法转换类型为NHibernate.Collection.Generic.PersistentGenericBag 1 [Foo]'的对象来键入System.Collections.Generic.List 1 [ Foo]'。

Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag1[Foo]' to type 'System.Collections.Generic.List1[Foo]'.

根据无法将类型为NHibernate.Collection.Generic.PersistentGenericBag的对象转换为List ,这是因为集合需要暴露给NHibernate作为接口,以便NHibernate可以注入一个它自己的集合类。

According to Unable to cast object of type NHibernate.Collection.Generic.PersistentGenericBag to List, this is because the collection needs to be exposed to NHibernate as an interface so that NHibernate can inject one of its own collection classes.

这篇文章建议使用IList,但遗憾的是这个接口不包括AsReadOnly()方法,搞砸我的计划暴露只读

The article suggests using IList instead, but regrettably this interface doesn't include the AsReadOnly() method, messing up my plans to expose only a readonly collection to the outside world.

任何人都可以提出我可以使用的接口,一种不同的方法,满足相同的要求,或者另一个职业,不涉及这个

Can anyone suggest what interface I might use instead, a different approach that meets the same requirements, or an alternative career that doesn't involve this much frustration?

感谢

David

推荐答案

AsReadOnly()方法不是获取ReadOnlyCollection的唯一方法。

The AsReadOnly() method isn't the only way to get a ReadOnlyCollection.

private IList<Foo> _foos = new List<Foo>();
public virtual ReadOnlyCollection<Foo> Foos { get { return new ReadOnlyCollection<Foo>(_foos); } }

另一个跳跃。

这篇关于只读集合属性NHibernate可以使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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