Skip to content

Support multiple formulas on one-to-one #1763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ public void CanSetFormula()
Assert.That(mapping.formula1, Is.EqualTo("SomeFormula"));
}

[Test]
public void CanSetMultipleFormulas()
{
var member = For<MyClass>.Property(c => c.Relation);
var mapping = new HbmOneToOne();
var mapper = new OneToOneMapper(member, mapping);

mapper.Formulas("formula1", "formula2", "formula3");
Assert.That(mapping.formula1, Is.Null);
Assert.That(mapping.formula, Has.Length.EqualTo(3));
Assert.That(
mapping.formula.Select(f => f.Text.Single()),
Is.EquivalentTo(new[] { "formula1", "formula2", "formula3" }));
}

[Test]
public void WhenSetFormulaWithNullThenSetFormulaWithNull()
{
Expand Down
19 changes: 18 additions & 1 deletion src/NHibernate/Mapping/ByCode/IOneToOneMapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Mapping.ByCode.Impl;
using NHibernate.Util;

namespace NHibernate.Mapping.ByCode
{
Expand All @@ -19,4 +21,19 @@ public interface IOneToOneMapper<T> : IOneToOneMapper
{
void PropertyReference<TProperty>(Expression<Func<T, TProperty>> reference);
}
}

// 6.0 TODO: move method into IOneToOneMapper
public static class OneToOneMapperExtensions
{
/// <summary>
/// Maps many formulas.
/// </summary>
/// <param name="mapper">The mapper.</param>
/// <param name="formulas">The formulas to map.</param>
public static void Formulas(this IOneToOneMapper mapper, params string[] formulas)
{
var o2oMapper = ReflectHelper.CastOrThrow<OneToOneMapper>(mapper, "Setting many formula");
o2oMapper.Formulas(formulas);
}
}
}
16 changes: 15 additions & 1 deletion src/NHibernate/Mapping/ByCode/Impl/OneToOneMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Cfg.MappingSchema;
Expand Down Expand Up @@ -115,6 +116,19 @@ public void Formula(string formula)
}
}

public void Formulas(params string[] formulas)
{
if (formulas == null)
throw new ArgumentNullException(nameof(formulas));

_oneToOne.formula1 = null;
_oneToOne.formula =
formulas
.Select(
f => new HbmFormula { Text = f.Split(StringHelper.LineSeparators, StringSplitOptions.None) })
.ToArray();
}

public void ForeignKey(string foreignKeyName)
{
_oneToOne.foreignkey = foreignKeyName;
Expand All @@ -140,4 +154,4 @@ public void PropertyReference<TProperty>(Expression<Func<T, TProperty>> referenc
PropertyReference(TypeExtensions.DecodeMemberAccessExpression(reference));
}
}
}
}