Permalink
Browse files

Merge branch 'issue169' of https://github.com/chester89/fluent-nhiber…

…nate into chester89-issue169
  • Loading branch information...
2 parents 640ae31 + 3302718 commit 4900f065537142e6f36e6520840195a65ef8d10f @jagregory committed Oct 11, 2012
@@ -76,4 +76,52 @@ public class when_using_multiple_overrides_from_different_assemblies
static AutoPersistenceModel model;
static ClassMapping mapping;
}
+
+ [Subject(typeof(IAutoMappingOverride<>))]
+ public class when_multiple_overrides_present_in_one_class
+ {
+ Establish context = () =>
+ {
+ model = AutoMap.Source(new StubTypeSource(typeof(Entity), typeof(Parent), typeof(B_Parent)));
+ model.Override(typeof(MultipleOverrides));
+ };
+
+ Because of = () =>
+ {
+ entityMapping = model.BuildMappingFor<Entity>();
+ parentMapping = model.BuildMappingFor<Parent>();
+ bParentMapping = model.BuildMappingFor<B_Parent>();
+ };
+
+ It should_apply_overrides_to_every_class_for_which_such_were_provided = () =>
+ {
+ entityMapping.EntityName.ShouldEqual("customEntityName");
+ parentMapping.TableName.ShouldEqual("fancyTableName_Parent");
+ bParentMapping.BatchSize.ShouldEqual(50);
+ };
+
+
+ static AutoPersistenceModel model;
+ static ClassMapping entityMapping;
+ static ClassMapping parentMapping;
+ static ClassMapping bParentMapping;
+ }
+
+ public class MultipleOverrides: IAutoMappingOverride<Entity>, IAutoMappingOverride<Parent>, IAutoMappingOverride<B_Parent>
+ {
+ public void Override(AutoMapping<Entity> mapping)
+ {
+ mapping.EntityName("customEntityName");
+ }
+
+ public void Override(AutoMapping<Parent> mapping)
+ {
+ mapping.Table("fancyTableName_Parent");
+ }
+
+ public void Override(AutoMapping<B_Parent> mapping)
+ {
+ mapping.BatchSize(50);
+ }
+ }
}
@@ -360,27 +360,28 @@ static bool IsAutomappingForType(object o, Type entityType)
/// <param name="overrideType">Override type, expected to be an IAutoMappingOverride</param>
public void Override(Type overrideType)
{
- var overrideInterface = overrideType.GetInterfaces().FirstOrDefault(x => x.IsAutoMappingOverrideType());
+ var overrideInterfaces = overrideType.GetInterfaces().Where(x => x.IsAutoMappingOverrideType()).ToList();
- if (overrideInterface == null) return;
-
- var entityType = overrideInterface.GetGenericArguments().First();
-
- AddOverride(entityType, instance =>
+ foreach (var overrideInterface in overrideInterfaces)
{
- if (!IsAutomappingForType(instance, entityType)) return;
+ var entityType = overrideInterface.GetGenericArguments().First();
+
+ AddOverride(entityType, instance =>
+ {
+ if (!IsAutomappingForType(instance, entityType)) return;
- var overrideInstance = Activator.CreateInstance(overrideType);
+ var overrideInstance = Activator.CreateInstance(overrideType);
- MethodInfo overrideHelperMethod = typeof(AutoPersistenceModel)
- .GetMethod("OverrideHelper", BindingFlags.NonPublic | BindingFlags.Instance);
+ MethodInfo overrideHelperMethod = typeof(AutoPersistenceModel)
+ .GetMethod("OverrideHelper", BindingFlags.NonPublic | BindingFlags.Instance);
- if (overrideHelperMethod == null) return;
+ if (overrideHelperMethod == null) return;
- overrideHelperMethod
- .MakeGenericMethod(entityType)
- .Invoke(this, new[] {instance, overrideInstance});
- });
+ overrideHelperMethod
+ .MakeGenericMethod(entityType)
+ .Invoke(this, new[] { instance, overrideInstance });
+ });
+ }
}
//called reflectively from method above

0 comments on commit 4900f06

Please sign in to comment.