Skip to content

Allow setting dynamic component templates from dictionary in ByCode #1941

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 1 commit into from
May 28, 2019
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
Expand Up @@ -6,7 +6,7 @@
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.MappingByCode.ExpliticMappingTests
namespace NHibernate.Test.MappingByCode.ExplicitMappingTests
{
[TestFixture]
public class DynamicComponentMappingTests
Expand All @@ -33,6 +33,141 @@ public IDictionary<string, object> Info
}
}

private class PersonWithDynamicInfo
{
public int Id { get; set; }
public dynamic Info { get; set; }
}

[Test]
public void WhenMapDynCompoByDictionaryThenMapItAndItsProperties()
{
//NH-3704
var mapper = new ModelMapper();
mapper.Class<Person>(
map =>
{
map.Id(x => x.Id, idmap => { });
map.Component(
x => x.Info,
new Dictionary<string, System.Type>
{{"MyInt", typeof(int)}, {"MyDate", typeof(DateTime)}},
z => { z.Property("MyInt", pm => pm.Column("MY_COLUMN")); });
});

var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Person) });
var hbmClass = hbmMapping.RootClasses[0];
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
Assert.That(hbmDynamicComponent, Is.Not.Null);
Assert.That(
hbmDynamicComponent.Properties.Select(x => x.Name),
Is.EquivalentTo(new[] { "MyInt", "MyDate" }));
}

[Test]
public void WhenMapDynCompoByDictionaryThenMapItAndItsPropertiesGeneric()
{
//NH-3704
var mapper = new ModelMapper();
mapper.Class<PersonWithGenericInfo>(
map =>
{
map.Id(x => x.Id, idmap => { });
map.Component(
x => x.Info,
new Dictionary<string, System.Type>
{{"MyInt", typeof(int)}, {"MyDate", typeof(DateTime)}},
z => { z.Property("MyInt", pm => pm.Column("MY_COLUMN")); });
});

var hbmMapping = mapper.CompileMappingFor(new[] { typeof(PersonWithGenericInfo) });
var hbmClass = hbmMapping.RootClasses[0];
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
Assert.That(hbmDynamicComponent, Is.Not.Null);
Assert.That(
hbmDynamicComponent.Properties.Select(x => x.Name),
Is.EquivalentTo(new[] { "MyInt", "MyDate" }));
}

[Test]
public void WhenMapDynCompoByDictionaryThenMapItAndItsPropertiesDynamic()
{
//NH-3704
var mapper = new ModelMapper();
mapper.Class<PersonWithDynamicInfo>(
map =>
{
map.Id(x => x.Id, idmap => { });
map.Component(
nameof(PersonWithDynamicInfo.Info),
new Dictionary<string, System.Type>
{{"MyInt", typeof(int)}, {"MyDate", typeof(DateTime)}},
z =>
{
z.Property("MyInt", pm => pm.Column("MY_COLUMN"));
z.Component<DateTime>("MyDate");
});
});

var hbmMapping = mapper.CompileMappingFor(new[] { typeof(PersonWithDynamicInfo) });
var hbmClass = hbmMapping.RootClasses[0];
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
Assert.That(hbmDynamicComponent, Is.Not.Null);
Assert.That(
hbmDynamicComponent.Properties.Select(x => x.Name),
Is.EquivalentTo(new[] { "MyInt", "MyDate" }));
}

[Test]
public void WhenMapPrivateDynCompoByDictionaryThenMapItAndItsProperties()
{
//NH-3704
var mapper = new ModelMapper();
mapper.Class<Person>(
map =>
{
map.Id(x => x.Id, idmap => { });
map.Component(
"Info",
new Dictionary<string, System.Type>
{{"MyInt", typeof(int)}, {"MyDate", typeof(DateTime)}},
z => { z.Property("MyInt", pm => pm.Column("MY_COLUMN")); });
});

var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Person) });
var hbmClass = hbmMapping.RootClasses[0];
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
Assert.That(hbmDynamicComponent, Is.Not.Null);
Assert.That(
hbmDynamicComponent.Properties.Select(x => x.Name),
Is.EquivalentTo(new[] { "MyInt", "MyDate" }));
}

[Test]
public void WhenMapPrivateDynCompoByDictionaryThenMapItAndItsPropertiesGeneric()
{
//NH-3704
var mapper = new ModelMapper();
mapper.Class<PersonWithGenericInfo>(
map =>
{
map.Id(x => x.Id, idmap => { });
map.Component(
"Info",
new Dictionary<string, System.Type>
{{"MyInt", typeof(int)}, {"MyDate", typeof(DateTime)}},
z => { z.Property("MyInt", pm => pm.Column("MY_COLUMN")); });
});

var hbmMapping = mapper.CompileMappingFor(new[] { typeof(PersonWithGenericInfo) });
var hbmClass = hbmMapping.RootClasses[0];
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
Assert.That(hbmDynamicComponent, Is.Not.Null);
Assert.That(
hbmDynamicComponent.Properties.Select(x => x.Name),
Is.EquivalentTo(new[] { "MyInt", "MyDate" }));
}

