Skip to content

NH-3704 - Allow Setting Dynamic Component Templates From Dictionary #341

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

Closed
wants to merge 3 commits into from
Closed
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,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
Expand All @@ -20,6 +21,42 @@ public IDictionary Info
}
}

[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 => { });
});

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 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 => { });
});

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 WhenMapDynCompoThenMapItAndItsProperties()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

namespace NHibernate.Mapping.ByCode.Impl.CustomizersImpl
{
Expand All @@ -18,6 +21,55 @@ public DynamicComponentCustomizer(IModelExplicitDeclarationsHolder explicitDecla
explicitDeclarationsHolder.AddAsDynamicComponent(propertyPath.LocalMember, typeof(TComponent));
}

public DynamicComponentCustomizer(IDictionary<string, System.Type> template, IModelExplicitDeclarationsHolder explicitDeclarationsHolder, ICustomizersHolder customizersHolder, PropertyPath propertyPath)
: base(explicitDeclarationsHolder, customizersHolder, propertyPath)
{
if (propertyPath == null)
{
throw new ArgumentNullException("propertyPath");
}
if (explicitDeclarationsHolder == null)
{
throw new ArgumentNullException("explicitDeclarationsHolder");
}

var componentType = this.CreateType(template);

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

private System.Type CreateType(IDictionary<string, System.Type> properties)

This comment was marked as spam.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your aspiration to implement changes with fewer possible code changes, but I want to have this properly implemented

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but try another way, it's not easy.

{
var assemblyName = new AssemblyName("MyDynamicAssembly");
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
var typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public | TypeAttributes.Serializable);

foreach (var property in properties)
{
var propertyBuilder = typeBuilder.DefineProperty(property.Key, PropertyAttributes.HasDefault, property.Value, null);
var getMethodBuilder = typeBuilder.DefineMethod("get_" + property.Key, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, property.Value, System.Type.EmptyTypes);
var setMethodBuilder = typeBuilder.DefineMethod("set_" + property.Key, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, typeof(void), new System.Type[] { property.Value });

var getILGenerator = getMethodBuilder.GetILGenerator();
getILGenerator.Emit(OpCodes.Ldarg_0);
getILGenerator.Emit(OpCodes.Nop);
getILGenerator.Emit(OpCodes.Ret);

var setILGenerator = setMethodBuilder.GetILGenerator();
setILGenerator.Emit(OpCodes.Ldarg_0);
setILGenerator.Emit(OpCodes.Ldarg_1);
setILGenerator.Emit(OpCodes.Ret);

propertyBuilder.SetGetMethod(getMethodBuilder);
propertyBuilder.SetSetMethod(setMethodBuilder);
}

var type = typeBuilder.CreateType();

return type;
}

#region IDynamicComponentMapper<TComponent> Members

public void Access(Accessor accessor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ protected void RegisterComponentMapping<TComponent>(Action<IComponentMapper<TCom

public void Component<TComponent>(Expression<Func<TEntity, IDictionary>> property, TComponent dynamicComponentTemplate, Action<IDynamicComponentMapper<TComponent>> mapping)
{
RegisterDynamicComponentMapping(property, mapping);
if (dynamicComponentTemplate is IDictionary<string, System.Type>)
{
RegisterDynamicComponentMapping<TComponent>(property, dynamicComponentTemplate as IDictionary<string, System.Type>, mapping);
}
else
{
RegisterDynamicComponentMapping(property, mapping);
}
}

protected virtual void RegisterDynamicComponentMapping<TComponent>(Expression<Func<TEntity, IDictionary>> property, Action<IDynamicComponentMapper<TComponent>> mapping)
Expand All @@ -105,6 +112,21 @@ protected virtual void RegisterDynamicComponentMapping<TComponent>(Expression<Fu
RegisterDynamicComponentMapping(mapping, member, memberOf);
}

protected virtual void RegisterDynamicComponentMapping<TComponent>(Expression<Func<TEntity, IDictionary>> property, IDictionary<string, System.Type> template, Action<IDynamicComponentMapper<TComponent>> mapping)
{
MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property);
MemberInfo memberOf = TypeExtensions.DecodeMemberAccessExpressionOf(property);
RegisterDynamicComponentMapping<TComponent>(template, mapping, member, memberOf);
}

protected void RegisterDynamicComponentMapping<TComponent>(IDictionary<string, System.Type> template, Action<IDynamicComponentMapper<TComponent>> mapping, params MemberInfo[] members)
{
foreach (var member in members)
{
mapping(new DynamicComponentCustomizer<TComponent>(template, explicitDeclarationsHolder, CustomizersHolder, new PropertyPath(PropertyPath, member)));
}
}

protected void RegisterDynamicComponentMapping<TComponent>(Action<IDynamicComponentMapper<TComponent>> mapping, params MemberInfo[] members)
{
foreach (var member in members)
Expand Down Expand Up @@ -479,7 +501,15 @@ public void Component<TComponent>(string notVisiblePropertyOrFieldName, TCompone
{
MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow(notVisiblePropertyOrFieldName);
MemberInfo memberOf = member.GetMemberFromReflectedType(typeof(TEntity));
RegisterDynamicComponentMapping<TComponent>(mapping, member, memberOf);

if (dynamicComponentTemplate is IDictionary<string, System.Type>)
{
RegisterDynamicComponentMapping<TComponent>(dynamicComponentTemplate as IDictionary<string, System.Type>, mapping, member, memberOf);
}
else
{
RegisterDynamicComponentMapping<TComponent>(mapping, member, memberOf);
}
}

public void Any<TProperty>(string notVisiblePropertyOrFieldName, System.Type idTypeOfMetaType, Action<IAnyMapper> mapping) where TProperty : class
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/NHibernate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\SharedAssemblyInfo.cs">
Expand Down