Skip to content

Commit 9ed5dfd

Browse files
committed
Class Customizers with mapping of not visible id/version
SVN: trunk@5791
1 parent 307ebe6 commit 9ed5dfd

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/NHibernate.Test/MappingByCode/ExpliticMappingTests/MappingOfPrivateMembersOnRootEntity.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class MappingOfPrivateMembersOnRootEntity
1111
public class MyClass
1212
{
1313
private int id;
14+
private int version;
1415
private string something;
1516
}
1617

@@ -20,11 +21,12 @@ public void MapClassWithIdAndProperty()
2021
var mapper = new ModelMapper();
2122
mapper.Class<MyClass>(ca =>
2223
{
23-
ca.Id(ForClass<MyClass>.Field("id"), map =>
24+
ca.Id("id", map =>
2425
{
2526
map.Column("MyClassId");
2627
map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
2728
});
29+
ca.Version("version", map => { });
2830
ca.Property("something", map => map.Length(150));
2931
});
3032
var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
@@ -39,6 +41,8 @@ public void MapClassWithIdAndProperty()
3941
hbmGenerator.@class.Should().Be("hilo");
4042
hbmGenerator.param[0].name.Should().Be("max_low");
4143
hbmGenerator.param[0].GetText().Should().Be("100");
44+
var hbmVersion = hbmClass.Version;
45+
hbmVersion.name.Should().Be("version");
4246
var hbmProperty = hbmClass.Properties.OfType<HbmProperty>().Single();
4347
hbmProperty.name.Should().Be("something");
4448
hbmProperty.access.Should().Be("field");

src/NHibernate/Mapping/ByCode/IClassMapper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ public interface IClassAttributesMapper<TEntity> : IEntityAttributesMapper, IEnt
4040
{
4141
void Id<TProperty>(Expression<Func<TEntity, TProperty>> idProperty);
4242
void Id<TProperty>(Expression<Func<TEntity, TProperty>> idProperty, Action<IIdMapper> idMapper);
43-
void Id(FieldInfo idProperty, Action<IIdMapper> idMapper);
43+
void Id(string notVidiblePropertyOrFieldName, Action<IIdMapper> idMapper);
4444

4545
void ComponentAsId<TComponent>(Expression<Func<TEntity, TComponent>> idProperty) where TComponent : class;
4646
void ComponentAsId<TComponent>(Expression<Func<TEntity, TComponent>> idProperty, Action<IComponentAsIdMapper<TComponent>> idMapper) where TComponent : class;
47+
void ComponentAsId<TComponent>(string notVidiblePropertyOrFieldName) where TComponent : class;
48+
void ComponentAsId<TComponent>(string notVidiblePropertyOrFieldName, Action<IComponentAsIdMapper<TComponent>> idMapper) where TComponent : class;
4749

4850
void ComposedId(Action<IComposedIdMapper<TEntity>> idPropertiesMapping);
4951

@@ -54,6 +56,7 @@ public interface IClassAttributesMapper<TEntity> : IEntityAttributesMapper, IEnt
5456
void Schema(string schemaName);
5557
void Mutable(bool isMutable);
5658
void Version<TProperty>(Expression<Func<TEntity, TProperty>> versionProperty, Action<IVersionMapper> versionMapping);
59+
void Version(string notVidiblePropertyOrFieldName, Action<IVersionMapper> versionMapping);
5760
void NaturalId(Action<IBasePlainPropertyContainerMapper<TEntity>> naturalIdPropertiesMapping, Action<INaturalIdAttributesMapper> naturalIdMapping);
5861
void NaturalId(Action<IBasePlainPropertyContainerMapper<TEntity>> naturalIdPropertiesMapping);
5962
void Cache(Action<ICacheMapper> cacheMapping);

src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/ClassCustomizer.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ public void Id<TProperty>(Expression<Func<TEntity, TProperty>> idProperty, Actio
4343
CustomizersHolder.AddCustomizer(typeof (TEntity), m => m.Id(member, idMapper));
4444
}
4545

46-
public void Id(FieldInfo idProperty, Action<IIdMapper> idMapper)
46+
public void Id(string notVidiblePropertyOrFieldName, Action<IIdMapper> idMapper)
4747
{
48-
if (idProperty != null)
48+
MemberInfo member = null;
49+
if (notVidiblePropertyOrFieldName != null)
4950
{
50-
ExplicitDeclarationsHolder.AddAsPoid(idProperty);
51+
member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
52+
ExplicitDeclarationsHolder.AddAsPoid(member);
5153
}
52-
CustomizersHolder.AddCustomizer(typeof(TEntity), m => m.Id(idProperty, idMapper));
54+
CustomizersHolder.AddCustomizer(typeof(TEntity), m => m.Id(member, idMapper));
5355
}
5456

5557
public void ComponentAsId<TComponent>(Expression<Func<TEntity, TComponent>> idProperty) where TComponent : class
@@ -64,6 +66,18 @@ public void ComponentAsId<TComponent>(Expression<Func<TEntity, TComponent>> idPr
6466
idMapper(new ComponentAsIdCustomizer<TComponent>(ExplicitDeclarationsHolder, CustomizersHolder, propertyPath));
6567
}
6668

69+
public void ComponentAsId<TComponent>(string notVidiblePropertyOrFieldName) where TComponent : class
70+
{
71+
ComponentAsId<TComponent>(notVidiblePropertyOrFieldName, x => { });
72+
}
73+
74+
public void ComponentAsId<TComponent>(string notVidiblePropertyOrFieldName, Action<IComponentAsIdMapper<TComponent>> idMapper) where TComponent : class
75+
{
76+
var member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
77+
var propertyPath = new PropertyPath(null, member);
78+
idMapper(new ComponentAsIdCustomizer<TComponent>(ExplicitDeclarationsHolder, CustomizersHolder, propertyPath));
79+
}
80+
6781
public void ComposedId(Action<IComposedIdMapper<TEntity>> idPropertiesMapping)
6882
{
6983
idPropertiesMapping(new ComposedIdCustomizer<TEntity>(ExplicitDeclarationsHolder, CustomizersHolder));
@@ -110,6 +124,13 @@ public void Version<TProperty>(Expression<Func<TEntity, TProperty>> versionPrope
110124
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.Version(member, versionMapping));
111125
}
112126

127+
public void Version(string notVidiblePropertyOrFieldName, Action<IVersionMapper> versionMapping)
128+
{
129+
var member = typeof(TEntity).GetPropertyOrFieldMatchingName(notVidiblePropertyOrFieldName);
130+
ExplicitDeclarationsHolder.AddAsVersionProperty(member);
131+
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.Version(member, versionMapping));
132+
}
133+
113134
public void NaturalId(Action<IBasePlainPropertyContainerMapper<TEntity>> naturalIdPropertiesMapping, Action<INaturalIdAttributesMapper> naturalIdMapping)
114135
{
115136
naturalIdPropertiesMapping(new NaturalIdCustomizer<TEntity>(ExplicitDeclarationsHolder, CustomizersHolder));

0 commit comments

Comments
 (0)