Skip to content

Commit 7729277

Browse files
authored
Add KeyColumn override to AutoMapping<> (#468)
* Deprecate inline subclass methods on AutoMapping Closes #42
1 parent eb245b9 commit 7729277

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Inheritance.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ public void TestInheritanceMappingDoesntIncludeBaseTypePropertiesWithSubclass()
182182
public void TestInheritanceOverridingMappingProperties()
183183
{
184184
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
185-
.Override<ExampleClass>(t => t.JoinedSubClass<ExampleInheritedClass>("OverridenKey", p => p.Map(c => c.ExampleProperty, "columnName")))
186185
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures");
187186

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

201199
new AutoMappingTester<ExampleClass>(autoMapper)

src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Overrides.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ public void JoinedSubclassOverrideShouldOverrideExistingHasManyToMany()
202202
.Element("//joined-subclass/bag[@name='Children']/many-to-many").Exists();
203203
}
204204

205+
[Test]
206+
public void JoinedSubclassOverrideShouldOverrideKeyColumn()
207+
{
208+
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
209+
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
210+
.Override<ExampleInheritedClass>(m => m.KeyColumn("MyKey"));
211+
212+
new AutoMappingTester<ExampleClass>(autoMapper)
213+
.Element("//joined-subclass/key/column").HasAttribute("name", "MyKey");
214+
}
215+
205216
[Test]
206217
public void JoinedSubclassOverrideShouldOverrideExistingHasOne()
207218
{

src/FluentNHibernate/Automapping/AutoMapping.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ IPropertyIgnorer IPropertyIgnorer.IgnoreProperties(Func<Member, bool> predicate)
128128
return this;
129129
}
130130

131+
[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
131132
public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyColumn, Action<AutoJoinedSubClassPart<TSubclass>> action)
132133
where TSubclass : T
133134
{
@@ -142,6 +143,7 @@ public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyCol
142143
return joinedclass;
143144
}
144145

146+
[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
145147
public IAutoClasslike JoinedSubClass(Type type, string keyColumn)
146148
{
147149
var genericType = typeof (AutoJoinedSubClassPart<>).MakeGenericType(type);
@@ -153,12 +155,14 @@ public IAutoClasslike JoinedSubClass(Type type, string keyColumn)
153155
return (IAutoClasslike)joinedclass;
154156
}
155157

158+
[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
156159
public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyColumn)
157160
where TSubclass : T
158161
{
159162
return JoinedSubClass<TSubclass>(keyColumn, null);
160163
}
161164

165+
[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
162166
public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue, Action<AutoSubClassPart<TSubclass>> action)
163167
where TSubclass : T
164168
{
@@ -174,12 +178,14 @@ public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue
174178
return subclass;
175179
}
176180

181+
[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
177182
public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue)
178183
where TSubclass : T
179184
{
180185
return SubClass<TSubclass>(discriminatorValue, null);
181186
}
182187

188+
[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
183189
public IAutoClasslike SubClass(Type type, string discriminatorValue)
184190
{
185191
var genericType = typeof(AutoSubClassPart<>).MakeGenericType(type);
@@ -212,5 +218,24 @@ public void Join(string table, Action<AutoJoinPart<T>> action)
212218
return null;
213219
}
214220
#pragma warning restore 809
221+
222+
/// <summary>
223+
/// Adds a column to the key for this subclass, if used
224+
/// in a table-per-subclass strategy.
225+
/// </summary>
226+
/// <param name="column">Column name</param>
227+
public void KeyColumn(string column)
228+
{
229+
KeyMapping key;
230+
231+
if (attributes.IsSpecified("Key"))
232+
key = attributes.GetOrDefault<KeyMapping>("Key");
233+
else
234+
key = new KeyMapping();
235+
236+
key.AddColumn(Layer.UserSupplied, new ColumnMapping(column));
237+
238+
attributes.Set("Key", Layer.UserSupplied, key);
239+
}
215240
}
216241
}

src/FluentNHibernate/Automapping/IAutoClasslike.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ namespace FluentNHibernate.Automapping
66
public interface IAutoClasslike : IMappingProvider
77
{
88
void DiscriminateSubClassesOnColumn(string column);
9+
[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
910
IAutoClasslike JoinedSubClass(Type type, string keyColumn);
11+
[Obsolete("Inline definitions of subclasses are depreciated. Please create a derived class from SubclassMap in the same way you do with ClassMap.")]
1012
IAutoClasslike SubClass(Type type, string discriminatorValue);
1113
void AlterModel(ClassMappingBase mapping);
1214
}

0 commit comments

Comments
 (0)