Skip to content

Instantly share code, notes, and snippets.

@jasondentler
Created July 21, 2010 15:37
Show Gist options
  • Save jasondentler/484645 to your computer and use it in GitHub Desktop.
Save jasondentler/484645 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using ConfOrm;
using ConfOrm.NH;
using ConfOrm.Patterns;
using NHibernate.Cfg.MappingSchema;
namespace Eg.ConfORMMapping.Mappings
{
public class MappingFactory
{
private readonly Mapper _mapper;
public MappingFactory()
{
_mapper = GetMapper();
}
public HbmMapping CreateMapping()
{
return _mapper
.CompileMappingFor(GetEntityTypes());
}
private static
ObjectRelationalMapper GetORM()
{
var orm = new ObjectRelationalMapper();
orm.TablePerClassHierarchy<Product>();
orm.TablePerClass<ActorRole>();
orm.NaturalId<Product>(p => p.Name);
orm.Cascade<Movie, ActorRole>(
Cascade.All | Cascade.DeleteOrphans);
orm.Patterns.PoidStrategies
.Add(new GuidOptimizedPoidPattern());
orm.Patterns.Versions
.Add(new MyVersionPattern());
return orm;
}
private static Mapper GetMapper()
{
var orm = GetORM();
var mapper = new Mapper(orm);
mapper.AddPropertyPattern(
m => orm.IsRootEntity(m.DeclaringType) &&
!m.Name.Equals("Description"),
a => a.NotNullable(true));
return mapper;
}
private static IEnumerable<Type> GetEntityTypes()
{
var entityType = typeof (Entity);
return entityType.Assembly.GetTypes()
.Where(t => entityType.IsAssignableFrom(t));
}
}
}
using System;
using System.Reflection;
using ConfOrm;
namespace Eg.ConfORMMapping.Mappings
{
public class MyVersionPattern : IPattern<MemberInfo>
{
public bool Match(MemberInfo subject)
{
return IsInt(subject) &&
IsNamedVersion(subject);
}
private bool IsInt(MemberInfo subject)
{
if (subject == null)
{
return false;
}
var propertyOrFieldType =
subject.GetPropertyOrFieldType();
return propertyOrFieldType == typeof (int);
}
private bool IsNamedVersion(MemberInfo subject)
{
var name = subject.Name;
return name.Equals("version",
StringComparison.InvariantCultureIgnoreCase);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment