Skip to content

Commit 0953c53

Browse files
committed
implemented baseoffset for lists
1 parent 69bb044 commit 0953c53

File tree

7 files changed

+119
-9
lines changed

7 files changed

+119
-9
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,26 @@ public void CanSpecifyIndexBaseOffsetAndColumnForList()
284284
.ForMapping(map => map.HasMany(x => x.ListOfChildren)
285285
.AsList(index =>
286286
{
287-
index.Base(1);
287+
index.Offset(1);
288288
index.Column("ListIndex");
289289
}))
290290
.Element("class/list/list-index").HasAttribute("base", "1")
291291
.Element("class/list/list-index/column").HasAttribute("name", "ListIndex");
292292
}
293293

294+
[Test]
295+
[ExpectedException(typeof(NotSupportedException))]
296+
public void SpecifyingIndexBaseOffsetAndTypeForListThrowsNotSupportedException()
297+
{
298+
new MappingTester<OneToManyTarget>()
299+
.ForMapping(map => map.HasMany(x => x.ListOfChildren)
300+
.AsList(index =>
301+
{
302+
index.Offset(1);
303+
index.Type("ListIndex");
304+
}));
305+
}
306+
294307
[Test]
295308
public void CanSetTableName()
296309
{

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>
@@ -451,6 +447,7 @@
451447
<Compile Include="Mapping\Builders\FilterBuilder.cs" />
452448
<Compile Include="Mapping\Builders\IndexBuilder.cs" />
453449
<Compile Include="Mapping\Builders\KeyBuilder.cs" />
450+
<Compile Include="Mapping\Builders\ListIndexBuilder.cs" />
454451
<Compile Include="Mapping\Builders\ManyToManyBuilder.cs" />
455452
<Compile Include="Mapping\Builders\ManyToOneBuilder.cs" />
456453
<Compile Include="Mapping\Builders\MapBuilder.cs" />
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using FluentNHibernate.MappingModel;
3+
using FluentNHibernate.MappingModel.Collections;
4+
5+
namespace FluentNHibernate.Mapping.Builders
6+
{
7+
public class ListIndexBuilder
8+
{
9+
readonly IndexMapping mapping;
10+
readonly AttributeStore sharedColumnAttributes = new AttributeStore();
11+
12+
public ListIndexBuilder(IndexMapping mapping)
13+
{
14+
this.mapping = mapping;
15+
mapping.IsManyToMany = false;
16+
}
17+
18+
/// <summary>
19+
/// offset added to index in column
20+
/// </summary>
21+
/// <remarks>mutual exclusive with Type()</remarks>
22+
/// <param name="offset">offset</param>
23+
public void Offset(int offset)
24+
{
25+
if (mapping.HasValue(x => x.Type))
26+
throw new NotSupportedException("Offset is mutual exclusive with Type()");
27+
mapping.Offset = offset;
28+
}
29+
30+
/// <summary>
31+
/// Specifies the column name for the index or key of the dictionary.
32+
/// </summary>
33+
/// <param name="indexColumnName">Column name</param>
34+
public void Column(string indexColumnName)
35+
{
36+
mapping.AddColumn(new ColumnMapping(sharedColumnAttributes) { Name = indexColumnName });
37+
}
38+
39+
/// <summary>
40+
/// Specifies the type of the index/key column
41+
/// </summary>
42+
/// <remarks>mutual exclusive with offset</remarks>
43+
/// <typeparam name="TIndex">Index type</typeparam>
44+
public void Type<TIndex>()
45+
{
46+
if (mapping.HasValue(x => x.Offset))
47+
throw new NotSupportedException("Type() is mutual exclusive with Offset()");
48+
mapping.Type = new TypeReference(typeof(TIndex));
49+
}
50+
51+
/// <summary>
52+
/// Specifies the type of the index/key column
53+
/// </summary>
54+
/// <remarks>mutual exclusive with offset</remarks>
55+
/// <param name="type">Type</param>
56+
public void Type(Type type)
57+
{
58+
if (mapping.HasValue(x => x.Offset))
59+
throw new NotSupportedException("Type() is mutual exclusive with Offset()");
60+
mapping.Type = new TypeReference(type);
61+
}
62+
63+
/// <summary>
64+
/// Specifies the type of the index/key column
65+
/// </summary>
66+
/// <remarks>mutual exclusive with offset</remarks>
67+
/// <param name="type">Type</param>
68+
public void Type(string type)
69+
{
70+
if (mapping.HasValue(x => x.Offset))
71+
throw new NotSupportedException("Type() is mutual exclusive with Offset()");
72+
mapping.Type = new TypeReference(type);
73+
}
74+
}
75+
}

src/FluentNHibernate/Mapping/ToManyBase.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ public T AsList()
181181
/// Use a list collection with an index
182182
/// </summary>
183183
/// <param name="customIndexMapping">Index mapping</param>
184-
public T AsList(Action<IndexBuilder> customIndexMapping)
184+
public T AsList(Action<ListIndexBuilder> customIndexMapping)
185185
{
186186
collectionBuilder = attrs => new ListMapping(attrs);
187-
CreateIndexMapping(customIndexMapping);
187+
CreateListIndexMapping(customIndexMapping);
188188

189189
if (indexMapping.Columns.IsEmpty())
190190
indexMapping.AddDefaultColumn(new ColumnMapping { Name = "Index" });
@@ -254,6 +254,15 @@ private void CreateIndexMapping(Action<IndexBuilder> customIndex)
254254
customIndex(builder);
255255
}
256256

257+
private void CreateListIndexMapping(Action<ListIndexBuilder> customIndex)
258+
{
259+
indexMapping = new IndexMapping();
260+
var builder = new ListIndexBuilder(indexMapping);
261+
262+
if (customIndex != null)
263+
customIndex(builder);
264+
}
265+
257266
/// <summary>
258267
/// Map an element/value type
259268
/// </summary>

src/FluentNHibernate/Mapping/VersionPart.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public VersionPart(Type entity, Member property)
2929
/// <summary>
3030
/// Specify if this version is database generated
3131
/// </summary>
32-
public VersionGeneratedBuilder<IVersionMappingProvider> Generated
32+
public VersionGeneratedBuilder<VersionPart> Generated
3333
{
3434
get { return generated; }
3535
}

src/FluentNHibernate/MappingModel/Collections/IndexMapping.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public string EntityName
5050
get { return attributes.Get(x => x.EntityName); }
5151
set { attributes.Set(x => x.EntityName, value); }
5252
}
53+
54+
public int Offset
55+
{
56+
get { return attributes.Get(x => x.Offset); }
57+
set { attributes.Set(x => x.Offset, value); }
58+
}
5359

5460
public bool IsManyToMany { get; set; }
5561
public Type ContainingEntityType { get; set; }

src/FluentNHibernate/MappingModel/Output/XmlIndexWriter.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public override void ProcessIndex(IndexMapping mapping)
3030
if (mapping.IsManyToMany)
3131
WriteManyToManyIndex(mapping);
3232
else
33-
WriteIndex(mapping);
33+
if (mapping.HasValue(x => x.Offset))
34+
WriteListIndex(mapping);
35+
else
36+
WriteIndex(mapping);
3437
}
3538

3639
void WriteIndex(IndexMapping mapping)
@@ -41,6 +44,13 @@ void WriteIndex(IndexMapping mapping)
4144
element.WithAtt("type", mapping.Type);
4245
}
4346

47+
void WriteListIndex(IndexMapping mapping)
48+
{
49+
var element = document.AddElement("list-index");
50+
51+
element.WithAtt("base", mapping.Offset);
52+
}
53+
4454
void WriteManyToManyIndex(IndexMapping mapping)
4555
{
4656
var element = document.AddElement("index-many-to-many");

0 commit comments

Comments
 (0)