Skip to content

Allow specification of tuplizers for components and subclasses #727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NUnit.Framework;
using System;
using FluentNHibernate.MappingModel;
using NUnit.Framework;

namespace FluentNHibernate.Testing.DomainModel.Mapping;

Expand Down Expand Up @@ -103,6 +105,34 @@ public void ComponentSetsClass()
.ForMapping(m =>
m.Component(x => x.Component, c => c.Map(x => x.Name)))
.Element("class/component").HasAttribute("class", typeof(ComponentTarget).AssemblyQualifiedName);
}
}

[Test]
public void ComponentCanSetTuplizer()
{
Type tuplizerType = typeof(NHibernate.Tuple.Entity.PocoEntityTuplizer);
new MappingTester<PropertyTarget>()
.ForMapping(m =>
m.Component(x => x.Component, c => c.Tuplizer(TuplizerMode.Poco, tuplizerType)))
.Element("class/component/tuplizer")
.HasAttribute("class", tuplizerType.AssemblyQualifiedName)
.HasAttribute("entity-mode", nameof(TuplizerMode.Poco).ToLower());
}

[Test]
public void ComponentCanSetTuplizerInCorrectOrderRegardlessOfDeclaration()
{
Type tuplizerType = typeof(NHibernate.Tuple.Entity.PocoEntityTuplizer);
new MappingTester<PropertyTarget>()
.ForMapping(m =>
m.Component(x => x.Component, c =>
{
c.Map(x => x.Name);
c.ParentReference(x => x.MyParent);
c.Tuplizer(TuplizerMode.Poco, tuplizerType);
}))
.Element("class/component/tuplizer").Exists().ShouldBeInParentAtPosition(0)
.Element("class/component/parent").Exists().ShouldBeInParentAtPosition(1)
.Element("class/component/property").Exists().ShouldBeInParentAtPosition(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public virtual MappingTester<T> ShouldBeInParentAtPosition(int elementPosition)
else
{
XmlElement elementAtPosition = (XmlElement)currentElement.ParentNode.ChildNodes.Item(elementPosition);
Assert.That(elementAtPosition, Is.EqualTo(currentElement), $"Expected '{currentElement.Name}' but was '{elementAtPosition.Name}'");
Assert.That(elementAtPosition, Is.SameAs(currentElement), $"Expected '{currentElement.Name}' but was '{elementAtPosition.Name}'");
}

return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
using FluentNHibernate.Mapping;
using FluentNHibernate.MappingModel;
using NUnit.Framework;

namespace FluentNHibernate.Testing.DomainModel.Mapping;
Expand Down Expand Up @@ -590,7 +592,28 @@ public void SubSubclassOneToManyReferenceHasConventionApplied()
.HasAttribute("foreign-key", "test_fk");

}


[Test]
public void SubclassCanSetTuplizerInCorrectOrder()
{
Type tuplizerType = typeof(NHibernate.Tuple.Entity.PocoEntityTuplizer);
new MappingTester<MappedObject>()
.SubClassMapping<MappedObjectSubclass>(m =>
{
m.Map(x => x.SubclassProperty);
m.Tuplizer(TuplizerMode.Poco, tuplizerType);
m.DiscriminatorValue("test");
})
.ForMapping(m =>
{
m.DiscriminateSubClassesOnColumn("test_column");
m.Id(x => x.Id);
m.Tuplizer(TuplizerMode.Poco, tuplizerType);
})
.Element("class/subclass/tuplizer").Exists().ShouldBeInParentAtPosition(0)
.Element("class/tuplizer").Exists().ShouldBeInParentAtPosition(0);
}

public class TestPropertyConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using FluentNHibernate.MappingModel;
using NUnit.Framework;

namespace FluentNHibernate.Testing.DomainModel.Mapping;
Expand Down Expand Up @@ -44,4 +46,24 @@ public void ShouldAllowEntityNameToBeSetOnUnionSubclasses()
}).Element("class/union-subclass")
.HasAttribute("entity-name", "name");
}

[Test]
public void SubclassCanSetTuplizerInCorrectOrder()
{
Type tuplizerType = typeof(NHibernate.Tuple.Entity.PocoEntityTuplizer);
new MappingTester<MappedObject>()
.SubClassMapping<MappedObjectSubclass>(m =>
{
m.Map(x => x.SubclassProperty);
m.Tuplizer(TuplizerMode.Poco, tuplizerType);
})
.ForMapping(m =>
{
m.UseUnionSubclassForInheritanceMapping();
m.Id(x => x.Id);
m.Tuplizer(TuplizerMode.Poco, tuplizerType);
})
.Element("class/union-subclass/tuplizer").Exists().ShouldBeInParentAtPosition(0)
.Element("class/tuplizer").Exists().ShouldBeInParentAtPosition(0);
}
}
13 changes: 2 additions & 11 deletions src/FluentNHibernate/Mapping/ClassMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,17 +583,8 @@ public ClassMap<T> ApplyFilter(string name)
/// </summary>
/// <param name="mode">Tuplizer entity-mode</param>
/// <param name="tuplizerType">Tuplizer type</param>
public TuplizerPart Tuplizer(TuplizerMode mode, Type tuplizerType)
{
providers.TuplizerMapping = new TuplizerMapping();
providers.TuplizerMapping.Set(x => x.Mode, Layer.UserSupplied, mode);
providers.TuplizerMapping.Set(x => x.Type, Layer.UserSupplied, new TypeReference(tuplizerType));

return new TuplizerPart(providers.TuplizerMapping)
.Type(tuplizerType)
.Mode(mode);
}

public TuplizerPart Tuplizer(TuplizerMode mode, Type tuplizerType) => CreateTuplizerPart(mode, tuplizerType);

ClassMapping IMappingProvider.GetClassMapping()
{
var mapping = new ClassMapping(attributes.Clone());
Expand Down
12 changes: 12 additions & 0 deletions src/FluentNHibernate/Mapping/ClasslikeMapBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using FluentNHibernate.Mapping.Providers;
using FluentNHibernate.MappingModel;
using FluentNHibernate.Utils;

namespace FluentNHibernate.Mapping;
Expand Down Expand Up @@ -438,6 +439,17 @@ protected StoredProcedurePart StoredProcedure(string element, string innerText)
providers.StoredProcedures.Add(part);
return part;
}

protected TuplizerPart CreateTuplizerPart(TuplizerMode mode, Type tuplizerType)
{
providers.TuplizerMapping = new TuplizerMapping();
providers.TuplizerMapping.Set(x => x.Mode, Layer.UserSupplied, mode);
providers.TuplizerMapping.Set(x => x.Type, Layer.UserSupplied, new TypeReference(tuplizerType));

return new TuplizerPart(providers.TuplizerMapping)
.Type(tuplizerType)
.Mode(mode);
}

internal IEnumerable<IPropertyMappingProvider> Properties => providers.Properties;

Expand Down
9 changes: 9 additions & 0 deletions src/FluentNHibernate/Mapping/ComponentPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public ComponentPart<T> LazyLoad()
nextBool = true;
return this;
}

/// <summary>
/// Configures the tuplizer for this component. The tuplizer defines how to transform
/// a Property-Value to its persistent representation, and viceversa a Column-Value
/// to its in-memory representation, and the EntityMode defines which tuplizer is in use.
/// </summary>
/// <param name="mode">Tuplizer entity-mode</param>
/// <param name="tuplizerType">Tuplizer type</param>
public TuplizerPart Tuplizer(TuplizerMode mode, Type tuplizerType) => CreateTuplizerPart(mode, tuplizerType);

