|
8 | 8 | using FluentNHibernate.Mapping.Providers;
|
9 | 9 | using FluentNHibernate.MappingModel;
|
10 | 10 | using FluentNHibernate.MappingModel.ClassBased;
|
| 11 | +using FluentNHibernate.Utils; |
| 12 | +using FluentNHibernate.Utils.Reflection; |
11 | 13 | using FluentNHibernate.Visitors;
|
12 | 14 |
|
13 | 15 | namespace FluentNHibernate.Automapping
|
@@ -345,37 +347,40 @@ public AutoPersistenceModel Override<T>(Action<AutoMapping<T>> populateMap)
|
345 | 347 | return this;
|
346 | 348 | }
|
347 | 349 |
|
| 350 | + static bool IsAutomappingForType(object o, Type entityType) |
| 351 | + { |
| 352 | + var autoMappingType = ReflectionHelper.AutomappingTypeForEntityType(entityType); |
| 353 | + |
| 354 | + return o.GetType().IsAssignableFrom(autoMappingType); |
| 355 | + } |
| 356 | + |
348 | 357 | /// <summary>
|
349 | 358 | /// Adds an IAutoMappingOverride reflectively
|
350 | 359 | /// </summary>
|
351 | 360 | /// <param name="overrideType">Override type, expected to be an IAutoMappingOverride</param>
|
352 | 361 | public void Override(Type overrideType)
|
353 | 362 | {
|
354 |
| - Type overrideInterface = overrideType.GetInterfaces() |
355 |
| - .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IAutoMappingOverride<>) && x.GetGenericArguments().Length > 0) |
356 |
| - .FirstOrDefault(); |
357 |
| - if (overrideInterface != null) |
358 |
| - { |
359 |
| - Type entityType = overrideInterface.GetGenericArguments().First(); |
360 |
| - Type autoMappingType = typeof(AutoMapping<>).MakeGenericType(entityType); |
361 |
| - AddOverride(entityType, x => |
362 |
| - { |
363 |
| - if (x.GetType().IsAssignableFrom(autoMappingType)) |
364 |
| - { |
365 |
| - var overrideInstance = Activator.CreateInstance(overrideType); |
366 |
| - |
367 |
| - MethodInfo overrideHelperMethod = typeof(AutoPersistenceModel) |
368 |
| - .GetMethod("OverrideHelper", BindingFlags.NonPublic | BindingFlags.Instance); |
369 |
| - |
370 |
| - if (overrideHelperMethod != null) |
371 |
| - { |
372 |
| - overrideHelperMethod |
373 |
| - .MakeGenericMethod(entityType) |
374 |
| - .Invoke(this, new[] {x, overrideInstance}); |
375 |
| - } |
376 |
| - } |
377 |
| - }); |
378 |
| - } |
| 363 | + var overrideInterface = overrideType.GetInterfaces().FirstOrDefault(x => x.IsAutoMappingOverrideType()); |
| 364 | + |
| 365 | + if (overrideInterface == null) return; |
| 366 | + |
| 367 | + var entityType = overrideInterface.GetGenericArguments().First(); |
| 368 | + |
| 369 | + AddOverride(entityType, instance => |
| 370 | + { |
| 371 | + if (!IsAutomappingForType(instance, entityType)) return; |
| 372 | + |
| 373 | + var overrideInstance = Activator.CreateInstance(overrideType); |
| 374 | + |
| 375 | + MethodInfo overrideHelperMethod = typeof(AutoPersistenceModel) |
| 376 | + .GetMethod("OverrideHelper", BindingFlags.NonPublic | BindingFlags.Instance); |
| 377 | + |
| 378 | + if (overrideHelperMethod == null) return; |
| 379 | + |
| 380 | + overrideHelperMethod |
| 381 | + .MakeGenericMethod(entityType) |
| 382 | + .Invoke(this, new[] {instance, overrideInstance}); |
| 383 | + }); |
379 | 384 | }
|
380 | 385 |
|
381 | 386 | //called reflectively from method above
|
|
0 commit comments