Skip to content

Instantly share code, notes, and snippets.

@andreabalducci
Created April 10, 2013 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andreabalducci/5354973 to your computer and use it in GitHub Desktop.
Save andreabalducci/5354973 to your computer and use it in GitHub Desktop.
mapping custom factory for mongodb c# driver deserialization
namespace APP.Domain
{
public class FactoryClassMap : BsonClassMap
{
public FactoryClassMap(Type classType, IDeserializationFactory factory) : base(classType)
{
Func<object> factoryDelegate = factory.Create;
MapCreator(factoryDelegate);
AutoMap();
}
}
public static class DomainModelRegistration
{
public static void Register(IKernel kernel)
{
var assembly = Assembly.GetExecutingAssembly();
var types =
assembly.GetTypes().Where(x => typeof (AggregateRoot).IsAssignableFrom(x) && x.IsClass && !x.IsAbstract);
foreach (var type in types)
{
if (!BsonClassMap.IsClassMapRegistered(type))
{
var factoryType = typeof(IDeserializationFactory<>).MakeGenericType(new[] { type });
if (kernel.HasComponent(factoryType))
{
var factory = (IDeserializationFactory)kernel.Resolve(factoryType);
BsonClassMap.RegisterClassMap(new FactoryClassMap(type, factory));
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment