Skip to content

Collection element type can not be set via ICollectionConvention #770

@cremor

Description

@cremor

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:

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:

public ElementPart Type<TElement>()
{
attributes.Set("Type", Layer.UserSupplied, new TypeReference(typeof(TElement)));
return this;
}

But here the convention sets it with Layer.Conventions:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions