Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mapper class with the ability to store XML
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