Compiling Mapping using Fluent NHibernate

This took me some time to find and work out so I am posting this in case I ever need again. I was getting mapping errors so wanted to see what Fluent NH spits out.

public class FluentNHUtil
{
	string schemaExportPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
		"FluentNHMappings");
	[Observation]
	public void compile_mappings_to_disk()
	{
		if (Directory.Exists(schemaExportPath))
			Directory.Delete(schemaExportPath, true);
		Directory.CreateDirectory(schemaExportPath);
		var model = new ReportingModelPersistenceMappingHandler().CreateModel();
		model.CompileMappings();
		model.WriteMappingsTo(schemaExportPath);		
	}
	
}
public class ReportingModelPersistenceMappingHandler
{
	public MappingConfiguration Handle(MappingConfiguration mappingConfiguration)
	{
		mappingConfiguration.AutoMappings.Add(CreateModel());
		mappingConfiguration.HbmMappings.AddFromAssembly(GetType().Assembly);
		return mappingConfiguration;
	}

	public AutoPersistenceModel CreateModel()
	{
		//the Components convention seems redundant but is dealing with an oddity in FluentNH
		return AutoMap.Assembly(GetType().Assembly)
			.Setup(s =>
					{
						s.IsComponentType = type => type.Namespace.Contains("Components");
					})
			.Conventions.Add(
			new ReportingModelIdConvention(),
			DefaultLazy.Never(),
			new PluralizeTableNameConvention(),
			new HasManyDefaultConvention(),
			new LabNumberComponentConvention()
			).Where(t => t.Namespace.Contains(GetNamespaceConvention()) && t.Namespace.Contains("Components")==false);
	}

	private string GetNamespaceConvention()
	{
		return GetType().Namespace + ".Model";
	}
}

Posted 10-29-2009 4:03 PM by Michael Nichols

[Advertisement]

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)