Skip to content

Commit

Permalink
Add KeyColumn override to AutoMapping<> (#468)
Browse files Browse the repository at this point in the history
* Deprecate inline subclass methods on AutoMapping

Closes #42
  • Loading branch information
hazzik authored Sep 20, 2020
1 parent eb245b9 commit 7729277
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ public void TestInheritanceMappingDoesntIncludeBaseTypePropertiesWithSubclass()
public void TestInheritanceOverridingMappingProperties()
{
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Override<ExampleClass>(t => t.JoinedSubClass<ExampleInheritedClass>("OverridenKey", p => p.Map(c => c.ExampleProperty, "columnName")))
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures");

new AutoMappingTester<ExampleClass>(autoMapper)
Expand All @@ -194,8 +193,7 @@ public void TestInheritanceOverridingMappingProperties()
public void TestInheritanceSubclassOverridingMappingProperties()
{
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Setup(x => x.IsDiscriminated = type => true)
.Override<ExampleClass>(t => t.SubClass<ExampleInheritedClass>("discriminator", p => p.Map(c => c.ExampleProperty, "columnName")))
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("discriminator"))
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures");

new AutoMappingTester<ExampleClass>(autoMapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Override<ExampleInheritedClass>(m => m.KeyColumn("MyKey"));

new AutoMappingTester<ExampleClass>(autoMapper)
.Element("//joined-subclass/key/column").HasAttribute("name", "MyKey");
}

[Test]
public void JoinedSubclassOverrideShouldOverrideExistingHasOne()
{
Expand Down
25 changes: 25 additions & 0 deletions src/FluentNHibernate/Automapping/AutoMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ IPropertyIgnorer IPropertyIgnorer.IgnoreProperties(Func<Member, bool> 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<TSubclass> JoinedSubClass<TSubclass>(string keyColumn, Action<AutoJoinedSubClassPart<TSubclass>> action)
where TSubclass : T
{
Expand All @@ -142,6 +143,7 @@ public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(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);
Expand All @@ -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<TSubclass> JoinedSubClass<TSubclass>(string keyColumn)
where TSubclass : T
{
return JoinedSubClass<TSubclass>(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<TSubclass> SubClass<TSubclass>(object discriminatorValue, Action<AutoSubClassPart<TSubclass>> action)
where TSubclass : T
{
Expand All @@ -174,12 +178,14 @@ public AutoSubClassPart<TSubclass> SubClass<TSubclass>(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<TSubclass> SubClass<TSubclass>(object discriminatorValue)
where TSubclass : T
{
return SubClass<TSubclass>(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);
Expand Down Expand Up @@ -212,5 +218,24 @@ public void Join(string table, Action<AutoJoinPart<T>> action)
return null;
}
#pragma warning restore 809

/// <summary>
/// Adds a column to the key for this subclass, if used
/// in a table-per-subclass strategy.
/// </summary>
/// <param name="column">Column name</param>
public void KeyColumn(string column)
{
KeyMapping key;

if (attributes.IsSpecified("Key"))
key = attributes.GetOrDefault<KeyMapping>("Key");
else
key = new KeyMapping();

key.AddColumn(Layer.UserSupplied, new ColumnMapping(column));

attributes.Set("Key", Layer.UserSupplied, key);
}
}
}
2 changes: 2 additions & 0 deletions src/FluentNHibernate/Automapping/IAutoClasslike.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 7729277

Please sign in to comment.