Getting Fluent NHibernate and Castle NHibernateFacility to play well together

There’s an interface, Castle.Facilities.NHibernateIntegration.IConfigurationContributor, which gives one the opportunity to hook onto the NHibernate.Cfg.Configuration that the Facility will create, and add on the FluentNH mappings that you’d like (or do anything else you might like to enrich it, actually).  You register your config contributor into your IoC container – the Facility will look for these during start-up (so, if you have more than one SessionFactory, you’ll need to filter on the ids).  This ends up looking something like:

public class FooConfigurationContributor : IConfigurationContributor 
{
	public void Process(string name, Configuration config)
	{
		if (name == "foo") // only enrich the session-factory configuration with id="foo" in the facility ioc-config.
		{
			var maps = from t in typeof (FooMap).Assembly.GetTypes()
			                   where
			                   	t.BaseType.Name.Contains(typeof (ClassMap<>).Name)
			                   select t;
			Fluently
				.Configure(config)
				.Mappings(x =>
				{
					foreach (var map in maps)
					{
						x.FluentMappings.Add(map);
					}
				})
				.BuildConfiguration();
		}
	}
}
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • DotNetKicks
  • DZone
  • LinkedIn
  • Technorati

Leave a comment

Your comment

Click to Insert Smiley

SmileBig SmileGrinLaughLOLFrownBig FrownWinkKissRazzAngelAngryReally AngryConfusedNeutralThinkingChicCoolNerdSillyDrunken RazzMad RazzEvil GrinMeanPissed OffReally PissedCurseShoutGrit TeethCryWeepSide FrownWiltSmugDisdainRoll EyesSarcasmLoserTalk to the HandShyBeat UpPainShameBeautyBlushCuteLashesKissingKiss BlowKissedHeh!SmirkSnickerGiggleIn LoveDroolEek!ShockSickSuspenseTrembleDazedHypnotizedFoot in MouthMoney MouthQuietShut MouthDOH!IDKQuestionLyingStruggleSweatStopByeGo AwayWavingTime OutCall MeOn the PhoneMeetingSecretHandshakeHigh FiveHug LeftHug RightClapDanceJumpFingers CrossedVictoryYawnSleepyPrayWorshipWaitingAlienClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicPartyPirateSkywalkerSnowmanSoldierGhostSkeletonEatStarvingVampireZombie KillerBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteYesNoAirplaneCarIslandAnnouncebrbBeerDrinkLiquorCakeCoffeePizzaWatermelonBowlPlateCanMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette