Skip to content

Commit 21cb167

Browse files
committed
fixing #190 - should recognize byte and short as identity
1 parent 1e82f30 commit 21cb167

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using FluentNHibernate.Automapping;
6+
using FluentNHibernate.Testing.Automapping;
7+
using NUnit.Framework;
8+
9+
namespace FluentNHibernate.Testing.AutoMapping.Apm
10+
{
11+
[TestFixture]
12+
public class IdentityTests
13+
{
14+
[Test]
15+
public void CanUseIdentityGeneratorForIntIds()
16+
{
17+
var autoMapper = AutoMap.Source(new StubTypeSource(typeof(ClassWithIntId)));
18+
19+
new AutoMappingTester<ClassWithIntId>(autoMapper)
20+
.Element("class/id/generator").HasAttribute("class", "identity");
21+
}
22+
23+
[Test]
24+
public void CanUseIdentityGeneratorForLongIds()
25+
{
26+
var autoMapper = AutoMap.Source(new StubTypeSource(typeof(ClassWithLongId)));
27+
28+
new AutoMappingTester<ClassWithLongId>(autoMapper)
29+
.Element("class/id/generator").HasAttribute("class", "identity");
30+
}
31+
32+
[Test]
33+
public void CanUseIdentityGeneratorForByteIds()
34+
{
35+
var autoMapper = AutoMap.Source(new StubTypeSource(typeof(ByteId)));
36+
37+
new AutoMappingTester<ByteId>(autoMapper)
38+
.Element("class/id/generator").HasAttribute("class", "identity");
39+
}
40+
41+
[Test]
42+
public void CanUseIdentityGeneratorForShortIds()
43+
{
44+
var autoMapper = AutoMap.Source(new StubTypeSource(typeof(ShortId)));
45+
46+
new AutoMappingTester<ShortId>(autoMapper)
47+
.Element("class/id/generator").HasAttribute("class", "identity");
48+
}
49+
}
50+
51+
class ClassWithIntId
52+
{
53+
public virtual int Id { get; set; }
54+
}
55+
56+
class ClassWithLongId
57+
{
58+
public virtual long Id { get; set; }
59+
}
60+
61+
class ByteId
62+
{
63+
public virtual byte Id { get; set; }
64+
}
65+
66+
class ShortId
67+
{
68+
public virtual short Id { get; set; }
69+
}
70+
}

src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@
101101
<Compile Include="AutoMapping\Apm\AlterationCollectionTests.cs" />
102102
<Compile Include="AutoMapping\Apm\AlterationTests.cs" />
103103
<Compile Include="AutoMapping\Apm\ConcreteBaseClassTests.cs" />
104-
<Compile Include="AutoMapping\Apm\Conventions\VersionConventionTests.cs" />
105-
<Compile Include="AutoMapping\DefaultAutoMappingConfigurationTests.cs" />
104+
<Compile Include="AutoMapping\Apm\Conventions\VersionConventionTests.cs" />
105+
<Compile Include="AutoMapping\DefaultAutoMappingConfigurationTests.cs" />
106+
<Compile Include="AutoMapping\Apm\IdentityTests.cs" />
106107
<Compile Include="AutoMapping\Overrides\ReferenceComponentOverrides.cs" />
107108
<Compile Include="AutoMapping\Overrides\AutoMappingOverrideAlterationTests.cs" />
108109
<Compile Include="AutoMapping\Apm\CacheOverrideTests.cs" />

src/FluentNHibernate/Automapping/Steps/IdentityStep.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
using System;
2+
using System.Collections.Generic;
23
using FluentNHibernate.Mapping;
34
using FluentNHibernate.MappingModel;
45
using FluentNHibernate.MappingModel.ClassBased;
5-
using FluentNHibernate.MappingModel.Collections;
66
using FluentNHibernate.MappingModel.Identity;
77

88
namespace FluentNHibernate.Automapping.Steps
99
{
1010
public class IdentityStep : IAutomappingStep
1111
{
1212
private readonly IAutomappingConfiguration cfg;
13+
readonly List<Type> identityCompatibleTypes = new List<Type> { typeof(long), typeof(int), typeof(short), typeof(byte) };
1314

1415
public IdentityStep(IAutomappingConfiguration cfg)
1516
{
@@ -54,14 +55,14 @@ void SetDefaultAccess(Member member, IdMapping mapping)
5455
mapping.Set(x => x.Access, Layer.Defaults, cfg.GetAccessStrategyForReadOnlyProperty(member).ToString());
5556
}
5657

57-
static GeneratorMapping GetDefaultGenerator(Member property)
58+
GeneratorMapping GetDefaultGenerator(Member property)
5859
{
5960
var generatorMapping = new GeneratorMapping();
6061
var defaultGenerator = new GeneratorBuilder(generatorMapping, property.PropertyType, Layer.Defaults);
6162

6263
if (property.PropertyType == typeof(Guid))
6364
defaultGenerator.GuidComb();
64-
else if (property.PropertyType == typeof(int) || property.PropertyType == typeof(long))
65+
else if (identityCompatibleTypes.Contains(property.PropertyType))
6566
defaultGenerator.Identity();
6667
else
6768
defaultGenerator.Assigned();

0 commit comments

Comments
 (0)