Skip to content

Commit

Permalink
Possible fix for nhibernate#330 - Feature: Support for additional att…
Browse files Browse the repository at this point in the history
…ributes in the

key-property element of an set (and equal)
  • Loading branch information
lanwin committed Oct 22, 2009
1 parent 0e45a91 commit 555ba46
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/FluentNHibernate/FluentNHibernate.csproj
Expand Up @@ -365,6 +365,7 @@
<Compile Include="MappingModel\ManyToManyTableNameVisitor.cs" />
<Compile Include="MappingModel\Output\XmlFilterDefinitionWriter.cs" />
<Compile Include="MappingModel\Output\XmlFilterWriter.cs" />
<Compile Include="Mapping\ColumnMappingCollection.cs" />
<Compile Include="Mapping\FilterDefinition.cs" />
<Compile Include="Mapping\FilterPart.cs" />
<Compile Include="Mapping\IFilterDefinition.cs" />
Expand Down Expand Up @@ -684,4 +685,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
57 changes: 57 additions & 0 deletions src/FluentNHibernate/Mapping/ColumnMappingCollection.cs
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using FluentNHibernate.MappingModel;

namespace FluentNHibernate.Mapping
{
public class ColumnMappingCollection<TParent>
{
private readonly IList<ColumnMapping> columnMappings = new List<ColumnMapping>();
private readonly TParent parent;

public ColumnMappingCollection(TParent parent)
{
this.parent = parent;
}

public TParent Add(string name)
{
columnMappings.Add(new ColumnMapping{Name = name});
return parent;
}

public TParent Add(ColumnMapping mapping)
{
columnMappings.Add(mapping);
return parent;
}

public TParent Add(params ColumnMapping[] mappings)
{
foreach (var mapping in mappings)
{
Add(mapping);
}
return parent;
}

public TParent Add(params string[] names)
{
foreach(var name in names)
{
Add(name);
}
return parent;
}

public TParent Clear()
{
columnMappings.Clear();
return parent;
}

public IList<ColumnMapping> List()
{
return new List<ColumnMapping>(columnMappings).AsReadOnly();
}
}
}
34 changes: 29 additions & 5 deletions src/FluentNHibernate/Mapping/OneToManyPart.cs
Expand Up @@ -9,7 +9,7 @@ namespace FluentNHibernate.Mapping
public class OneToManyPart<TChild> : ToManyBase<OneToManyPart<TChild>, TChild, OneToManyMapping>
{
private readonly Type entity;
private readonly ColumnNameCollection<OneToManyPart<TChild>> columns;
private readonly ColumnMappingCollection<OneToManyPart<TChild>> columns;
private readonly CollectionCascadeExpression<OneToManyPart<TChild>> cascade;
private readonly NotFoundExpression<OneToManyPart<TChild>> notFound;
private IndexManyToManyPart manyToManyIndex;
Expand All @@ -33,7 +33,7 @@ protected OneToManyPart(Type entity, MemberInfo member, Type collectionType)
this.entity = entity;
childType = collectionType;

columns = new ColumnNameCollection<OneToManyPart<TChild>>(this);
columns = new ColumnMappingCollection<OneToManyPart<TChild>>(this);
cascade = new CollectionCascadeExpression<OneToManyPart<TChild>>(this, value => collectionAttributes.Set(x => x.Cascade, value));
notFound = new NotFoundExpression<OneToManyPart<TChild>>(this, value => relationshipAttributes.Set(x => x.NotFound, value));

Expand All @@ -59,7 +59,7 @@ public override ICollectionMapping GetCollectionMapping()

foreach (var column in columns.List())
{
collection.Key.AddColumn(new ColumnMapping { Name = column });
collection.Key.AddColumn(column);
}

// HACK: shouldn't have to do this!
Expand Down Expand Up @@ -123,14 +123,38 @@ protected override ICollectionRelationshipMapping GetRelationship()
return mapping;
}

public OneToManyPart<TChild> KeyColumn(Action<ColumnMapping> configurator)
{
var mapping = new ColumnMapping {Name = entity.Name + "_id"};

if(configurator != null)
configurator(mapping);

KeyColumns.Clear();
KeyColumns.Add(mapping);
return this;
}

public OneToManyPart<TChild> KeyColumn(string columnName)
{
KeyColumns.Clear();
KeyColumns.Add(columnName);
KeyColumns.Add(new ColumnMapping { Name = columnName });
return this;
}

public OneToManyPart<TChild> KeyColumn(string columnName, Action<ColumnMapping> configurator)
{
var mapping = new ColumnMapping {Name = columnName};

if(configurator != null)
configurator(mapping);

KeyColumns.Clear();
KeyColumns.Add(mapping);
return this;
}

public ColumnNameCollection<OneToManyPart<TChild>> KeyColumns
public ColumnMappingCollection<OneToManyPart<TChild>> KeyColumns
{
get { return columns; }
}
Expand Down

0 comments on commit 555ba46

Please sign in to comment.