Created
May 29, 2013 12:48
-
-
Save SlyNet/5670026 to your computer and use it in GitHub Desktop.
Mapper class with the ability to store XML
This file contains hidden or 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 static class ActiveRecordMapper | |
{ | |
public static void GenerateMappings(global::NHibernate.Cfg.Configuration config, IEnumerable<Assembly> modelAssemblies, Type baseDomainEntityType) | |
{ | |
var xmlVisitor = new XmlGenerationVisitor(); | |
ActiveRecordModelCollection models = BuildModels(modelAssemblies, baseDomainEntityType); | |
var connectorVisitor = new GraphConnectorVisitor(models); | |
connectorVisitor.VisitNodes(models); | |
var semanticVisitor = new SemanticVerifierVisitor(models); | |
semanticVisitor.VisitNodes(models); | |
foreach (ActiveRecordModel model in models) | |
{ | |
if (!model.IsNestedType && !model.IsDiscriminatorSubClass && !model.IsJoinedSubClass) | |
{ | |
xmlVisitor.Reset(); | |
xmlVisitor.CreateXml(model); | |
String xml = xmlVisitor.Xml; | |
if (xml != String.Empty) | |
{ | |
AddXmlString(config, xml, model); | |
} | |
} | |
} | |
} | |
private static List<Type> CollectValidActiveRecordTypesFromAssembly(IEnumerable<Assembly> assemblies, Type baseDomainEntityType) | |
{ | |
var types = new List<Type>(); | |
foreach (var assembly in assemblies) | |
{ | |
var exportedTypes = assembly.GetExportedTypes(); | |
types.AddRange(exportedTypes.Where(t => t.IsSubclassOf(baseDomainEntityType) && t != baseDomainEntityType)); | |
} | |
return types.Where(IsActiveRecordType).ToList(); | |
} | |
private static ActiveRecordModelCollection BuildModels(IEnumerable<Assembly> domainAssemblies, Type baseDomainEntityType) | |
{ | |
var builder = new ActiveRecordModelBuilder(); | |
var types = CollectValidActiveRecordTypesFromAssembly(domainAssemblies, baseDomainEntityType); | |
ActiveRecordModelCollection models = builder.Models; | |
foreach (Type type in types) | |
{ | |
ActiveRecordModel model = builder.Create(type); | |
if (model == null) | |
{ | |
throw new ActiveRecordException(String.Format("ActiveRecordModel for `{0}` could not be created", type.FullName)); | |
} | |
} | |
return models; | |
} | |
private static bool IsActiveRecordType(ICustomAttributeProvider type) | |
{ | |
return type.IsDefined(typeof(ActiveRecordAttribute), false); | |
} | |
private static void AddXmlString(global::NHibernate.Cfg.Configuration config, string xml, ActiveRecordModel model) | |
{ | |
config.AddXmlString(xml); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment