Adding Inflector to UnityWeb

Today as I was browsing the Fluent NHibernate discussion boards I was reading the post regarding features that the creators of the S#arp Architecture were interested in seeing.

One of the things that was pointed out was that FH doesn’t include a pluralizer which can make convention mapping problem matic.

Take my current AutoPersistanceModel

var persistenceModel = AutoPersistenceModel.MapEntitiesFromAssemblyOf<Employee>()
.WithConvention(c => c.DefaultLazyLoad = false)
.WithConvention(convention =>   convention.GetTableName = type => type.Name + "s")
.WithConvention(convention => convention.GetPrimaryKeyNameFromType = type => type.Name + "ID")
.WithConvention(convention => convention.GetForeignKeyName = prop => prop.Name + "ID")
.WithConvention(convention=>convention.DefaultCache = cache => cache.AsReadWrite())
.WithConvention(c => c.OneToManyConvention = m =>
                                                 {
                                                     m.Cascade.All();
                                                     m.LazyLoad();
                                                 })
.ForTypesThatDeriveFrom<Category>(autoMap =>
                                      {
                                            autoMap.WithTable("Categories");
                                          autoMap.Map(p => p.Name, "CategoryName");
                                      })
.ForTypesThatDeriveFrom<Territory>(autoMap =>
                                       {
                                             autoMap.WithTable("Territories");
                                           autoMap.Map(p => p.Description, "TerritoryDescription");
                                           autoMap.Map(p => p.Region,
 "RegionID").CustomTypeIs(typeof (Region));
                                       })
;

In more than 1 senario I need to force the autoMap to use the correct spelling of a table name instead of just being able to append S on it. With Inflector I can get a much cleaner map and only to have:

var persistenceModel = AutoPersistenceModel.MapEntitiesFromAssemblyOf<Employee>()
.WithConvention(c => c.DefaultLazyLoad = false)
.WithConvention(convention =>   convention.GetTableName = type => Inflector.Pluralize(type.Name))
.WithConvention(convention => convention.GetPrimaryKeyNameFromType = type => type.Name + "ID")
.WithConvention(convention => convention.GetForeignKeyName = prop => prop.Name + "ID")
.WithConvention(convention=>convention.DefaultCache = cache => cache.AsReadWrite())
.WithConvention(c => c.OneToManyConvention = m =>
                                                 {
                                                     m.Cascade.All();
                                                     m.LazyLoad();
                                                 })
.ForTypesThatDeriveFrom<Category>(autoMap =>
                                      {
                                          autoMap.Map(p => p.Name, "CategoryName");
                                      })
.ForTypesThatDeriveFrom<Territory>(autoMap =>
                                       {
                                           autoMap.Map(p => p.Description, "TerritoryDescription");
                                           autoMap.Map(p => p.Region,
 "RegionID").CustomTypeIs(typeof (Region));
                                       })
;

You can download Inflector and it’s source over at Andrew Peter’s blog Inflector.NET.

On a further note I believe I’ve learned how to remove the need to have the specific mappings for my enum properties, Name property and Description property which will significantly reduce the code noise of the auto mapping which will show up soon in another blog bost.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s