Skip to content

Make persistent collection classes implement the IReadOnly* interfaces #2284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 17, 2019

Conversation

marcelltoth
Copy link
Contributor

Issue

Currently the PersistentGenericBag/List/etc. classes that NHibernate uses only implement the IList<T> or ICollection<T> interface, but not the readonly IReadOnlyList<T> / IReadOnlyCollection<T> interfaces. Unfortunately the read/write interfaces do not inherit the readonly ones. This in turn makes using such properties in my model classes illegal.

Example code:

public virtual IReadOnlyCollection<ShippingAddress> ShippingAddresses { get; protected set; }

This throws at runtime:

System.InvalidCastException: Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag1[MyProject.Domain.Entities.BillingAddress]' to type 'System.Collections.Generic.IReadOnlyCollection1[MyProject.Domain.Entities.BillingAddress]'

Solution

I made sure all of our custom persistent collection classes implement the IReadOnly* counterpart as well.

At most places I only had to add the the interface, all methods / properties were already implemented by the implementation for the mutable interface. Where there were missing properties I implemented them using an explicit interface implementation so no change in the collection's public interface has been made other than the added interface implementation.

Remark

This is my first contribution to this repo. I tried to follow all the guidelines and existing coding style, but if there is anything I need to change, I welcome all feedback.

Copy link
Contributor Author

@marcelltoth marcelltoth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Alexander, I never thought that collections themselves have async counterparts.

@hazzik hazzik merged commit 77738a0 into nhibernate:master Dec 17, 2019
@hazzik hazzik added this to the 5.3 milestone Dec 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants