Skip to content

Commit e43b362

Browse files
committed
Simplification
1 parent 6f91a83 commit e43b362

File tree

2 files changed

+20
-218
lines changed

2 files changed

+20
-218
lines changed

Extensions/Xtensive.Orm.Localization.Tests/Model/DictionaryModel.cs

Lines changed: 6 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,25 @@
55
namespace Xtensive.Orm.Localization.Tests.Model
66
{
77
[HierarchyRoot(InheritanceSchema = InheritanceSchema.ConcreteTable)]
8-
public abstract class IdentifiedEntity : Entity
8+
public abstract class AbstractDictionary : Entity
99
{
1010
[Key, Field]
1111
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-
12+
2313
[Field(Nullable = false, Length = 64)]
2414
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; }
15+
16+
// abstract property
3117
public abstract string Name { get; set; }
32-
public abstract string Description { get; set; }
3318

3419
public AbstractDictionary(Session session) : base(session) { }
3520
}
3621

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-
4722
public abstract class AbstractLocalizableDictionary<T, TT> : AbstractDictionary, ILocalizable<TT>
4823
where T : AbstractLocalizableDictionary<T, TT>
4924
where TT : AbstractDictionaryLocalization<T, TT>
5025
{
5126
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; }
5327

5428
[Field]
5529
public LocalizationSet<TT> Localizations { get; private set; }
@@ -61,100 +35,21 @@ public abstract class AbstractDictionaryLocalization<T, TT> : Localization<T>
6135
where TT : AbstractDictionaryLocalization<T, TT>
6236
where T : AbstractLocalizableDictionary<T, TT>
6337
{
64-
public const string TableNamePrefix = AbstractDictionary.TableNamePrefix;
65-
public const string TableNameSuffix = "_Localization";
66-
6738
[Field(Nullable = false, Length = 512)]
6839
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; }
7440

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

78-
[TableMapping(TableNamePrefix + nameof(CommunicationPlatform))]
79-
public class CommunicationPlatform : AbstractNonLocalizableDictionary
44+
public class Country : AbstractLocalizableDictionary<Country, CountryLocalization>, ILocalizable<CountryLocalization>
8045
{
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-
11746
public Country(Session session) : base(session) { }
11847
}
11948

12049
[HierarchyRoot]
121-
public class CountryLocalization : Localization<Country>
50+
public class CountryLocalization : AbstractDictionaryLocalization<Country, CountryLocalization>
12251
{
123-
[Field(Length = 100)]
124-
public string Name { get; set; }
12552
public CountryLocalization(Session session, CultureInfo culture, Country target) : base(session, culture, target) { }
12653
}
12754

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-
}
16055
}

Extensions/Xtensive.Orm.Localization.Tests/ProjectionToCustomTypesTests.cs

Lines changed: 14 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -14,132 +14,39 @@ protected override void PopulateDatabase()
1414
using (var ts = session.OpenTransaction()) {
1515

1616
// 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
17+
Country m1 = new Country(session) {
18+
Identifier = "HUN",
19+
Name = "Magyarország"
3220
};
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
21+
Country m2 = new Country(session) {
22+
Identifier = "RUS",
23+
Name = "Oroszország"
6324
};
6425
using (new LocalizationScope(EnglishCulture))
65-
m2.Name = "eng-bm2";
26+
m2.Name = "Russia";
6627
using (new LocalizationScope(SpanishCulture))
67-
m2.Name = "de-bm2";
28+
m2.Name = "Rusia";
6829

6930
ts.Complete();
7031
}
7132
}
7233
}
7334

7435
[Test]
75-
public void NonLocalizableTest()
36+
public void EntityHierarchyWithAbstractPropertyTest()
7637
{
7738
Thread.CurrentThread.CurrentCulture = EnglishCulture;
7839
using (var session = Domain.OpenSession()) {
7940
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 });
41+
var q = session.Query.All<Country>().OrderBy(e => e.Identifier).Select(e => new { e.Name});
12142
var l = q.ToList();
12243
// assertions
12344
Assert.AreEqual(2, l.Count);
124-
12545
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);
46+
Assert.AreEqual(propertyInfos.Length, 1);
47+
Assert.AreEqual(propertyInfos.First().Name, nameof(Country.Name));
48+
Assert.AreEqual(l.First().Name, "Magyarország");
49+
Assert.AreEqual(l.Last().Name, "Russia");
14350
}
14451
}
14552
}

0 commit comments

Comments
 (0)