-
Notifications
You must be signed in to change notification settings - Fork 684
Description
It seems like it is impossible to specify the type of a collection of elements in a convention.
I have written a convention which can be simplified to this:
public class MyElementConvention : ICollectionConvention {
public void Apply(ICollectionInstance instance) {
instance.Element.Type(typeof(MyUserType));
}
}It should be used in collection mappings like this:
HasMany(x => x.SomeElements)
.Table("MyElements")
.AsSet()
.KeyColumn("ParentId")
.Element("ElementValue");
// Alternative without convention:
//.Element("ElementValue", x => x.Type<MyUserType>());The convention is executed correctly, I've confirmed this via a debugger. But the instance.Element.Type(mapperType) call in the Apply method never has any effect on the mapping.
I've debugged Fluent NHibernate a bit and it seems like the problem is here:
fluent-nhibernate/src/FluentNHibernate/Mapping/ToManyBase.cs
Lines 417 to 426 in eed20a2
| public T Element(string columnName) | |
| { | |
| elementPart = new ElementPart(typeof(T)); | |
| elementPart.Type<TChild>(); | |
| if (!string.IsNullOrEmpty(columnName)) | |
| elementPart.Column(columnName); | |
| return (T)this; | |
| } |
Line 420 already sets the "Type" attribute with Layer.UserSupplied, see:
fluent-nhibernate/src/FluentNHibernate/Mapping/ElementPart.cs
Lines 41 to 45 in eed20a2
| public ElementPart Type<TElement>() | |
| { | |
| attributes.Set("Type", Layer.UserSupplied, new TypeReference(typeof(TElement))); | |
| return this; | |
| } |
But here the convention sets it with Layer.Conventions:
fluent-nhibernate/src/FluentNHibernate/Conventions/Instances/ElementInstance.cs
Lines 33 to 36 in eed20a2
| public new void Type(Type type) | |
| { | |
| mapping.Set(x => x.Type, Layer.Conventions, new TypeReference(type)); | |
| } |
So the attribute value from the convention isn't used.
I think the Element mapping method needs to use Layer.Defaults to allow the convention to work. So it should not call the public Type<TElement> method.