Search code examples
databasenhibernatehibernatedomain-driven-designlinktable

How do you deal with linktables when using Domain Driven Design and nHibernate?


If you have the tables Groups, Users, Channels and between each two a linktable, how do you organize that in Domain Driven Design and nHibernate? Does a Group have a UserCollection and a ChannelCollection, a User a GroupCollection and ChannelCollection and a Channel a GroupCollection and UserCollection?

And if you want to add a Group to a Channel, do you need to save the Channel with all of its groups? Even worse, adding a User to a Channel... saving all the users of the Channel requires to load them all first, then adding the new user and then saving the Channel (ok, probably only the added user will be saved, but you do need to load them all...). Or will you create a method in a repository AddUserToChannel(User user, Channel channel) or AddUserToChannel(User user, int channelId)?


Solution

  • Yes and no. Yes: your Group has a Channels collection etc. However lazy loading comes into play. My understanding is that most of the concerns you have about having to load large quantities of records are invalid. ie saying something like:

    myGroup.Channels.Add(myChannel);
    myChannel.Groups.Add(myGroup);
    

    Wouldnt force myChannel's groups to be loaded.