Skip to content

Commit 73ebfd9

Browse files
authored
Obsolete FluentNHibernate.Data.Entity (#685)
The `FluentNHibernate.Data.Entity` class is problematic because of the following: * It does not consider transient entities (see #376) * It does not uphold the requirement to have a stable `GetHashCode()` during the lifetime of the entity * It probably does have a bug working with proxies as it checks for a concrete type. Instead of using `FluentNHibernate.Data.Entity` please implement your own. Closes #376 +semver:feature
1 parent cee3136 commit 73ebfd9

File tree

5 files changed

+35
-25
lines changed

5 files changed

+35
-25
lines changed

src/FluentNHibernate.Testing/DomainModel/ConnectedTester.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ public NestedSubClassMap()
106106

107107
#pragma warning restore 612, 618
108108

109-
public class SuperRecord : Entity
109+
public class SuperRecord
110110
{
111+
public virtual long Id { get; set; }
111112
public virtual string Name { get; set; }
112113
}
113114

@@ -121,8 +122,9 @@ public class Child2Record : ChildRecord
121122
public virtual string Third { get; set; }
122123
}
123124

124-
public class Record : Entity
125+
public class Record
125126
{
127+
public virtual long Id { get; set; }
126128
public virtual string Name { get; set; }
127129
public virtual int Age { get; set; }
128130
public virtual string Location { get; set; }
@@ -136,8 +138,9 @@ public BinaryRecordMap()
136138
Map(x => x.BinaryValue).Not.Nullable();
137139
}
138140
}
139-
public class BinaryRecord : Entity
141+
public class BinaryRecord
140142
{
143+
public virtual long Id { get; set; }
141144
public virtual byte[] BinaryValue { get; set; }
142145
}
143146

@@ -149,8 +152,11 @@ public class CachedRecordMap : ClassMap<CachedRecord>
149152
Id(x => x.Id, "id");
150153
}
151154
}
152-
public class CachedRecord : Entity
153-
{ }
155+
156+
public class CachedRecord
157+
{
158+
public virtual long Id { get; set; }
159+
}
154160

155161
public class RecordFilter : FilterDefinition
156162
{
@@ -171,8 +177,9 @@ public RecordWithNullablePropertyMap()
171177
}
172178
}
173179

174-
public class RecordWithNullableProperty : Entity
180+
public class RecordWithNullableProperty
175181
{
182+
public virtual long Id { get; set; }
176183
public virtual string Name { get; set; }
177184
public virtual int? Age { get; set; }
178185
public virtual string Location { get; set; }

src/FluentNHibernate.Testing/DomainModel/EntityEquality.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
using System;
12
using FluentNHibernate.Data;
23
using NUnit.Framework;
34

45
namespace FluentNHibernate.Testing.DomainModel;
56

6-
[TestFixture]
7+
[TestFixture, Obsolete("Testing obsolete FluentNHibernate.Data.Entity")]
78
public class EntityEquality
89
{
910
[Test]
@@ -53,7 +54,7 @@ public void Two_entities_with_different_Ids_should_not_equal_each_other()
5354
[Test]
5455
public void Subclassed_entities_should_equal_each_other_with_same_Id()
5556
{
56-
var first = new TestSubEntity {Id = 99};
57+
var first = new TestSubEntity { Id = 99 };
5758
var second = new TestSubEntity { Id = 99 };
5859

5960
first.Equals(second).ShouldBeTrue();
@@ -77,18 +78,11 @@ public void Deep_subclassed_entities_should_not_equal_their_parent_classed_entit
7778
first.Equals(second).ShouldBeFalse();
7879
}
7980

80-
public class ConcreteEntity : Entity
81-
{}
81+
public class ConcreteEntity : Entity;
8282

83-
public class TestSubEntity : ConcreteEntity
84-
{
85-
}
83+
public class TestSubEntity : ConcreteEntity;
8684

87-
public class AnotherSubEntity : ConcreteEntity
88-
{
89-
}
85+
public class AnotherSubEntity : ConcreteEntity;
9086

91-
public class DeepSubEntity : TestSubEntity
92-
{
93-
}
87+
public class DeepSubEntity : TestSubEntity;
9488
}

src/FluentNHibernate.Testing/DomainModel/InverseOneToManyTester.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using FluentNHibernate.Mapping;
34
using NUnit.Framework;
@@ -28,14 +29,14 @@ public void Should_handle_inverse_collections()
2829
{
2930
var artists = new List<Artist>
3031
{
31-
new Artist {Name = "Artist1"},
32-
new Artist {Name = "Artist2"}
32+
new Artist { Name = "Artist1" },
33+
new Artist { Name = "Artist2" }
3334
};
3435

3536
new PersistenceSpecification<Genre>(_source)
3637
.CheckProperty(g => g.Id, 1L)
3738
.CheckProperty(g => g.Name, "Genre")
38-
.CheckComponentList(g => g.Artists, artists, (g, a) => g.AddArtist(a))
39+
.CheckInverseList(g => g.Artists, artists, new FuncEqualityComparer<Artist>(a => a.Id), (genre, artist) => genre.AddArtist(artist))
3940
.VerifyTheMappings();
4041
}
4142
}
@@ -79,3 +80,9 @@ public GenreMap()
7980
.Inverse();
8081
}
8182
}
83+
84+
class FuncEqualityComparer<T>(Func<T, object> func) : EqualityComparer<T>
85+
{
86+
public override bool Equals(T x, T y) => Equals(func(x), func(y));
87+
public override int GetHashCode(T obj) => throw new NotSupportedException();
88+
}

src/FluentNHibernate.Testing/DomainModel/Music.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Collections.Generic;
2-
using FluentNHibernate.Data;
32

43
namespace FluentNHibernate.Testing.DomainModel;
54

6-
public class Artist : Entity
5+
public class Artist
76
{
7+
public virtual long Id { get; set; }
88
public virtual string Name { get; set; }
99
public virtual ISet<Album> Albums { get; set; }
1010
public virtual Genre Genre { get; set; }
@@ -15,8 +15,9 @@ public Artist()
1515
}
1616
}
1717

18-
public class Genre : Entity
18+
public class Genre
1919
{
20+
public virtual long Id { get; set; }
2021
public virtual string Name { get; set; }
2122
public virtual IList<Artist> Artists { get; set; }
2223

src/FluentNHibernate/Data/Entity.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FluentNHibernate.Data;
44

55
[Serializable]
6+
[Obsolete("Please do not use this class and implement your own as this base class is problematic because it does not consider transient entities and a requirement to have immutable GetHashCode().")]
67
public abstract class Entity : IEquatable<Entity>
78
{
89
public virtual long Id { get; set; }

0 commit comments

Comments
 (0)