Skip to content

Commit 08f59be

Browse files
committed
Merge branch 'master' of https://github.com/firo222/fluent-nhibernate into firo222-master
Conflicts: src/FluentNHibernate/FluentNHibernate.csproj src/FluentNHibernate/Mapping/ToManyBase.cs src/FluentNHibernate/MappingModel/Collections/IndexMapping.cs src/FluentNHibernate/MappingModel/Output/XmlIndexWriter.cs
2 parents d996a30 + 0953c53 commit 08f59be

File tree

6 files changed

+139
-10
lines changed

6 files changed

+139
-10
lines changed

src/FluentNHibernate.Testing/DomainModel/Mapping/OneToManyTester.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,42 @@ public void CanSpecifyIndexColumnAndTypeForList()
270270
{
271271
new MappingTester<OneToManyTarget>()
272272
.ForMapping(map => map.HasMany(x => x.ListOfChildren)
273-
.AsList(index => index
274-
.Column("ListIndex")
275-
.Type<int>()
276-
))
273+
.AsList(index =>
274+
{
275+
index.Column("ListIndex");
276+
index.Type<int>();
277+
}))
277278
.Element("class/list/index").HasAttribute("type", typeof(int).AssemblyQualifiedName)
278279
.Element("class/list/index/column").HasAttribute("name", "ListIndex");
279280
}
280281