[Test]
public void WhenMapDynCompoThenMapItAndItsProperties()
{
Expand Down
30 changes: 30 additions & 0 deletions src/NHibernate/Mapping/ByCode/IPlainPropertyContainerMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,29 @@ public interface IBasePlainPropertyContainerMapper<TContainer> : IMinimalPlainPr
{
void Component<TComponent>(Expression<Func<TContainer, TComponent>> property, Action<IComponentMapper<TComponent>> mapping);
void Component<TComponent>(Expression<Func<TContainer, TComponent>> property);
/// <summary>
/// Maps a non-generic dictionary property as a dynamic component.
/// </summary>
/// <param name="property">The property to map.</param>
/// <param name="dynamicComponentTemplate">The template for the component. It should either be a (usually
/// anonymous) type having the same properties than the component, or an
/// <c>IDictionary&lt;string, System.Type&gt;</c> of property names with their type.</param>
/// <param name="mapping">The mapping of the component.</param>
/// <typeparam name="TComponent">The type of the template.</typeparam>
void Component<TComponent>(Expression<Func<TContainer, IDictionary>> property, TComponent dynamicComponentTemplate, Action<IDynamicComponentMapper<TComponent>> mapping);

void Component<TComponent>(string notVisiblePropertyOrFieldName, Action<IComponentMapper<TComponent>> mapping);
void Component<TComponent>(string notVisiblePropertyOrFieldName);
/// <summary>
/// Maps a property or field as a dynamic component. The property can be a C# <c>dynamic</c> or a dictionary of
/// property names to their value.
/// </summary>
/// <param name="notVisiblePropertyOrFieldName">The property or field name to map.</param>
/// <param name="dynamicComponentTemplate">The template for the component. It should either be a (usually
/// anonymous) type having the same properties than the component, or an
/// <c>IDictionary&lt;string, System.Type&gt;</c> of property names with their type.</param>
/// <param name="mapping">The mapping of the component.</param>
/// <typeparam name="TComponent">The type of the template.</typeparam>
void Component<TComponent>(string notVisiblePropertyOrFieldName, TComponent dynamicComponentTemplate, Action<IDynamicComponentMapper<TComponent>> mapping);

void Any<TProperty>(Expression<Func<TContainer, TProperty>> property, System.Type idTypeOfMetaType, Action<IAnyMapper> mapping) where TProperty : class;
Expand All @@ -61,6 +80,17 @@ public interface IPlainPropertyContainerMapper<TContainer> : IBasePlainPropertyC
public static class BasePlainPropertyContainerMapperExtensions
{
//6.0 TODO: Merge into IBasePlainPropertyContainerMapper<> interface
/// <summary>
/// Maps a generic <c>IDictionary&lt;string, object&gt;</c> property as a dynamic component.
/// </summary>
/// <param name="mapper">The mapper.</param>
/// <param name="property">The property to map.</param>
/// <param name="dynamicComponentTemplate">The template for the component. It should either be a (usually
/// anonymous) type having the same properties than the component, or an
/// <c>IDictionary&lt;string, System.Type&gt;</c> of property names with their type.</param>
/// <param name="mapping">The mapping of the component.</param>
/// <typeparam name="TContainer">The type of the mapped class.</typeparam>
/// <typeparam name="TComponent">The type of the template.</typeparam>
public static void Component<TContainer, TComponent>(
this IBasePlainPropertyContainerMapper<TContainer> mapper,
Expression<Func<TContainer, IDictionary<string, object>>> property,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void Parent<TProperty>(Expression<Func<TComponent, TProperty>> parent) wh

public void Parent(string notVisiblePropertyOrFieldName, Action<IComponentParentMapper> parentMapping)
{
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVisiblePropertyOrFieldName);
MemberInfo member = GetRequiredPropertyOrFieldByName(notVisiblePropertyOrFieldName);
AddCustomizer(m => m.Parent(member, parentMapping));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
using System;
using System.Reflection;

namespace NHibernate.Mapping.ByCode.Impl.CustomizersImpl
{
public class DynamicComponentCustomizer<TComponent> : PropertyContainerCustomizer<TComponent>, IDynamicComponentMapper<TComponent>
public class DynamicComponentCustomizer<TComponent> : PropertyContainerCustomizer<TComponent>,
IDynamicComponentMapper<TComponent>
{
public DynamicComponentCustomizer(IModelExplicitDeclarationsHolder explicitDeclarationsHolder, ICustomizersHolder customizersHolder, PropertyPath propertyPath)
private readonly System.Type _componentType;

public DynamicComponentCustomizer(
IModelExplicitDeclarationsHolder explicitDeclarationsHolder,
ICustomizersHolder customizersHolder,
PropertyPath propertyPath)
: this(typeof(TComponent), explicitDeclarationsHolder, customizersHolder, propertyPath)
{
}

internal DynamicComponentCustomizer(
System.Type componentType,
IModelExplicitDeclarationsHolder explicitDeclarationsHolder,
ICustomizersHolder customizersHolder,
PropertyPath propertyPath)
: base(explicitDeclarationsHolder, customizersHolder, propertyPath)
{
if (propertyPath == null)
{
throw new ArgumentNullException("propertyPath");
throw new ArgumentNullException(nameof(propertyPath));
}

if (explicitDeclarationsHolder == null)
{
throw new ArgumentNullException("explicitDeclarationsHolder");
throw new ArgumentNullException(nameof(explicitDeclarationsHolder));
}
explicitDeclarationsHolder.AddAsDynamicComponent(propertyPath.LocalMember, typeof(TComponent));

_componentType = componentType;
explicitDeclarationsHolder.AddAsDynamicComponent(propertyPath.LocalMember, _componentType);
}

#region IDynamicComponentMapper<TComponent> Members
Expand Down Expand Up @@ -51,5 +70,15 @@ public void Unique(bool unique)
}

#endregion

protected override MemberInfo GetRequiredPropertyOrFieldByName(string memberName)
{
var result = _componentType.GetPropertyOrFieldMatchingName(memberName);
if (result == null)
{
throw new MappingException(string.Format("Member not found. The member '{0}' does not exists in type {1}", memberName, _componentType.FullName));
}
return result;
}
}
}
}
Loading