Since when I started using ConfORM to map my domain entities I felt the need to have something that showed me what type of mappings it generates; you can easily write tests and export the XML generated mappings (you have examples on how to do this in the ConfORM source code sample projects), but I wasn’t very confortable with that solution so I started writing my own mapping explorer utility.

Some of the key requirements I set are:

  • The ability to instantiate and configure ConfORM the same exact way it work in your project.
  • The support for mixed mapping techniques (that is part of the domain mapped in XML, as I wrote in my previous article).
  • It should work without a real database connection (for the basic set of features).
  • You should not be required to run your application, just point to the assemblies that contain the mappings.
  • It should not lock the assemblies when executing, so you are free to change and recompile the codebase with the ToolBox still open.

Minimal set of features:

  • Point to a folder containing a set of assemblies to explore.
  • Discover all the mappings available in the assemblies (even embedded XML files mappings).
  • Show each mapped entity and its corresponding mapping.
  • The ability to show all the mappings in a single view or navigate between them.
  • Show the database SQL generation script.
  • Show NHibernate logs and errors while generating the mappings.

This is the very first drop of my ToolBox Mapping Explorer, it’s like an early development preview and many options are hardcoded (you cannot change the target database type for example, it’s actually stuck to SQL Server 2005), but nonetheless it works. Here’s how the thing looks like right now:

ConfOrmToolBox

Its usage is quite intuitive: point to a folder containing the set of assemblies you want to examine, hit the ‘Process’ button to discover the defined domain mappers (more on this in a minute), then hit the ‘Show Mapping’ button to start processing and show the mappings.

After that you can start looking at your mappings (it will show only the automatically generated ones at the moment, any predefined XML mapping will not be displayed) and at the database generation script; if you encounter any error, detailed exception information will be reported in the log box and in the related information box.

Let’s see how a domain mapper is discovered: this utility is still a bit invasive when it comes to define a domain mapper, in fact to be used it requires that your ConfORM initialization phase is wrapped in a class that implement the following interface:

public interface IDomainMapper
{
	HbmMapping HbmMapping { get; }

	IList<HbmMapping> HbmMappings { get; }
}

It’s important that you keep the name of the interface and the names of the two properties, here’s an example of usage:

public class DomainMapper : IDomainMapper
{
	private static ObjectRelationalMapper _domainInspector;
	private static Mapper _mapper;

	#region IDomainMapper Members

	public HbmMapping HbmMapping
	{
		get
		{
			// compile the mapping for the specified entities
			var types = InitConfOrm();
			return _mapper.CompileMappingFor(types);
		}
	}

	public IList<HbmMapping> HbmMappings
	{
		get
		{
			// compile the mapping for the specified entities
			var types = InitConfOrm();
			return _mapper.CompileMappingForEach(types).ToList();
		}
	}

	private static IEnumerable<Type> InitConfOrm()
	{
		_domainInspector = new ObjectRelationalMapper();

		// define your patterns, and POID stretegies
		var patternsAppliers = new SafePropertyAccessorPack();

		_mapper = new Mapper(_domainInspector, patternsAppliers);

		// define the mapping shape
		// ...		

		return mappedEntities;
	}

	#endregion
}

That’s it for this post, see you next! Ah... wait... the binaries!

Here are the compiled download package:

And here is a ConfORM test project you can use to see the ToolBox in action:

Related Content