282+
[Test]
283+
public void CanSpecifyIndexBaseOffsetAndColumnForList()
284+
{
285+
new MappingTester<OneToManyTarget>()
286+
.ForMapping(map => map.HasMany(x => x.ListOfChildren)
287+
.AsList(index =>
288+
{
289+
index.Offset(1);
290+
index.Column("ListIndex");
291+
}))
292+
.Element("class/list/list-index").HasAttribute("base", "1")
293+
.Element("class/list/list-index/column").HasAttribute("name", "ListIndex");
294+
}
295+
296+
[Test]
297+
[ExpectedException(typeof(NotSupportedException))]
298+
public void SpecifyingIndexBaseOffsetAndTypeForListThrowsNotSupportedException()
299+
{
300+
new MappingTester<OneToManyTarget>()
301+
.ForMapping(map => map.HasMany(x => x.ListOfChildren)
302+
.AsList(index =>
303+
{
304+
index.Offset(1);
305+
index.Type("ListIndex");
306+
}));
307+
}
308+
281309
[Test]
282310
public void CanSetTableName()
283311
{

src/FluentNHibernate/FluentNHibernate.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@
5858
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
5959
</PropertyGroup>
6060
<ItemGroup>
61-
<Reference Include="Castle.DynamicProxy2, Version=2.1.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
62-
<SpecificVersion>False</SpecificVersion>
63-
<HintPath>..\..\tools\NHibernate\Castle.DynamicProxy2.dll</HintPath>
64-
</Reference>
6561
<Reference Include="Iesi.Collections, Version=1.0.0.3, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
6662
<SpecificVersion>False</SpecificVersion>
6763
<HintPath>..\..\tools\NHibernate\Iesi.Collections.dll</HintPath>
@@ -438,6 +434,7 @@
438434
<Compile Include="MappingModel\Output\XmlNaturalIdWriter.cs" />
439435
<Compile Include="Mapping\Access.cs" />
440436
<Compile Include="Mapping\CollectionTypeResolver.cs" />
437+
<Compile Include="Mapping\ListIndexPart.cs" />
441438
<Compile Include="Mapping\Laziness.cs" />
442439
<Compile Include="Mapping\MappingProviderStore.cs" />
443440
<Compile Include="Mapping\MemberAccessResolver.cs" />
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using FluentNHibernate.MappingModel;
3+
using FluentNHibernate.MappingModel.Collections;
4+
5+
namespace FluentNHibernate.Mapping
6+
{
7+
public class ListIndexPart
8+
{
9+
readonly IndexMapping mapping;
10+
readonly AttributeStore sharedColumnAttributes = new AttributeStore();
11+
12+
public ListIndexPart(IndexMapping mapping)
13+
{
14+
this.mapping = mapping;
15+
}
16+
17+
/// <summary>
18+
/// offset added to index in column
19+
/// </summary>
20+
/// <remarks>mutual exclusive with Type()</remarks>
21+
/// <param name="offset">offset</param>
22+
public void Offset(int offset)
23+
{
24+
if (mapping.HasValue(x => x.Type))
25+
throw new NotSupportedException("Offset is mutual exclusive with Type()");
26+
mapping.Offset = offset;
27+
}
28+
29+
/// <summary>
30+
/// Specifies the column name for the index or key of the dictionary.
31+
/// </summary>
32+
/// <param name="indexColumnName">Column name</param>
33+
public void Column(string indexColumnName)
34+
{
35+
mapping.AddColumn(new ColumnMapping(sharedColumnAttributes) { Name = indexColumnName });
36+
}
37+
38+
/// <summary>
39+
/// Specifies the type of the index/key column
40+
/// </summary>
41+
/// <remarks>mutual exclusive with offset</remarks>
42+
/// <typeparam name="TIndex">Index type</typeparam>
43+
public void Type<TIndex>()
44+
{
45+
if (mapping.HasValue(x => x.Offset))
46+
throw new NotSupportedException("Type() is mutual exclusive with Offset()");
47+
mapping.Type = new TypeReference(typeof(TIndex));
48+
}
49+
50+
/// <summary>
51+
/// Specifies the type of the index/key column
52+
/// </summary>
53+
/// <remarks>mutual exclusive with offset</remarks>
54+
/// <param name="type">Type</param>
55+
public void Type(Type type)
56+
{
57+
if (mapping.HasValue(x => x.Offset))
58+
throw new NotSupportedException("Type() is mutual exclusive with Offset()");
59+
mapping.Type = new TypeReference(type);
60+
}
61+
62+
/// <summary>
63+
/// Specifies the type of the index/key column
64+
/// </summary>
65+
/// <remarks>mutual exclusive with offset</remarks>
66+
/// <param name="type">Type</param>
67+
public void Type(string type)
68+
{
69+
if (mapping.HasValue(x => x.Offset))
70+
throw new NotSupportedException("Type() is mutual exclusive with Offset()");
71+
mapping.Type = new TypeReference(type);
72+
}
73+
}
74+
}

src/FluentNHibernate/Mapping/ToManyBase.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ public T AsList()
170170
/// Use a list collection with an index
171171
/// </summary>
172172
/// <param name="customIndexMapping">Index mapping</param>
173-
public T AsList(Action<IndexPart> customIndexMapping)
173+
public T AsList(Action<ListIndexPart> customIndexMapping)
174174
{
175175
collectionBuilder = attrs => CollectionMapping.List(attrs);
176-
CreateIndexMapping(customIndexMapping);
176+
CreateListIndexMapping(customIndexMapping);
177177

178178
if (indexMapping.Columns.IsEmpty())
179179
indexMapping.AddDefaultColumn(new ColumnMapping { Name = "Index" });
@@ -390,6 +390,15 @@ private void CreateIndexMapping(Action<IndexPart> customIndex)
390390
#pragma warning restore 612,618
391391
}
392392

393+
private void CreateListIndexMapping(Action<ListIndexPart> customIndex)
394+
{
395+
indexMapping = new IndexMapping();
396+
var builder = new ListIndexPart(indexMapping);
397+
398+
if (customIndex != null)
399+
customIndex(builder);
400+
}
401+
393402
/// <summary>
394403
/// Map an element/value type
395404
/// </summary>

src/FluentNHibernate/MappingModel/Collections/IndexMapping.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public TypeReference Type
3333
set { attributes.Set(x => x.Type, value); }
3434
}
3535

36+
public int Offset
37+
{
38+
get { return attributes.Get(x => x.Offset); }
39+
set { attributes.Set(x => x.Offset, value); }
40+
}
41+
3642
public Type ContainingEntityType { get; set; }
3743

3844
public IDefaultableEnumerable<ColumnMapping> Columns

src/FluentNHibernate/MappingModel/Output/XmlIndexWriter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,27 @@ public override void ProcessIndex(IndexMapping mapping)
2626
{
2727
document = new XmlDocument();
2828

29+
if (mapping.HasValue(x => x.Offset))
30+
WriteListIndex(mapping);
31+
else
32+
WriteIndex(mapping);
33+
}
34+
35+
void WriteIndex(IndexMapping mapping)
36+
{
2937
var element = document.AddElement("index");
3038

3139
if (mapping.HasValue(x => x.Type))
3240
element.WithAtt("type", mapping.Type);
3341
}
3442

43+
void WriteListIndex(IndexMapping mapping)
44+
{
45+
var element = document.AddElement("list-index");
46+
47+
element.WithAtt("base", mapping.Offset);
48+
}
49+
3550
public override void Visit(ColumnMapping columnMapping)
3651
{
3752
var writer = serviceLocator.GetWriter<ColumnMapping>();

0 commit comments

Comments
 (0)