Skip to content

Commit

Permalink
Add DiscriminatorValue override to AutoMapping (#469)
Browse files Browse the repository at this point in the history
+semver:feature
  • Loading branch information
hazzik authored Sep 20, 2020
1 parent 7729277 commit e9e574d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,9 @@ public void JoinedSubclassOverrideShouldOverrideExistingReferencesAny()
[Test]
public void SubclassOverrideShouldOverrideExistingProperty()
{
#pragma warning disable 612,618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(c => c.Map(x => x.ExampleProperty).Column("test"));

new AutoMappingTester<ExampleClass>(autoMapper)
Expand Down Expand Up @@ -290,11 +288,9 @@ public void SubclassOverrideShouldOverrideExistingComponent()
[Test]
public void SubclassOverrideShouldOverrideExistingHasMany()
{
#pragma warning disable 612,618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m => m.HasMany(x => x.Children).Inverse());

new AutoMappingTester<ExampleClass>(autoMapper)
Expand All @@ -304,11 +300,9 @@ public void SubclassOverrideShouldOverrideExistingHasMany()
[Test]
public void SubclassOverrideShouldOverrideExistingHasManyToMany()
{
#pragma warning disable 612,618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m => m.HasManyToMany(x => x.Children).Inverse());

new AutoMappingTester<ExampleClass>(autoMapper)
Expand All @@ -319,11 +313,9 @@ public void SubclassOverrideShouldOverrideExistingHasManyToMany()
[Test]
public void SubclassOverrideShouldOverrideExistingHasOne()
{
#pragma warning disable 612,618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m => m.HasOne(x => x.Parent));

new AutoMappingTester<ExampleClass>(autoMapper)
Expand All @@ -334,11 +326,9 @@ public void SubclassOverrideShouldOverrideExistingHasOne()
[Test]
public void SubclassOverrideShouldOverrideExistingReferences()
{
#pragma warning disable 612, 618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m => m.References(x => x.Parent).Access.Field());

new AutoMappingTester<ExampleClass>(autoMapper)
Expand All @@ -348,11 +338,9 @@ public void SubclassOverrideShouldOverrideExistingReferences()
[Test]
public void SubclassOverrideShouldOverrideExistingReferencesAny()
{
#pragma warning disable 612, 618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m =>
m.ReferencesAny(x => x.DictionaryChild)
.EntityIdentifierColumn("one")
Expand All @@ -363,5 +351,18 @@ public void SubclassOverrideShouldOverrideExistingReferencesAny()
.Element("//subclass/any[@name='DictionaryChild']").Exists()
.Element("//subclass/map[@name='DictionaryChild']").DoesntExist();
}


[Test]
public void SubclassOverrideShouldOverrideDiscriminatorValue()
{
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m => m.DiscriminatorValue(1));

new AutoMappingTester<ExampleClass>(autoMapper)
.Element("//subclass").HasAttribute("discriminator-value", "1");
}
}
}
10 changes: 10 additions & 0 deletions src/FluentNHibernate/Automapping/AutoMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,15 @@ public void KeyColumn(string column)

attributes.Set("Key", Layer.UserSupplied, key);
}

/// <summary>
/// Set the discriminator value, if this entity is in a table-per-class-hierarchy
/// mapping strategy.
/// </summary>
/// <param name="discriminatorValue">Discriminator value</param>
public void DiscriminatorValue(object discriminatorValue)
{
attributes.Set("DiscriminatorValue", Layer.UserSupplied, discriminatorValue);
}
}
}

0 comments on commit e9e574d

Please sign in to comment.