Should be able to add a user to more than one role. Should be able to remove the user from specific roles.
Convention over Configuration
This topic is garnering quite a bit of interest of late in the .Net community. James Kovacs recently appeared on an episode of DotNetRocks rocks discussing Convention over Configuration. He spent a fair amount of time discussing the move from XML configuration for NHibernate by using the conventions of Fluent NHibernate. By default S#arp Architecture implements the Fluent NHibernate auto-mapping convention. This means that you only need to alter the convention for various edge cases, one of those being M:M associations. Knowing in advance that you will need to perform the override is handy hence the reason for today’s discussion.
Review of the Model
I have the following two entities forming a many-to-many relationship.
Mapping Override using Class Auto Mapping
I created a sub-folder within my data assembly naming it NHibernateMaps and then proceeded to add the following two classes within it. I added using statements for Fluent NHibernate’s AutoMap and AutoMap Alteration namespaces. The key mapping attributes below for both sides of the relationship are the WithParentKeyColumn and WithChildKeyColumn values.
public class AppUserMap : IAutoMappingOverride<AppUser>
public void Override(AutoMap<AppUser> mapping)
mapping.Id(x => x.Id, "AppUserID")
mapping.Map(x => x.LoginName).WithLengthOf(50);
mapping.Map(x => x.Password).WithLengthOf(255);
mapping.HasManyToMany(x => x.Roles)
public class RoleMap : IAutoMappingOverride<Role>
public void Override(AutoMap<Role> mapping)
mapping.Id(x => x.Id, "RoleID")
mapping.Map(x => x.Name, "RoleName");
mapping.HasManyToMany<AppUser>(x => x.AppUsers)
The choice of using a Bag, List, Set, Map or array structure to handle the transient collection in memory depends on the context of the requirement at hand. In our domain model above we are representing the User and Role associations as an IList structure that is equivalent to NHibernate’s IBag object and therefore we set the mapping to use a Bag. Karl Seguin who blogs over at CodeBetter.com discusses this point in the Relationships section in Part 6 of a great series of articles that formed his book called Foundations of Programming.
In the next article I hope to implement the same override but by using the Conventional approach. Therefore there will not be any need to use any mapping classes. I will wrap up the series by indicating the additional changes needed in the UI and controller layers.