Skip to content

Instantly share code, notes, and snippets.

@fabiomaulo
Created July 21, 2010 04:41
Show Gist options
  • Save fabiomaulo/484073 to your computer and use it in GitHub Desktop.
Save fabiomaulo/484073 to your computer and use it in GitHub Desktop.
public class CreatePhisicalMappings
{
[Test, Explicit]
public void CreateXmlMappings()
{
HbmMapping[] mappings = new ConfOrmInitializer().GetCompiledMappingsPerClass();
WriteAllXmlMapping(mappings);
}
private static void WriteAllXmlMapping(IEnumerable<HbmMapping> mappings)
{
foreach (HbmMapping hbmMapping in mappings)
{
string fileName = GetFileName(hbmMapping);
string document = Serialize(hbmMapping);
File.WriteAllText(fileName, document);
}
}
private static string GetFileName(HbmMapping hbmMapping)
{
string name = "MyMapping";
HbmClass rc = hbmMapping.RootClasses.FirstOrDefault();
if (rc != null)
{
name = rc.Name;
}
HbmSubclass sc = hbmMapping.SubClasses.FirstOrDefault();
if (sc != null)
{
name = sc.Name;
}
HbmJoinedSubclass jc = hbmMapping.JoinedSubclasses.FirstOrDefault();
if (jc != null)
{
name = jc.Name;
}
HbmUnionSubclass uc = hbmMapping.UnionSubclasses.FirstOrDefault();
if (uc != null)
{
name = uc.Name;
}
return name + ".hbm.xml";
}
private static string Serialize(HbmMapping hbmElement)
{
string result;
var setting = new XmlWriterSettings {Indent = true};
var serializer = new XmlSerializer(typeof (HbmMapping));
using (var memStream = new MemoryStream(2048))
{
using (XmlWriter xmlWriter = XmlWriter.Create(memStream, setting))
{
serializer.Serialize(xmlWriter, hbmElement);
}
memStream.Position = 0;
using (var sr = new StreamReader(memStream))
{
result = sr.ReadToEnd();
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment