Skip to content

Localization extension - complex entity class hierarchies and projection to custom types #102

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

Merged
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
@@ -0,0 +1,58 @@
// Copyright (C) 2020 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.

using System.Globalization;
using Xtensive.Orm.Model;

namespace Xtensive.Orm.Localization.Tests.Model
{
[HierarchyRoot(InheritanceSchema = InheritanceSchema.ConcreteTable)]
public abstract class AbstractDictionary : Entity
{
[Key, Field]
public int Id { get; private set; }

[Field(Nullable = false, Length = 64)]
public string Identifier { get; set; }

// abstract property
public abstract string Name { get; set; }

protected AbstractDictionary(Session session) : base(session) { }
}

public abstract class AbstractLocalizableDictionary<T, TT> : AbstractDictionary, ILocalizable<TT>
where T : AbstractLocalizableDictionary<T, TT>
where TT : AbstractDictionaryLocalization<T, TT>
{
public override string Name { get => Localizations.Current.Name; set => Localizations.Current.Name = value; }

[Field]
public LocalizationSet<TT> Localizations { get; private set; }

protected AbstractLocalizableDictionary(Session session) : base(session) { }
}

public abstract class AbstractDictionaryLocalization<T, TT> : Localization<T>
where TT : AbstractDictionaryLocalization<T, TT>
where T : AbstractLocalizableDictionary<T, TT>
{
[Field(Nullable = false, Length = 512)]
public string Name { get; set; }

protected AbstractDictionaryLocalization(Session session, CultureInfo culture, T target) : base(session, culture, target) { }
}

public class Country : AbstractLocalizableDictionary<Country, CountryLocalization>, ILocalizable<CountryLocalization>
{
public Country(Session session) : base(session) { }
}

[HierarchyRoot]
public class CountryLocalization : AbstractDictionaryLocalization<Country, CountryLocalization>
{
public CountryLocalization(Session session, CultureInfo culture, Country target) : base(session, culture, target) { }
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (C) 2020 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.

using System.Linq;
using System.Threading;
using NUnit.Framework;
using Xtensive.Orm.Localization.Tests.Model;

namespace Xtensive.Orm.Localization.Tests
{
[TestFixture]
public class ProjectionToCustomTypesTests : AutoBuildTest
{
protected override void PopulateDatabase()
{
using (var session = Domain.OpenSession())
using (var ts = session.OpenTransaction()) {
// populating database
var m1 = new Country(session) {
Identifier = "HUN",
Name = "Magyarország"
};
var m2 = new Country(session) {
Identifier = "RUS",
Name = "Oroszország"
};
using (new LocalizationScope(EnglishCulture)) {
m2.Name = "Russia";
}
using (new LocalizationScope(SpanishCulture)) {
m2.Name = "Rusia";
}
ts.Complete();
}
}

[Test]
public void EntityHierarchyWithAbstractPropertyTest()
{
Thread.CurrentThread.CurrentCulture = EnglishCulture;
using (var session = Domain.OpenSession())
using (var ts = session.OpenTransaction()) {
var q = session.Query.All<Country>().OrderBy(e => e.Identifier).Select(e => new { e.Name });
var l = q.ToList();
// assertions
Assert.AreEqual(2, l.Count);
var propertyInfos = l.First().GetType().GetProperties();
Assert.AreEqual(propertyInfos.Length, 1);
Assert.AreEqual(propertyInfos.First().Name, nameof(Country.Name));
Assert.AreEqual(l.First().Name, "Magyarország");
Assert.AreEqual(l.Last().Name, "Russia");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected override Expression VisitMemberAccess(MemberExpression me)
if (Map == null)
return me;

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

Expand Down