Skip to content

Commit 3ca8122

Browse files
committed
model and test for 'Localization extension - complex entity class hierarchies and projection to custom entities'
1 parent 1215bf1 commit 3ca8122

File tree

2 files changed

+308
-0
lines changed

2 files changed

+308
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System;
2+
using System.Globalization;
3+
using Xtensive.Orm.Model;
4+
5+
namespace Xtensive.Orm.Localization.Tests.Model
6+
{
7+
[HierarchyRoot(InheritanceSchema = InheritanceSchema.ConcreteTable)]
8+
public abstract class IdentifiedEntity : Entity
9+
{
10+
[Key, Field]
11+
public int Id { get; set; }
12+
13+
[Version, Field(Nullable = false)]
14+
public int RecordVersion { get; set; }
15+
16+
public IdentifiedEntity(Session session) : base(session) { }
17+
}
18+
19+
public abstract class AbstractDictionary : IdentifiedEntity
20+
{
21+
public const string TableNamePrefix = "Dict_";
22+
23+
[Field(Nullable = false, Length = 64)]
24+
public string Identifier { get; set; }
25+
[Field(Nullable = false)]
26+
public bool Enabled { get; set; }
27+
[Field(Nullable = false)]
28+
public bool Selectable { get; set; }
29+
[Field(Nullable = false)]
30+
public bool Displayable { get; set; }
31+
public abstract string Name { get; set; }
32+
public abstract string Description { get; set; }
33+
34+
public AbstractDictionary(Session session) : base(session) { }
35+
}
36+
37+
public class AbstractNonLocalizableDictionary : AbstractDictionary
38+
{
39+
[Field(Nullable = false, Length = 512)]
40+
public override string Name { get; set; }
41+
[Field(Length = 2048, LazyLoad = true)]
42+
public override string Description { get; set; }
43+
44+
public AbstractNonLocalizableDictionary(Session session) : base(session) { }
45+
}
46+
47+
public abstract class AbstractLocalizableDictionary<T, TT> : AbstractDictionary, ILocalizable<TT>
48+
where T : AbstractLocalizableDictionary<T, TT>
49+
where TT : AbstractDictionaryLocalization<T, TT>
50+
{
51+
public override string Name { get => Localizations.Current.Name; set => Localizations.Current.Name = value; }
52+
public override string Description { get => Localizations.Current.Description; set => Localizations.Current.Description = value; }
53+
54+
[Field]
55+
public LocalizationSet<TT> Localizations { get; private set; }
56+
57+
public AbstractLocalizableDictionary(Session session) : base(session) { }
58+
}
59+
60+
public abstract class AbstractDictionaryLocalization<T, TT> : Localization<T>
61+
where TT : AbstractDictionaryLocalization<T, TT>
62+
where T : AbstractLocalizableDictionary<T, TT>
63+
{
64+
public const string TableNamePrefix = AbstractDictionary.TableNamePrefix;
65+
public const string TableNameSuffix = "_Localization";
66+
67+
[Field(Nullable = false, Length = 512)]
68+
public string Name { get; set; }
69+
[Field(Length = 2048, LazyLoad = true)]
70+
public string Description { get; set; }
71+
72+
[Version, Field(Nullable = false)]
73+
public int RecordVersion { get; set; }
74+
75+
protected AbstractDictionaryLocalization(Session session, CultureInfo culture, T target) : base(session, culture, target) { }
76+
}
77+
78+
[TableMapping(TableNamePrefix + nameof(CommunicationPlatform))]
79+
public class CommunicationPlatform : AbstractNonLocalizableDictionary
80+
{
81+
[Field(Length = 50)]
82+
public string ProtocolPrefix { get; set; }
83+
84+
public CommunicationPlatform(Session session) : base(session) { }
85+
}
86+
87+
[TableMapping(TableNamePrefix + nameof(BuiltinMessage))]
88+
public class BuiltinMessage : AbstractLocalizableDictionary<BuiltinMessage, BuiltinMessageLocalization>, ILocalizable<BuiltinMessageLocalization>
89+
{
90+
public BuiltinMessage(Session session) : base(session) { }
91+
}
92+
93+
[HierarchyRoot]
94+
[TableMapping(TableNamePrefix + nameof(BuiltinMessage) + TableNameSuffix)]
95+
public class BuiltinMessageLocalization : AbstractDictionaryLocalization<BuiltinMessage, BuiltinMessageLocalization>
96+
{
97+
public BuiltinMessageLocalization(Session session, CultureInfo culture, BuiltinMessage target) : base(session, culture, target) { }
98+
}
99+
100+
public class Country : IdentifiedEntity, ILocalizable<CountryLocalization>
101+
{
102+
[Field]
103+
public int OrderValue { get; set; }
104+
[Field]
105+
public bool Enabled { get; set; }
106+
107+
// Localizable field. Note that it is non-persistent
108+
public string Name
109+
{
110+
get => Localizations.Current.Name;
111+
set => Localizations.Current.Name = value;
112+
}
113+
114+
[Field]
115+
public LocalizationSet<CountryLocalization> Localizations { get; private set; }
116+
117+
public Country(Session session) : base(session) { }
118+
}
119+
120+
[HierarchyRoot]
121+
public class CountryLocalization : Localization<Country>
122+
{
123+
[Field(Length = 100)]
124+
public string Name { get; set; }
125+
public CountryLocalization(Session session, CultureInfo culture, Country target) : base(session, culture, target) { }
126+
}
127+
128+
[HierarchyRoot]
129+
public class Color : Entity, ILocalizable<ColorLocalization>
130+
{
131+
[Key, Field]
132+
public int Id { get; set; }
133+
134+
[Field]
135+
public int OrderValue { get; set; }
136+
[Field]
137+
public bool Enabled { get; set; }
138+
139+
// Localizable field. Note that it is non-persistent
140+
public string Name
141+
{
142+
get => Localizations.Current.Name;
143+
set => Localizations.Current.Name = value;
144+
}
145+
146+
[Field]
147+
public LocalizationSet<ColorLocalization> Localizations { get; private set; }
148+
149+
public Color(Session session) : base(session) { }
150+
}
151+
152+
[HierarchyRoot]
153+
public class ColorLocalization : Localization<Color>
154+
{
155+
[Field(Length = 100)]
156+
public string Name { get; set; }
157+
158+
public ColorLocalization(Session session, CultureInfo culture, Color target) : base(session, culture, target) { }
159+
}
160+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using System.Linq;
2+
using System.Threading;
3+
using NUnit.Framework;
4+
using Xtensive.Orm.Localization.Tests.Model;
5+
6+
namespace Xtensive.Orm.Localization.Tests
7+
{
8+
[TestFixture]
9+
public class ProjectionToCustomTypesTests : AutoBuildTest
10+
{
11+
protected override void PopulateDatabase()
12+
{
13+
using (var session = Domain.OpenSession()) {
14+
using (var ts = session.OpenTransaction()) {
15+
16+
// populating database
17+
18+
Country c1 = new Country(session) {
19+
Enabled = true,
20+
Name = "Magyarország",
21+
OrderValue = 3
22+
};
23+
Country c2 = new Country(session) {
24+
Enabled = true,
25+
Name = "Anglia",
26+
OrderValue = 6
27+
};
28+
Country c3 = new Country(session) {
29+
Enabled = false,
30+
Name = "Spain",
31+
OrderValue = 2
32+
};
33+
using (new LocalizationScope(EnglishCulture)) {
34+
c1.Name = "Hungary";
35+
c2.Name = "England";
36+
}
37+
38+
CommunicationPlatform cp1 = new CommunicationPlatform(session) {
39+
Identifier = "cp1",
40+
Name = "cp1",
41+
Description = "dcp1",
42+
ProtocolPrefix = "abc",
43+
Enabled = true
44+
};
45+
CommunicationPlatform cp2 = new CommunicationPlatform(session) {
46+
Identifier = "cp2",
47+
Name = "cp2",
48+
Description = "dcp2",
49+
ProtocolPrefix = "def"
50+
};
51+
52+
BuiltinMessage m1 = new BuiltinMessage(session) {
53+
Identifier = "bm1",
54+
Name = "bm1",
55+
Description = "dbm1",
56+
Enabled = true
57+
};
58+
BuiltinMessage m2 = new BuiltinMessage(session) {
59+
Identifier = "bm2",
60+
Name = "bm2",
61+
Description = "dbm2",
62+
Enabled = true
63+
};
64+
using (new LocalizationScope(EnglishCulture))
65+
m2.Name = "eng-bm2";
66+
using (new LocalizationScope(SpanishCulture))
67+
m2.Name = "de-bm2";
68+
69+
ts.Complete();
70+
}
71+
}
72+
}
73+
74+
[Test]
75+
public void NonLocalizableTest()
76+
{
77+
Thread.CurrentThread.CurrentCulture = EnglishCulture;
78+
using (var session = Domain.OpenSession()) {
79+
using (var ts = session.OpenTransaction()) {
80+
var q = session.Query.All<CommunicationPlatform>().OrderBy(e => e.Identifier).Select(e => new { e.Identifier, e.Name, e.Enabled, e.Description });
81+
var l = q.ToList();
82+
// assertions
83+
var propertyInfos = l.First().GetType().GetProperties();
84+
Assert.AreEqual(propertyInfos.Length, 4);
85+
Assert.AreEqual(propertyInfos[0].Name, nameof(CommunicationPlatform.Identifier));
86+
Assert.AreEqual(propertyInfos[1].Name, nameof(CommunicationPlatform.Name));
87+
Assert.AreEqual(propertyInfos[2].Name, nameof(CommunicationPlatform.Enabled));
88+
Assert.AreEqual(propertyInfos[3].Name, nameof(CommunicationPlatform.Description));
89+
90+
ts.Complete();
91+
}
92+
}
93+
}
94+
95+
[Test]
96+
public void SimpleClassHierarchyTest()
97+
{
98+
Thread.CurrentThread.CurrentCulture = EnglishCulture;
99+
using (var session = Domain.OpenSession()) {
100+
using (var ts = session.OpenTransaction()) {
101+
var q = session.Query.All<Country>().OrderBy(e => e.OrderValue).Select(e => new { e.Name, e.Enabled });
102+
var l = q.ToList();
103+
// assertions
104+
var propertyInfos = l.First().GetType().GetProperties();
105+
Assert.AreEqual(propertyInfos.Length, 2);
106+
Assert.AreEqual(propertyInfos.First().Name, nameof(Country.Name));
107+
Assert.AreEqual(propertyInfos.Last().Name, nameof(Country.Enabled));
108+
109+
ts.Complete();
110+
}
111+
}
112+
}
113+
114+
[Test]
115+
public void ComplexClassHierarchyTest()
116+
{
117+
Thread.CurrentThread.CurrentCulture = EnglishCulture;
118+
using (var session = Domain.OpenSession()) {
119+
using (var ts = session.OpenTransaction()) {
120+
var q = session.Query.All<BuiltinMessage>().OrderBy(e => e.Identifier).Select(e => new { e.Identifier, e.Name, e.Enabled, e.Description });
121+
var l = q.ToList();
122+
// assertions
123+
Assert.AreEqual(2, l.Count);
124+
125+
var propertyInfos = l.First().GetType().GetProperties();
126+
Assert.AreEqual(propertyInfos.Length, 4);
127+
Assert.AreEqual(propertyInfos[0].Name, nameof(BuiltinMessage.Identifier));
128+
Assert.AreEqual(propertyInfos[1].Name, nameof(BuiltinMessage.Name));
129+
Assert.AreEqual(propertyInfos[2].Name, nameof(BuiltinMessage.Enabled));
130+
Assert.AreEqual(propertyInfos[3].Name, nameof(BuiltinMessage.Description));
131+
132+
var f = l.First();
133+
Assert.AreEqual(f.Identifier, "bm1");
134+
Assert.AreEqual(f.Name, "bm1");
135+
Assert.AreEqual(f.Description, "dbm1");
136+
Assert.AreEqual(f.Enabled, true);
137+
138+
var s = l.Last();
139+
Assert.AreEqual(s.Identifier, "bm2");
140+
Assert.AreEqual(s.Name, "eng-bm2");
141+
Assert.AreEqual(s.Description, "dbm2");
142+
Assert.AreEqual(s.Enabled, true);
143+
}
144+
}
145+
}
146+
147+
}
148+
}

0 commit comments

Comments
 (0)