NHibernate 可以使用的只读集合属性
我的域类具有如下所示的集合:
private List<Foo> _foos = new List<Foo>();
public virtual ReadOnlyCollection<Foo> Foos { get { return _foos.AsReadOnly(); } }
这为我提供了可以从类内部修改的只读集合(即通过使用字段 _foos)。
该集合映射如下(流畅的NHibernate):
HasMany(x => x.Foos).KeyColumn("ParentClassId").Cascade.All().Inverse().Access.CamelCaseField(Prefix.Underscore);
现在,当我尝试使用该集合时,我得到:
无法将类型为'NHibernate.Collection.Generic.PersistentGenericBag1[Foo]'的对象转换为类型为'System. Collections.Generic.List
1[Foo]'。
根据 无法转换 NHibernate.Collection 类型的对象。 Generic.PersistentGenericBag to List,这是因为集合需要作为接口暴露给NHibernate,以便NHibernate可以注入自己的集合类之一。
文章建议改用 IList,但遗憾的是这个接口不包含 AsReadOnly() 方法,打乱了我只向外界公开只读集合的计划。
谁能建议我可以使用什么界面,满足相同要求的不同方法,或者不涉及这么多挫败感的替代职业?
谢谢
大卫
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(); } }
This gives me readonly collections that can be modified from within the class (i.e. by using the field _foos).
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:
Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag1[Foo]' to type 'System.Collections.Generic.List
1[Foo]'.
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.
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?
Thanks
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AsReadOnly() 方法并不是获取 ReadOnlyCollection 的唯一方法。
又一个圆环跳了。
The AsReadOnly() method isn't the only way to get a ReadOnlyCollection.
Another hoop jumped.
您的答案是一个很好的解决方案,但我只是将集合公开为
IEnumerable
。这种方法有一个小风险,因为这些可以被强制转换回 IList。这是否是可接受的风险取决于应用程序。Your answer is a good solution but I just expose collections as
IEnumerable<T>
. There is a small risk with this approach because these can be cast back to IList. Whether or not that's an acceptable risk depends on the application.由于 IList 无法满足您的需求以及您没有(幸运地)使用 Automapping,因此我将 Foos 设置为受保护/私有 IList“NHibernate 友好”集合,然后创建一个公共 ReadOnlyCollection ,其内容为通过福斯。
像这样:
这样,唯一公开的属性将是我可笑地称为“ReadOnlyFoos”的属性。
Due to the fact that an IList will not meet your needs and how you're not (luckily) using Automapping, I would set Foos to be a protected/private IList 'NHibernate-friendly' collection, and then create a public ReadOnlyCollection that reads through Foos.
Something like:
This way, the only exposed property would be the one I've ridiculously called "ReadOnlyFoos".
考虑将集合公开为
IEnumerable
而不是ReadOnlyCollection
;它本质上为您提供相同级别的保护,而无需将您的模型与特定的集合实现联系起来。请参阅这篇文章< /a> 进行进一步讨论。Consider exposing the collection as
IEnumerable
instead ofReadOnlyCollection
; it essentially gives you the same level of protection without having to tie your model to a specific collection implementation. See this article for further discussion.