Forked from andreabalducci/DomainModelRegistration.cs
Created
August 1, 2017 19:22
-
-
Save dphoebus/c5bb20011ada556a3f7ec8dfc8dbad4d to your computer and use it in GitHub Desktop.
mapping custom factory for mongodb c# driver deserialization
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
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