Skip to content

Commit f675c64

Browse files
committed
Added configuration helpers
static methods to help get the EngineConfiguration correct depending on the usage.
1 parent 8273415 commit f675c64

18 files changed

+375
-805
lines changed

OrigoDB.Modules.Protobuf.Test/Domain/Company.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
1+
using System.Collections.Generic;
52
using ProtoBuf;
63

74
namespace Modules.ProtoBuf.Test.Domain

OrigoDB.Modules.Protobuf.Test/Domain/Employee.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using ProtoBuf;
6-
using OrigoDB.Modules.ProtoBuf;
1+
using ProtoBuf;
72

83
namespace Modules.ProtoBuf.Test.Domain
94
{
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using OrigoDB.Core;
4+
using ProtoBuf;
5+
6+
namespace Modules.ProtoBuf.Test.Domain
7+
{
8+
9+
[ProtoContract(ImplicitFields=ImplicitFields.AllFields, AsReferenceDefault = true)]
10+
public class SpecialTodoItem : TodoItem
11+
{
12+
public string SpecialValue { get; set; }
13+
}
14+
15+
[ProtoContract(ImplicitFields = ImplicitFields.AllFields, AsReferenceDefault = true)]
16+
[ProtoInclude(100, typeof(SpecialTodoItem))]
17+
public class TodoItem
18+
{
19+
public readonly Guid Id;
20+
public string Title { get; set; }
21+
public DateTime? Due;
22+
public DateTime? Completed;
23+
24+
public TodoItem()
25+
{
26+
Id = Guid.NewGuid();
27+
Title = "No name";
28+
}
29+
}
30+
31+
[ProtoContract(ImplicitFields = ImplicitFields.AllFields, AsReferenceDefault = true, SkipConstructor = true)]
32+
public class Category
33+
{
34+
public string Name { get; set; }
35+
public List<TodoItem> Items { get; set; }
36+
37+
public Category(string name)
38+
{
39+
Name = name;
40+
Items = new List<TodoItem>();
41+
}
42+
}
43+
44+
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
45+
public class TodoModel : Model
46+
{
47+
public Dictionary<Guid, TodoItem> Items
48+
{
49+
get;
50+
private set;
51+
}
52+
53+
public Dictionary<string, Category> Categories
54+
{
55+
get;
56+
private set;
57+
}
58+
59+
public TodoModel()
60+
{
61+
Items = new Dictionary<Guid, TodoItem>();
62+
Categories = new Dictionary<string, Category>(StringComparer.InvariantCultureIgnoreCase);
63+
}
64+
65+
public Guid AddItem(string title)
66+
{
67+
var item = new TodoItem{Title = title};
68+
Items.Add(item.Id, item);
69+
return item.Id;
70+
}
71+
72+
public Guid AddSpecialItem(string title)
73+
{
74+
var item = new SpecialTodoItem { Title = title };
75+
Items.Add(item.Id, item);
76+
return item.Id;
77+
}
78+
79+
public void SetCategories(Guid itemId, params string[] categoryNames)
80+
{
81+
TodoItem item;
82+
if (Items.TryGetValue(itemId, out item))
83+
{
84+
foreach (var categoryName in categoryNames)
85+
{
86+
if (!Categories.ContainsKey(categoryName))
87+
{
88+
Categories.Add(categoryName, new Category(categoryName));
89+
}
90+
Categories[categoryName].Items.Add(item);
91+
}
92+
}
93+
}
94+
95+
[ProtoAfterDeserialization]
96+
private void FixRefsAfterDeserialization()
97+
{
98+
foreach (var category in Categories.Values)
99+
{
100+
for (int i = 0; i < category.Items.Count; i++)
101+
{
102+
category.Items[i] = Items[category.Items[i].Id];
103+
}
104+
}
105+
}
106+
}
107+
}

OrigoDB.Modules.Protobuf.Test/Framework/SerializationHelper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal static class SerializationHelper
88
internal static Stream Serialize<T>(T instance, ProtoBufFormatter formatter = null)
99
{
1010
formatter = formatter ?? new ProtoBufFormatter();
11-
MemoryStream stream = new MemoryStream();
11+
var stream = new MemoryStream();
1212
formatter.Serialize(stream, instance);
1313
stream.Position = 0;
1414
return stream;
@@ -19,5 +19,10 @@ internal static T Deserialize<T>(Stream stream, ProtoBufFormatter formatter = nu
1919
formatter = formatter ?? new ProtoBufFormatter();
2020
return (T)formatter.Deserialize(stream);
2121
}
22+
23+
internal static T Clone<T>(T item, ProtoBufFormatter formatter = null)
24+
{
25+
return Deserialize<T>(Serialize(item, formatter), formatter);
26+
}
2227
}
2328
}

