From 77292770856dac034f95ae7dbe014acbd5c89b97 Mon Sep 17 00:00:00 2001 From: Alexander Zaytsev Date: Sun, 20 Sep 2020 13:32:06 +1200 Subject: [PATCH] Add KeyColumn override to AutoMapping<> (#468) * Deprecate inline subclass methods on AutoMapping Closes #42 --- .../AutoPersistenceModelTests.Inheritance.cs | 4 +-- .../AutoPersistenceModelTests.Overrides.cs | 11 ++++++++ .../Automapping/AutoMapping.cs | 25 +++++++++++++++++++ .../Automapping/IAutoClasslike.cs | 2 ++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Inheritance.cs b/src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Inheritance.cs index 2d0a4949a..159cac8ce 100644 --- a/src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Inheritance.cs +++ b/src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Inheritance.cs @@ -182,7 +182,6 @@ public void TestInheritanceMappingDoesntIncludeBaseTypePropertiesWithSubclass() public void TestInheritanceOverridingMappingProperties() { var autoMapper = AutoMap.AssemblyOf() - .Override(t => t.JoinedSubClass("OverridenKey", p => p.Map(c => c.ExampleProperty, "columnName"))) .Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures"); new AutoMappingTester(autoMapper) @@ -194,8 +193,7 @@ public void TestInheritanceOverridingMappingProperties() public void TestInheritanceSubclassOverridingMappingProperties() { var autoMapper = AutoMap.AssemblyOf() - .Setup(x => x.IsDiscriminated = type => true) - .Override(t => t.SubClass("discriminator", p => p.Map(c => c.ExampleProperty, "columnName"))) + .Override(t => t.DiscriminateSubClassesOnColumn("discriminator")) .Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures"); new AutoMappingTester(autoMapper) diff --git a/src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Overrides.cs b/src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Overrides.cs index 2185a3650..a1554a562 100644 --- a/src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Overrides.cs +++ b/src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Overrides.cs @@ -202,6 +202,17 @@ public void JoinedSubclassOverrideShouldOverrideExistingHasManyToMany() .Element("//joined-subclass/bag[@name='Children']/many-to-many").Exists(); } + [Test] + public void JoinedSubclassOverrideShouldOverrideKeyColumn() + { + var autoMapper = AutoMap.AssemblyOf() + .Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures") + .Override(m => m.KeyColumn("MyKey")); + + new AutoMappingTester(autoMapper) + .Element("//joined-subclass/key/column").HasAttribute("name", "MyKey"); + } + [Test] public void JoinedSubclassOverrideShouldOverrideExistingHasOne() { diff --git a/src/FluentNHibernate/Automapping/AutoMapping.cs b/src/FluentNHibernate/Automapping/AutoMapping.cs index 9e20d02e8..641ebad9c 100644 --- a/src/FluentNHibernate/Automapping/AutoMapping.cs +++ b/src/FluentNHibernate/Automapping/AutoMapping.cs @@ -128,6 +128,7 @@ IPropertyIgnorer IPropertyIgnorer.IgnoreProperties(Func predicate) return this; } + [Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")] public AutoJoinedSubClassPart JoinedSubClass(string keyColumn, Action> action) where TSubclass : T { @@ -142,6 +143,7 @@ public AutoJoinedSubClassPart JoinedSubClass(string keyCol return joinedclass; } + [Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")] public IAutoClasslike JoinedSubClass(Type type, string keyColumn) { var genericType = typeof (AutoJoinedSubClassPart<>).MakeGenericType(type); @@ -153,12 +155,14 @@ public IAutoClasslike JoinedSubClass(Type type, string keyColumn) return (IAutoClasslike)joinedclass; } + [Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")] public AutoJoinedSubClassPart JoinedSubClass(string keyColumn) where TSubclass : T { return JoinedSubClass(keyColumn, null); } + [Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")] public AutoSubClassPart SubClass(object discriminatorValue, Action> action) where TSubclass : T { @@ -174,12 +178,14 @@ public AutoSubClassPart SubClass(object discriminatorValue return subclass; } + [Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")] public AutoSubClassPart SubClass(object discriminatorValue) where TSubclass : T { return SubClass(discriminatorValue, null); } + [Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")] public IAutoClasslike SubClass(Type type, string discriminatorValue) { var genericType = typeof(AutoSubClassPart<>).MakeGenericType(type); @@ -212,5 +218,24 @@ public void Join(string table, Action> action) return null; } #pragma warning restore 809 + + /// + /// Adds a column to the key for this subclass, if used + /// in a table-per-subclass strategy. + /// + /// Column name + public void KeyColumn(string column) + { + KeyMapping key; + + if (attributes.IsSpecified("Key")) + key = attributes.GetOrDefault("Key"); + else + key = new KeyMapping(); + + key.AddColumn(Layer.UserSupplied, new ColumnMapping(column)); + + attributes.Set("Key", Layer.UserSupplied, key); + } } } \ No newline at end of file diff --git a/src/FluentNHibernate/Automapping/IAutoClasslike.cs b/src/FluentNHibernate/Automapping/IAutoClasslike.cs index 637bb79c2..ad09892a0 100644 --- a/src/FluentNHibernate/Automapping/IAutoClasslike.cs +++ b/src/FluentNHibernate/Automapping/IAutoClasslike.cs @@ -6,7 +6,9 @@ namespace FluentNHibernate.Automapping public interface IAutoClasslike : IMappingProvider { void DiscriminateSubClassesOnColumn(string column); + [Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")] IAutoClasslike JoinedSubClass(Type type, string keyColumn); + [Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")] IAutoClasslike SubClass(Type type, string discriminatorValue); void AlterModel(ClassMappingBase mapping); }