Files
SocialPub/PrivaPub/Infrastructure/Data/EntityMaps.cs
T

33 lines
887 B
C#
Raw Normal View History

using MongoDB.Entities;
namespace PrivaPub.Infrastructure.Data
{
public static class EntityMaps
{
static readonly Lock Gate = new();
static bool _warm;
// once per process, one map at a time: test hosts boot side by side
public static void Warm()
{
lock (Gate)
{
if (_warm)
return;
Map();
_warm = true;
}
}
static void Map()
{
var collection = typeof(DB).GetMethods()
.Single(m => m.Name == nameof(DB.Collection) && m.IsGenericMethodDefinition && m.GetParameters().Length == 0);
var entities = typeof(EntityMaps).Assembly.GetTypes()
.Where(t => t is { IsClass: true, IsAbstract: false, IsGenericTypeDefinition: false } && typeof(IEntity).IsAssignableFrom(t))
.OrderBy(t => t.FullName, StringComparer.Ordinal);
foreach (var entity in entities)
collection.MakeGenericMethod(entity).Invoke(DB.Default, null);
}
}
}