public Configuration MapEntities(IEnumerable <Type> rootEntityTypes, MatchEntities matchEntities = null,
                                         Action <hibernatemapping> applyCustomMappings = null)
        {
            var entityTypes = GetEntityTypes(rootEntityTypes, matchEntities);

            var mappingXDoc = new hibernatemapping(); //this creates the mapping xml document

            //create class xml elements for each entity type
            foreach (var type in entityTypes)
            {
                var @class = new @class()
                {
                    name  = type.AssemblyQualifiedName,
                    table = type.Name.Pluralize(), //pluralized table names - could easily have checked for a [TableName("SomeTable")] attribute for custom overrides
                };
                [email protected](@class);
            }

            var conventions =
                GetAll <IClassConvention>() //get all the conventions from the current project
                .TopologicalSort()          //sort them into a dependency tree
                .ToList();

            using (Profiler.Step("Running conventions"))
            {
                //run througn all the conventions, updating the document as we go
                foreach (var convention in conventions)
                {
                    using (Profiler.Step("Running convention " + convention.ToString()))
                    {
                        foreach (var type in entityTypes)
                        {
                            var @class = [email protected](c => c.name == type.AssemblyQualifiedName);
                            convention.Apply(type, @class, entityTypes, mappingXDoc);
                        }
                    }
                }
            }

            if (applyCustomMappings != null)
            {
                applyCustomMappings(mappingXDoc);
            }

            var xml = mappingXDoc.ToString();

            if (_writeToDisk)
            {
                var path = HostingEnvironment.ApplicationPhysicalPath ??
                           Path.GetDirectoryName(
                    Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").
                    Replace("/", "\\"));
                File.WriteAllText(Path.Combine(path, "config.hbm.xml"), xml);
            }
            _cfg.AddXml(xml);


            foreach (var classConvention in conventions)
            {
                foreach (var adbo in classConvention.AuxDbObjects())
                {
                    _cfg.AddAuxiliaryDatabaseObject(adbo);
                }
            }
            return(_cfg);
        }