Sunday, August 30, 2009

S#arp Architecture – Part 1: Implementing the M:M mapping override

User Story

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 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 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")
.WithUnsavedValue(0)
.GeneratedBy.Identity();

mapping.WithTable("AppUsers");
mapping.SetAttribute("lazy", "false");
mapping.Map(x => x.LoginName).WithLengthOf(50);
mapping.Map(x => x.Password).WithLengthOf(255);
mapping.HasManyToMany(x => x.Roles)
.WithTableName("AppUserRoles")
.WithParentKeyColumn("AppUserID")
.WithChildKeyColumn("RoleID")
.AsBag();
}
}
public class RoleMap : IAutoMappingOverride<Role>
{
public void Override(AutoMap<Role> mapping)
{
mapping.Id(x => x.Id, "RoleID")
.WithUnsavedValue(0)
.GeneratedBy.Identity();

mapping.Map(x => x.Name, "RoleName");
mapping.SetAttribute("lazy","false");
mapping.HasManyToMany<AppUser>(x => x.AppUsers)
.WithTableName("AppUserRoles")
.Inverse()
.WithParentKeyColumn("RoleID")
.WithChildKeyColumn("AppUserID")
.AsBag();
}
}

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. 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 .

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.

Saturday, August 29, 2009

S#arp Architecture - Architectural review

Overview

For the past 6 months or so I have been using and following closely the framework’s evolution from beta to release. During this time I have found the project, community and reference documentation excellent! and the team of contributors have done a professional job assembling this framework into a key source of guidance on how to assemble an enterprise architecture that embraces (from its Project main page on Google code) :

  • Loose coupling leveraging Microsoft’s ASP.Net MVC
  • Persistence ignorance with NHibernate
  • Domain Driven Design
  • Pre-configured infrastructure

S#arp Architecture includes S#arp Scaffolding that greatly speeds up the process of adding CRUD functionality for your entities through the use of T4 templates implemented with the T4 Template toolkit. The “out-of-the box” templates can be extended adding support for using the JavaScript library. It also includes a Visual Studio project template to build out your Solution tree. Billy has a good on the topic of extending the T4 template to embrace the EXT-JS library.He frequently responds to questions that come into the Google group discussion forum linked at the bottom of this post. has done several videos on getting started and extending the architecture. Others of note who contribute with insightful comments and blog entries that have helped my understanding are .

What follows will be a series of blog posts documenting the ability to override the default generated templates and code to produce the desired result of managing the roles for various users accessing an MVC website in the context of varying controller actions.

The posts will be as follows:

Introducing the model and overriding Fluent NHibernate’s auto mapping strategy within S#arp Architecture

Links