tags:

views:

106

answers:

1

As a way to learn NHibernate, I came up with a small project that includes a typical users & groups authentication system. It got me thinking about how this would be done. I quickly put together the following classes and mapped them to the database, which worked after a lot of trial and error. I ended up with a three-table database schema with a many to many association table between the User and Group tables.

public class User
{
    public virtual string Username { get; set; }
    public virtual byte[] PasswordHash { get; set; }
    public virtual IList<Group> Groups { get; set; }
}

public class Group
{
    public virtual string Name { get; set; }
    public virtual IList<User> Users { get; set; }
}

My question is regarding the scaleability and potential performance of this sort of class design. If this was in a production system with tens of thousands of users, even with lazy-loading on a Group's Users collection, any call to the Groups property could set off a potentially HUGE data retrieval.

How would NHibernate cope with such a scenario and how might I improve upon my design?

+2  A: 

Don't create these as properties. Add functions to these classes which will allow you to fine tune your queries (through the use of parameters) to retrieve the specific data sets you require.

Spencer Ruport
I thought that one of the killer features of an ORM was persistence ignorance. If I was to create a GetUsers(...) method on the Group class, it would need to know how to get Users, which is surely persistence awareness.Similarly if I wanted to call myGroup.AddUser(...), if my Group class does not maintain a collection of Users, where does it put the User?
EC