Skip to content

Commit 4900f06

Browse files
committed
Merge branch 'issue169' of https://github.com/chester89/fluent-nhibernate into chester89-issue169
2 parents 640ae31 + 3302718 commit 4900f06

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

src/FluentNHibernate.Specs/Automapping/OverrideSpecs.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,52 @@ public class when_using_multiple_overrides_from_different_assemblies
7676
static AutoPersistenceModel model;
7777
static ClassMapping mapping;
7878
}
79+
80+
[Subject(typeof(IAutoMappingOverride<>))]
81+
public class when_multiple_overrides_present_in_one_class
82+
{
83+
Establish context = () =>
84+
{
85+
model = AutoMap.Source(new StubTypeSource(typeof(Entity), typeof(Parent), typeof(B_Parent)));
86+
model.Override(typeof(MultipleOverrides));
87+
};
88+
89+
Because of = () =>
90+
{
91+
entityMapping = model.BuildMappingFor<Entity>();
92+
parentMapping = model.BuildMappingFor<Parent>();
93+
bParentMapping = model.BuildMappingFor<B_Parent>();
94+
};
95+
96+
It should_apply_overrides_to_every_class_for_which_such_were_provided = () =>
97+
{
98+
entityMapping.EntityName.ShouldEqual("customEntityName");
99+
parentMapping.TableName.ShouldEqual("fancyTableName_Parent");
100+
bParentMapping.BatchSize.ShouldEqual(50);
101+
};
102+
103+
104+
static AutoPersistenceModel model;
105+
static ClassMapping entityMapping;
106+
static ClassMapping parentMapping;
107+
static ClassMapping bParentMapping;
108+
}
109+
110+
public class MultipleOverrides: IAutoMappingOverride<Entity>, IAutoMappingOverride<Parent>, IAutoMappingOverride<B_Parent>
111+
{
112+
public void Override(AutoMapping<Entity> mapping)
113+
{
114+
mapping.EntityName("customEntityName");
115+
}
116+
117+
public void Override(AutoMapping<Parent> mapping)
118+
{
119+
mapping.Table("fancyTableName_Parent");
120+
}
121+
122+
public void Override(AutoMapping<B_Parent> mapping)
123+
{
124+
mapping.BatchSize(50);
125+
}
126+
}
79127
}

src/FluentNHibernate/Automapping/AutoPersistenceModel.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -360,27 +360,28 @@ static bool IsAutomappingForType(object o, Type entityType)
360360
/// <param name="overrideType">Override type, expected to be an IAutoMappingOverride</param>
361361
public void Override(Type overrideType)
362362
{
363-
var overrideInterface = overrideType.GetInterfaces().FirstOrDefault(x => x.IsAutoMappingOverrideType());
363+
var overrideInterfaces = overrideType.GetInterfaces().Where(x => x.IsAutoMappingOverrideType()).ToList();
364364

365-
if (overrideInterface == null) return;
366-
367-
var entityType = overrideInterface.GetGenericArguments().First();
368-
369-
AddOverride(entityType, instance =>
365+
foreach (var overrideInterface in overrideInterfaces)
370366
{
371-
if (!IsAutomappingForType(instance, entityType)) return;
367+
var entityType = overrideInterface.GetGenericArguments().First();
368+
369+
AddOverride(entityType, instance =>
370+
{
371+
if (!IsAutomappingForType(instance, entityType)) return;
372372

373-
var overrideInstance = Activator.CreateInstance(overrideType);
373+
var overrideInstance = Activator.CreateInstance(overrideType);
374374

375-
MethodInfo overrideHelperMethod = typeof(AutoPersistenceModel)
376-
.GetMethod("OverrideHelper", BindingFlags.NonPublic | BindingFlags.Instance);
375+
MethodInfo overrideHelperMethod = typeof(AutoPersistenceModel)
376+
.GetMethod("OverrideHelper", BindingFlags.NonPublic | BindingFlags.Instance);
377377

378-
if (overrideHelperMethod == null) return;
378+
if (overrideHelperMethod == null) return;
379379

380-
overrideHelperMethod
381-
.MakeGenericMethod(entityType)
382-
.Invoke(this, new[] {instance, overrideInstance});
383-
});
380+
overrideHelperMethod
381+
.MakeGenericMethod(entityType)
382+
.Invoke(this, new[] { instance, overrideInstance });
383+
});
384+
}
384385
}
385386

386387
//called reflectively from method above

0 commit comments

Comments
 (0)