Skip to content

Commit f4803a4

Browse files
authored
Merge pull request #102 from beercsaba/test-localization-projectiontocustomtypes
Localization extension: Support for inherited from abstract class localization fields
2 parents bcd087d + 4efec2b commit f4803a4

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (C) 2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System.Globalization;
6+
using Xtensive.Orm.Model;
7+
8+
namespace Xtensive.Orm.Localization.Tests.Model
9+
{
10+
[HierarchyRoot(InheritanceSchema = InheritanceSchema.ConcreteTable)]
11+
public abstract class AbstractDictionary : Entity
12+
{
13+
[Key, Field]
14+
public int Id { get; private set; }
15+
16+
[Field(Nullable = false, Length = 64)]
17+
public string Identifier { get; set; }
18+
19+
// abstract property
20+
public abstract string Name { get; set; }
21+
22+
protected AbstractDictionary(Session session) : base(session) { }
23+
}
24+
25+
public abstract class AbstractLocalizableDictionary<T, TT> : AbstractDictionary, ILocalizable<TT>
26+
where T : AbstractLocalizableDictionary<T, TT>
27+
where TT : AbstractDictionaryLocalization<T, TT>
28+
{
29+
public override string Name { get => Localizations.Current.Name; set => Localizations.Current.Name = value; }
30+
31+
[Field]
32+
public LocalizationSet<TT> Localizations { get; private set; }
33+
34+
protected AbstractLocalizableDictionary(Session session) : base(session) { }
35+
}
36+
37+
public abstract class AbstractDictionaryLocalization<T, TT> : Localization<T>
38+
where TT : AbstractDictionaryLocalization<T, TT>
39+
where T : AbstractLocalizableDictionary<T, TT>
40+
{
41+
[Field(Nullable = false, Length = 512)]
42+
public string Name { get; set; }
43+
44+
protected AbstractDictionaryLocalization(Session session, CultureInfo culture, T target) : base(session, culture, target) { }
45+
}
46+
47+
public class Country : AbstractLocalizableDictionary<Country, CountryLocalization>, ILocalizable<CountryLocalization>
48+
{
49+
public Country(Session session) : base(session) { }
50+
}
51+
52+
[HierarchyRoot]
53+
public class CountryLocalization : AbstractDictionaryLocalization<Country, CountryLocalization>
54+
{
55+
public CountryLocalization(Session session, CultureInfo culture, Country target) : base(session, culture, target) { }
56+
}
57+
58+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (C) 2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System.Linq;
6+
using System.Threading;
7+
using NUnit.Framework;
8+
using Xtensive.Orm.Localization.Tests.Model;
9+
10+
namespace Xtensive.Orm.Localization.Tests
11+
{
12+
[TestFixture]
13+
public class ProjectionToCustomTypesTests : AutoBuildTest
14+
{
15+
protected override void PopulateDatabase()
16+
{
17+
using (var session = Domain.OpenSession())
18+
using (var ts = session.OpenTransaction()) {
19+
// populating database
20+
var m1 = new Country(session) {
21+
Identifier = "HUN",
22+
Name = "Magyarország"
23+
};
24+
var m2 = new Country(session) {
25+
Identifier = "RUS",
26+
Name = "Oroszország"
27+
};
28+
using (new LocalizationScope(EnglishCulture)) {
29+
m2.Name = "Russia";
30+
}
31+
using (new LocalizationScope(SpanishCulture)) {
32+
m2.Name = "Rusia";
33+
}
34+
ts.Complete();
35+
}
36+
}
37+
38+
[Test]
39+
public void EntityHierarchyWithAbstractPropertyTest()
40+
{
41+
Thread.CurrentThread.CurrentCulture = EnglishCulture;
42+
using (var session = Domain.OpenSession())
43+
using (var ts = session.OpenTransaction()) {
44+
var q = session.Query.All<Country>().OrderBy(e => e.Identifier).Select(e => new { e.Name });
45+
var l = q.ToList();
46+
// assertions
47+
Assert.AreEqual(2, l.Count);
48+
var propertyInfos = l.First().GetType().GetProperties();
49+
Assert.AreEqual(propertyInfos.Length, 1);
50+
Assert.AreEqual(propertyInfos.First().Name, nameof(Country.Name));
51+
Assert.AreEqual(l.First().Name, "Magyarország");
52+
Assert.AreEqual(l.Last().Name, "Russia");
53+
}
54+
}
55+
}
56+
}

Extensions/Xtensive.Orm.Localization/Internals/LocalizationExpressionVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected override Expression VisitMemberAccess(MemberExpression me)
2525
if (Map == null)
2626
return me;
2727

28-
var localizationInfo = Map.Get(me.Member.ReflectedType);
28+
var localizationInfo = Map.Get(me.Expression?.Type ?? me.Member.ReflectedType);
2929
if (localizationInfo == null || !localizationInfo.LocalizationTypeInfo.Fields.Contains(me.Member.Name))
3030
return base.VisitMemberAccess(me);
3131

0 commit comments

Comments
 (0)