Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,106 @@
using System;
using System.Collections;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NUnit.Framework;
using SharpTestsEx;

namespace NHibernate.Test.MappingByCode.ExplicitMappingTests
{
public class JoinDynamicComponentTests
{
public class MyClass
{
public virtual int Code { get; set; }
public virtual string Name { get; set; }
public virtual MyOther Relation { get; set; }
public virtual MyOther JoinedRelation { get; set; }
public virtual IDictionary Attributes { get; set; }
public virtual IDictionary JoinedAttributes { get; set; }
}

public class MyOther
{
public int Id { get; set; }
}

public class MyClassMap : ClassMapping<MyClass>
{
public MyClassMap()
{

this.Component(p => p.Attributes,
new
{
IsVisible = false,
Hash = default(Guid),
Reference = default(MyOther)
},
m =>
{
m.Property(p => p.IsVisible);
m.Property(p => p.Hash);
m.ManyToOne(p => p.Reference);
});

Property(x => x.Code);
Join("JoinedAttributes", x =>
{
x.Property(p => p.Name);
x.Component(p => p.JoinedAttributes,
new
{
OtherReference = default(MyOther),
Description = string.Empty,
Age = 0,
},
m =>
{
m.ManyToOne(p => p.OtherReference);
m.Property(p => p.Description);
m.Property(p => p.Age);
});
x.ManyToOne(p => p.JoinedRelation);
});

ManyToOne(x => x.Relation);
}
}

[Test]
public void JoinedDynamicComponentShouldBeNestedInJoin()
{
var mapper = new ModelMapper();
mapper.AddMapping(typeof(MyClassMap));
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

var mappingXml = mapping.AsString();
Assert.AreEqual(mappingXml,
Copy link
Member

Choose a reason for hiding this comment

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

Test is failing because of line endings. Please change it to assert specific items you want to check. Please see other mapping-by-code tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced with 6 tests, checking the hbm mapping-by-code built model

@"<?xml version=""1.0"" encoding=""utf-8""?>
<hibernate-mapping xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" namespace=""NHibernate.Test.MappingByCode.ExplicitMappingTests"" assembly=""NHibernate.Test"" xmlns=""urn:nhibernate-mapping-2.2"">
<class name=""NHibernate.Test.MappingByCode.ExplicitMappingTests.JoinDynamicComponentTests+MyClass"">
<id type=""Int32"" />
<property name=""Code"" />
<many-to-one name=""Relation"" />
<dynamic-component name=""Attributes"">
<property name=""IsVisible"" type=""Boolean"" />
<property name=""Hash"" type=""Guid"" />
<many-to-one name=""Reference"" />
</dynamic-component>
<join table=""JoinedAttributes"">
<key column=""myclass_key"" />
<property name=""Name"" />
<many-to-one name=""JoinedRelation"" />
<dynamic-component name=""JoinedAttributes"">
<many-to-one name=""OtherReference"" />
<property name=""Description"" type=""String"" />
<property name=""Age"" type=""Int32"" />
</dynamic-component>
</join>
</class>
</hibernate-mapping>");
}

}
}
1 change: 1 addition & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@
<Compile Include="MappingByCode\ExplicitMappingTests\ConformistMappingRegistrationTests\UnionSubclassMappingRegistrationTest.cs" />
<Compile Include="MappingByCode\ExplicitMappingTests\DynamicComponentMappingTests.cs" />
<Compile Include="MappingByCode\ExplicitMappingTests\IdBagMappingTest.cs" />
<Compile Include="MappingByCode\ExplicitMappingTests\JoinDynamicComponentTests.cs" />
<Compile Include="MappingByCode\ExplicitMappingTests\MappingOfPrivateMembersOnRootEntity.cs" />
<Compile Include="MappingByCode\ExplicitMappingTests\NaturalIdTests.cs" />
<Compile Include="MappingByCode\ExplicitMappingTests\NestedComponetsTests.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Mapping/ByCode/IJoinMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IJoinAttributesMapper : IEntitySqlsMapper
void Fetch(FetchKind fetchMode);
}

public interface IJoinMapper : IJoinAttributesMapper, ICollectionPropertiesContainerMapper, IBasePlainPropertyContainerMapper { }
public interface IJoinMapper : IJoinAttributesMapper, IPropertyContainerMapper { }

public interface IJoinAttributesMapper<TEntity> : IEntitySqlsMapper where TEntity : class
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ protected override void RegisterComponentMapping<TComponent>(Expression<Func<TEn
base.RegisterComponentMapping(property, mapping);
}

protected override void RegisterDynamicComponentMapping<TComponent>(Expression<Func<TEntity, System.Collections.IDictionary>> property
, Action<IDynamicComponentMapper<TComponent>> mapping)
{
MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property);
ExplicitDeclarationsHolder.AddAsPropertySplit(new SplitDefinition(typeof(TEntity), splitGroupId, member));
base.RegisterDynamicComponentMapping(property, mapping);
}

protected override void RegisterManyToOneMapping<TProperty>(Expression<Func<TEntity, TProperty>> property, Action<IManyToOneMapper> mapping)
{
MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property);
Expand Down
4 changes: 4 additions & 0 deletions src/NHibernate/Mapping/ByCode/ModelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,10 @@ private void MapSplitProperties(System.Type propertiesContainerType, IEnumerable
{
MapComponent(member, memberPath, propertyType, propertiesContainer, propertiesContainerType);
}
else if (modelInspector.IsDynamicComponent(member))
{
MapDynamicComponent(member, memberPath, propertyType, propertiesContainer);
}
else
{
MapProperty(member, memberPath, propertiesContainer);
Expand Down