-
Notifications
You must be signed in to change notification settings - Fork 685
/
Copy pathAutoMappingOverrideAlteration.cs
39 lines (36 loc) · 1.44 KB
/
AutoMappingOverrideAlteration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#if USE_NULLABLE
#nullable enable
#endif
using System.Linq;
using System.Reflection;
namespace FluentNHibernate.Automapping.Alterations;
/// <summary>
/// Built-in alteration for altering an AutoPersistenceModel with instance of IAutoMappingOverride<T>.
/// </summary>
/// <param name="overrideAssembly">Assembly to load overrides from.</param>
public class AutoMappingOverrideAlteration(Assembly overrideAssembly) : IAutoMappingAlteration
{
/// <summary>
/// Alter the model
/// </summary>
/// <remarks>
/// Finds all types in the assembly (passed in the constructor) that implement IAutoMappingOverride<T>, then
/// creates an AutoMapping<T> and applies the override to it.
/// </remarks>
/// <param name="model">AutoPersistenceModel instance to alter</param>
public void Alter(AutoPersistenceModel model)
{
// find all types deriving from IAutoMappingOverride<T>
var types = from type in overrideAssembly.GetExportedTypes()
where !type.IsAbstract
let entity = (from interfaceType in type.GetInterfaces()
where interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IAutoMappingOverride<>)
select interfaceType.GetGenericArguments()[0]).FirstOrDefault()
where entity is not null
select type;
foreach (var type in types)
{
model.Override(type);
}
}
}