Created
July 21, 2010 04:41
-
-
Save fabiomaulo/484073 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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