Using ReadOnlyCollection preventing me from setting up a bi-directional many-to-many relationship

Posted by Kevin Pang on Stack Overflow See other posts from Stack Overflow or by Kevin Pang
Published on 2010-04-02T13:41:02Z Indexed on 2010/04/02 13:43 UTC
Read the original article Hit count: 386

Filed under:
|

I'm using NHibernate to persist a many-to-many relation between Users and Networks. I've set up both the User and Network class as follows, exposing each's collections as ReadOnlyCollections to prevent direct access to the underlying lists. I'm trying to make sure that the only way a User can be added to a Network is by using its "JoinNetwork" function. However, I can't seem to figure out how to add the User to the Network's list of users since its collection is readonly.

public class User    
{
    private ISet<Network> _Networks = new HashedSet<Network>();
    public ReadOnlyCollection<Network> Networks
    {
        get
        {
            return new List<Network>(_Networks).AsReadOnly();
        }
    }

    public void JoinNetwork(Network network)
    {
        _Networks.Add(network);

        // How do I add the current user to the Network's list of users?
    }
}

public class Network
{
    private ISet<User> _Users = new HashedSet<User>();
    public ReadOnlyCollection<User> Users
    {
        get
        {
            return new List<User>(_Users).AsReadOnly();
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate