Skip to content

NH-3110 - Support Polymorphism in mapping by code #300

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
Sep 21, 2014
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
@@ -0,0 +1,26 @@
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.MappingByCode.IntegrationTests.NH3110
{
[TestFixture]
public class Fixture
{
[Test]
public void CanSetPolymorphism()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(rc =>
{
rc.Polymorphism(PolymorphismType.Explicit);
});

var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

var entity = mapping.RootClasses[0];
Assert.AreEqual(entity.polymorphism, HbmPolymorphismType.Explicit);
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@
<Compile Include="MappingByCode\IntegrationTests\NH2825\Fixture.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH2825\FixtureByCode.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH2825\Parent.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3110\Fixture.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3135\Fixture.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3135\Models.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3280\Domain.cs" />
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate/Mapping/ByCode/IClassMapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Cfg.MappingSchema;

namespace NHibernate.Mapping.ByCode
{
Expand All @@ -25,6 +26,7 @@ public interface IClassAttributesMapper : IEntityAttributesMapper, IEntitySqlsMa
void Filter(string filterName, Action<IFilterMapper> filterMapping);
void Where(string whereClause);
void SchemaAction(SchemaAction action);
void Polymorphism(PolymorphismType type);
}

public interface IClassMapper : IClassAttributesMapper, IPropertyContainerMapper
Expand Down Expand Up @@ -64,6 +66,7 @@ public interface IClassAttributesMapper<TEntity> : IEntityAttributesMapper, IEnt
void Filter(string filterName, Action<IFilterMapper> filterMapping);
void Where(string whereClause);
void SchemaAction(SchemaAction action);
void Polymorphism(PolymorphismType type);
}

public interface IClassMapper<TEntity> : IClassAttributesMapper<TEntity>, IPropertyContainerMapper<TEntity> where TEntity : class
Expand Down
4 changes: 4 additions & 0 deletions src/NHibernate/Mapping/ByCode/Impl/ClassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ public void Join(string splitGroupId, Action<IJoinMapper> splitMapping)
splitMapping(splitGroup);
}

public void Polymorphism(PolymorphismType type)
{
classMapping.polymorphism = (HbmPolymorphismType) Enum.Parse(typeof(HbmPolymorphismType), type.ToString());
}
#endregion

#region Implementation of IEntityAttributesMapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public void Schema(string schemaName)
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.Schema(schemaName));
}

public void Polymorphism(PolymorphismType type)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.Polymorphism(type));
}

#endregion

#region Implementation of IEntityAttributesMapper
Expand Down
23 changes: 23 additions & 0 deletions src/NHibernate/Mapping/ByCode/PolymorphismType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernate.Mapping.ByCode
{
/// <summary>
/// The possible types of polymorphism for IClassMapper.
/// </summary>
public enum PolymorphismType
{
/// <summary>
/// Implicit polymorphism
/// </summary>
Implicit,

/// <summary>
/// Explicit polymorphism
/// </summary>
Explicit
}
}
1 change: 1 addition & 0 deletions src/NHibernate/NHibernate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@
<Compile Include="Mapping\ByCode\Impl\DynamicComponentMapper.cs" />
<Compile Include="Mapping\ByCode\Impl\ManyToAnyMapper.cs" />
<Compile Include="Mapping\ByCode\Import.cs" />
<Compile Include="Mapping\ByCode\PolymorphismType.cs" />
<Compile Include="Mapping\ByCode\PropertyGeneration.cs" />
<Compile Include="Mapping\ByCode\PropertyToField.cs" />
<Compile Include="Mapping\ByCode\SimpleModelInspector.cs" />
Expand Down