NHibernate 可以使用的只读集合属性

发布于 2024-09-11 23:27:41 字数 991 浏览 1 评论 0原文

我的域类具有如下所示的集合:

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.List1[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.List1[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

め七分饶幸 2024-09-18 23:27:41

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

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

又一个圆环跳了。

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); } }

Another hoop jumped.

绮筵 2024-09-18 23:27:41

您的答案是一个很好的解决方案,但我只是将集合公开为 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.

此岸叶落 2024-09-18 23:27:41

由于 IList 无法满足您的需求以及您没有(幸运地)使用 Automapping,因此我将 Foos 设置为受保护/私有 IList“NHibernate 友好”集合,然后创建一个公共 ReadOnlyCollection ,其内容为通过福斯。

像这样:

    protected IList<Foo> MappableFoos { get; set; }
    public ReadOnlyCollection<Foo> ReadOnlyFoos { get { return new ReadOnlyCollection<Foo>(MappableFoos) } }

    // Mapping file
    HasMany(x => x.MappableFoos ).KeyColumn("ParentClassId").Cascade.All().Inverse().Access.CamelCaseField(Prefix.Underscore);

这样,唯一公开的属性将是我可笑地称为“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:

    protected IList<Foo> MappableFoos { get; set; }
    public ReadOnlyCollection<Foo> ReadOnlyFoos { get { return new ReadOnlyCollection<Foo>(MappableFoos) } }

    // Mapping file
    HasMany(x => x.MappableFoos ).KeyColumn("ParentClassId").Cascade.All().Inverse().Access.CamelCaseField(Prefix.Underscore);

This way, the only exposed property would be the one I've ridiculously called "ReadOnlyFoos".

没有你我更好 2024-09-18 23:27:41

考虑将集合公开为 IEnumerable 而不是 ReadOnlyCollection;它本质上为您提供相同级别的保护,而无需将您的模型与特定的集合实现联系起来。请参阅这篇文章< /a> 进行进一步讨论。

Consider exposing the collection as IEnumerable instead of ReadOnlyCollection; 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文