IComponentMapping IComponentMappingProvider.GetComponentMapping()
{
Expand Down
2 changes: 2 additions & 0 deletions src/FluentNHibernate/Mapping/ComponentPartBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ protected ComponentMapping CreateComponentMapping()
if (member is not null)
mapping.Set(x => x.Name, Layer.Defaults, member.Name);

mapping.Set(x => x.Tuplizer, Layer.UserSupplied, providers.TuplizerMapping);

foreach (var property in providers.Properties)
mapping.AddProperty(property.GetPropertyMapping());

Expand Down
1 change: 0 additions & 1 deletion src/FluentNHibernate/Mapping/SubClassPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using FluentNHibernate.Mapping.Providers;
using FluentNHibernate.MappingModel;
using FluentNHibernate.MappingModel.ClassBased;
using FluentNHibernate.Utils;

namespace FluentNHibernate.Mapping;

Expand Down
13 changes: 12 additions & 1 deletion src/FluentNHibernate/Mapping/SubclassMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ public void Extends(Type type)
{
attributes.Set("Extends", Layer.UserSupplied, type);
}

/// <summary>
/// Configures the tuplizer for this entity. The tuplizer defines how to transform
/// a Property-Value to its persistent representation, and viceversa a Column-Value
/// to its in-memory representation, and the EntityMode defines which tuplizer is in use.
/// </summary>
/// <param name="mode">Tuplizer entity-mode</param>
/// <param name="tuplizerType">Tuplizer type</param>
public TuplizerPart Tuplizer(TuplizerMode mode, Type tuplizerType) => CreateTuplizerPart(mode, tuplizerType);

SubclassMapping IIndeterminateSubclassMappingProvider.GetSubclassMapping(SubclassType type)
{
Expand Down Expand Up @@ -330,6 +339,9 @@ SubclassMapping IIndeterminateSubclassMappingProvider.GetSubclassMapping(Subclas
case MappingProviderStore.ProviderType.StoredProcedure:
mapping.AddStoredProcedure(((IStoredProcedureMappingProvider)mappingProviderObj).GetStoredProcedureMapping());
break;
case MappingProviderStore.ProviderType.Tupilizer:
mapping.Set(y => y.Tuplizer, Layer.Defaults, (TuplizerMapping)mappingProviderObj);
break;
case MappingProviderStore.ProviderType.Subclass:
case MappingProviderStore.ProviderType.Filter:
case MappingProviderStore.ProviderType.Join:
Expand All @@ -338,7 +350,6 @@ SubclassMapping IIndeterminateSubclassMappingProvider.GetSubclassMapping(Subclas
case MappingProviderStore.ProviderType.NaturalId:
case MappingProviderStore.ProviderType.Version:
case MappingProviderStore.ProviderType.Discriminator:
case MappingProviderStore.ProviderType.Tupilizer:
default:
throw new Exception("Internal Error");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public override void AcceptVisitor(IMappingModelVisitor visitor)
{
visitor.ProcessComponent(this);

if (Tuplizer is not null)
visitor.Visit(Tuplizer);

base.AcceptVisitor(visitor);
}

Expand All @@ -39,6 +42,8 @@ public override void AcceptVisitor(IMappingModelVisitor visitor)
public TypeReference Class => attributes.GetOrDefault<TypeReference>();

public bool Lazy => attributes.GetOrDefault<bool>();

public TuplizerMapping Tuplizer => attributes.GetOrDefault<TuplizerMapping>();

public bool Equals(ComponentMapping other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public override void AcceptVisitor(IMappingModelVisitor visitor)
{
visitor.ProcessSubclass(this);

if (Tuplizer is not null)
visitor.Visit(Tuplizer);

if (SubclassType == SubclassType.JoinedSubclass && Key is not null)
visitor.Visit(Key);

Expand Down Expand Up @@ -66,6 +69,8 @@ public override void AcceptVisitor(IMappingModelVisitor visitor)
public TypeReference Persister => attributes.GetOrDefault<TypeReference>();

public int BatchSize => attributes.GetOrDefault<int>();

public TuplizerMapping Tuplizer => attributes.GetOrDefault<TuplizerMapping>();

public void OverrideAttributes(AttributeStore store)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public override void Visit(ParentMapping parentMapping)

document.ImportAndAppendChild(parentXml);
}

public override void Visit(TuplizerMapping mapping)
{
var writer = serviceLocator.GetWriter<TuplizerMapping>();
var filterXml = writer.Write(mapping);

document.ImportAndAppendChild(filterXml);
}
}
8 changes: 8 additions & 0 deletions src/FluentNHibernate/MappingModel/Output/XmlSubclassWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,12 @@ public override void Visit(JoinMapping joinMapping)

document.ImportAndAppendChild(xml);
}

public override void Visit(TuplizerMapping mapping)
{
var writer = serviceLocator.GetWriter<TuplizerMapping>();
var filterXml = writer.Write(mapping);

document.ImportAndAppendChild(filterXml);
}
}