OrigoDB.Modules.Protobuf.Test/OrigoDB.Modules.ProtoBuf.Test.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
<Reference Include="nunit.framework">
3939
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
4040
</Reference>
41-
<Reference Include="OrigoDB.Core, Version=0.12.0.0, Culture=neutral, processorArchitecture=MSIL">
41+
<Reference Include="OrigoDB.Core, Version=0.13.0.0, Culture=neutral, processorArchitecture=MSIL">
4242
<SpecificVersion>False</SpecificVersion>
43-
<HintPath>..\packages\OrigoDB.Core.0.12.0\lib\net40\OrigoDB.Core.dll</HintPath>
43+
<HintPath>..\packages\OrigoDB.Core.0.13.0\lib\net40\OrigoDB.Core.dll</HintPath>
4444
</Reference>
4545
<Reference Include="protobuf-net, Version=2.0.0.668, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
4646
<SpecificVersion>False</SpecificVersion>
@@ -50,20 +50,21 @@
5050
<Reference Include="System.Core">
5151
<RequiredTargetFramework>3.5</RequiredTargetFramework>
5252
</Reference>
53+
<Reference Include="System.XML" />
5354
</ItemGroup>
5455
<ItemGroup>
5556
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
5657
<Visible>False</Visible>
5758
</CodeAnalysisDependentAssemblyPaths>
5859
</ItemGroup>
5960
<ItemGroup>
61+
<Compile Include="Domain\TodoModel.cs" />
6062
<Compile Include="Framework\SerializationHelper.cs" />
6163
<Compile Include="Domain\Employee.cs" />
6264
<Compile Include="Domain\Company.cs" />
6365
<Compile Include="Properties\AssemblyInfo.cs" />
6466
<Compile Include="ProtoBufFormatterTests.cs" />
65-
<Compile Include="ProtoBufStreamHeaderTests.cs" />
66-
<Compile Include="SmokeTests.cs" />
67+
<Compile Include="TodoModelTests.cs" />
6768
</ItemGroup>
6869
<ItemGroup>
6970
<None Include="packages.config" />

OrigoDB.Modules.Protobuf.Test/ProtoBufFormatterTests.cs

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using NUnit.Framework;
4-
using OrigoDB.Modules.Protobuf;
54
using OrigoDB.Modules.ProtoBuf;
65
using System.Runtime.Serialization;
76
using Modules.ProtoBuf.Test.Framework;
@@ -35,10 +34,9 @@ public void CanDeserializeType()
3534
var graph = new Employee(16) { Name = "Kalle", Age = 42 };
3635

3736
// Act
38-
var stream = SerializationHelper.Serialize<Employee>(graph, formatter);
39-
var result = SerializationHelper.Deserialize<Employee>(stream, formatter);
37+
var result = SerializationHelper.Clone<Employee>(graph, formatter);
4038

41-
// Act
39+
// Assert
4240
Assert.IsInstanceOf<Employee>(result);
4341
Assert.AreEqual("Kalle", result.Name);
4442
Assert.AreEqual(42, result.Age);
@@ -49,9 +47,7 @@ public void CanDeserializeType()
4947
public void CanDeserializeComplexType()
5048
{
5149
// Arrange
52-
var modelBuilder = new RuntimeTypeModelBuilder();
53-
modelBuilder.Add(typeof(Employee));
54-
var formatter = new ProtoBufFormatter();
50+
var formatter = new ProtoBufFormatter<Company>();
5551
var graph = new Company() {
5652
Name = "Initech Corporation",
5753
Employees = new List<Employee> {
@@ -60,35 +56,15 @@ public void CanDeserializeComplexType()
6056
}
6157
};
6258

63-
//hack. triggers E#mployee type to be added to the TypeModel
64-
//SerializationHelper.Serialize(new Employee(), formatter);
65-
6659
// Act
67-
var stream = SerializationHelper.Serialize<Company>(graph, formatter);
68-
var result = SerializationHelper.Deserialize<Company>(stream, formatter);
60+
var result = SerializationHelper.Clone<Company>(graph, formatter);
6961

70-
// Act
62+
// Assert
7163
Assert.AreEqual("Initech Corporation", result.Name);
7264
Assert.IsNotNull(result.Employees);
7365
Assert.AreEqual(2, result.Employees.Count);
7466
Assert.AreEqual("Peter Gibbons", result.Employees.ElementAt(0).Name);
7567
Assert.AreEqual("Michael Bolton", result.Employees.ElementAt(1).Name);
7668
}
77-
78-
[Test]
79-
public void NonSerializedTypeIsNotConsideredKnown()
80-
{
81-
var formatter = new ProtoBufFormatter();
82-
Assert.IsFalse(formatter.IsKnownType(typeof(Employee)));
83-
}
84-
85-
[Test]
86-
public void SerializedTypeIsConsideredKnown()
87-
{
88-
var formatter = new ProtoBufFormatter();
89-
var graph = new Employee();
90-
var stream = SerializationHelper.Serialize<Employee>(graph, formatter);
91-
Assert.IsTrue(formatter.IsKnownType(typeof(Employee)));
92-
}
9369
}
9470
}

OrigoDB.Modules.Protobuf.Test/ProtoBufStreamHeaderTests.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)