Permalink
Browse files

Possible fix for #330 - Feature: Support for additional attributes in…

… the

key-property element of an set (and equal)
  • Loading branch information...
1 parent 0e45a91 commit 555ba46e60e60a13fe75b2c30460a4db2f6faacd @lanwin committed Oct 22, 2009
View
3 src/FluentNHibernate/FluentNHibernate.csproj
@@ -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" />
@@ -684,4 +685,4 @@
<Target Name="AfterBuild">
</Target>
-->
-</Project>
+</Project>
View
57 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();
+ }
+ }
+}
View
34 src/FluentNHibernate/Mapping/OneToManyPart.cs
@@ -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;
@@ -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));
@@ -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!
@@ -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; }
}

0 comments on commit 555ba46

Please sign in to comment.