Skip to content

Commit

Permalink
Added configuration helpers
Browse files Browse the repository at this point in the history
static methods to help get the EngineConfiguration correct depending on
the usage.
  • Loading branch information
rofr committed May 2, 2014
1 parent 8273415 commit f675c64
Show file tree
Hide file tree
Showing 18 changed files with 375 additions and 805 deletions.
5 changes: 1 addition & 4 deletions OrigoDB.Modules.Protobuf.Test/Domain/Company.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using ProtoBuf;

namespace Modules.ProtoBuf.Test.Domain
Expand Down
7 changes: 1 addition & 6 deletions OrigoDB.Modules.Protobuf.Test/Domain/Employee.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProtoBuf;
using OrigoDB.Modules.ProtoBuf;
using ProtoBuf;

namespace Modules.ProtoBuf.Test.Domain
{
Expand Down
107 changes: 107 additions & 0 deletions OrigoDB.Modules.Protobuf.Test/Domain/TodoModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using OrigoDB.Core;
using ProtoBuf;

namespace Modules.ProtoBuf.Test.Domain
{

[ProtoContract(ImplicitFields=ImplicitFields.AllFields, AsReferenceDefault = true)]
public class SpecialTodoItem : TodoItem
{
public string SpecialValue { get; set; }
}

[ProtoContract(ImplicitFields = ImplicitFields.AllFields, AsReferenceDefault = true)]
[ProtoInclude(100, typeof(SpecialTodoItem))]
public class TodoItem
{
public readonly Guid Id;
public string Title { get; set; }
public DateTime? Due;
public DateTime? Completed;

public TodoItem()
{
Id = Guid.NewGuid();
Title = "No name";
}
}

[ProtoContract(ImplicitFields = ImplicitFields.AllFields, AsReferenceDefault = true, SkipConstructor = true)]
public class Category
{
public string Name { get; set; }
public List<TodoItem> Items { get; set; }

public Category(string name)
{
Name = name;
Items = new List<TodoItem>();
}
}

[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
public class TodoModel : Model
{
public Dictionary<Guid, TodoItem> Items
{
get;
private set;
}

public Dictionary<string, Category> Categories
{
get;
private set;
}

public TodoModel()
{
Items = new Dictionary<Guid, TodoItem>();
Categories = new Dictionary<string, Category>(StringComparer.InvariantCultureIgnoreCase);
}

public Guid AddItem(string title)
{
var item = new TodoItem{Title = title};
Items.Add(item.Id, item);
return item.Id;
}

public Guid AddSpecialItem(string title)
{
var item = new SpecialTodoItem { Title = title };
Items.Add(item.Id, item);
return item.Id;
}

public void SetCategories(Guid itemId, params string[] categoryNames)
{
TodoItem item;
if (Items.TryGetValue(itemId, out item))
{
foreach (var categoryName in categoryNames)
{
if (!Categories.ContainsKey(categoryName))
{
Categories.Add(categoryName, new Category(categoryName));
}
Categories[categoryName].Items.Add(item);
}
}
}

[ProtoAfterDeserialization]
private void FixRefsAfterDeserialization()
{
foreach (var category in Categories.Values)
{
for (int i = 0; i < category.Items.Count; i++)
{
category.Items[i] = Items[category.Items[i].Id];
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static class SerializationHelper
internal static Stream Serialize<T>(T instance, ProtoBufFormatter formatter = null)
{
formatter = formatter ?? new ProtoBufFormatter();
MemoryStream stream = new MemoryStream();
var stream = new MemoryStream();
formatter.Serialize(stream, instance);
stream.Position = 0;
return stream;
Expand All @@ -19,5 +19,10 @@ internal static T Deserialize<T>(Stream stream, ProtoBufFormatter formatter = nu
formatter = formatter ?? new ProtoBufFormatter();
return (T)formatter.Deserialize(stream);
}

internal static T Clone<T>(T item, ProtoBufFormatter formatter = null)
{
return Deserialize<T>(Serialize(item, formatter), formatter);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="OrigoDB.Core, Version=0.12.0.0, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="OrigoDB.Core, Version=0.13.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\OrigoDB.Core.0.12.0\lib\net40\OrigoDB.Core.dll</HintPath>
<HintPath>..\packages\OrigoDB.Core.0.13.0\lib\net40\OrigoDB.Core.dll</HintPath>
</Reference>
<Reference Include="protobuf-net, Version=2.0.0.668, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand All @@ -50,20 +50,21 @@
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
<Visible>False</Visible>
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Compile Include="Domain\TodoModel.cs" />
<Compile Include="Framework\SerializationHelper.cs" />
<Compile Include="Domain\Employee.cs" />
<Compile Include="Domain\Company.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ProtoBufFormatterTests.cs" />
<Compile Include="ProtoBufStreamHeaderTests.cs" />
<Compile Include="SmokeTests.cs" />
<Compile Include="TodoModelTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
34 changes: 5 additions & 29 deletions OrigoDB.Modules.Protobuf.Test/ProtoBufFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using OrigoDB.Modules.Protobuf;
using OrigoDB.Modules.ProtoBuf;
using System.Runtime.Serialization;
using Modules.ProtoBuf.Test.Framework;
Expand Down Expand Up @@ -35,10 +34,9 @@ public void CanDeserializeType()
var graph = new Employee(16) { Name = "Kalle", Age = 42 };

// Act
var stream = SerializationHelper.Serialize<Employee>(graph, formatter);
var result = SerializationHelper.Deserialize<Employee>(stream, formatter);
var result = SerializationHelper.Clone<Employee>(graph, formatter);

// Act
// Assert
Assert.IsInstanceOf<Employee>(result);
Assert.AreEqual("Kalle", result.Name);
Assert.AreEqual(42, result.Age);
Expand All @@ -49,9 +47,7 @@ public void CanDeserializeType()
public void CanDeserializeComplexType()
{
// Arrange
var modelBuilder = new RuntimeTypeModelBuilder();
modelBuilder.Add(typeof(Employee));
var formatter = new ProtoBufFormatter();
var formatter = new ProtoBufFormatter<Company>();
var graph = new Company() {
Name = "Initech Corporation",
Employees = new List<Employee> {
Expand All @@ -60,35 +56,15 @@ public void CanDeserializeComplexType()
}
};

//hack. triggers E#mployee type to be added to the TypeModel
//SerializationHelper.Serialize(new Employee(), formatter);

// Act
var stream = SerializationHelper.Serialize<Company>(graph, formatter);
var result = SerializationHelper.Deserialize<Company>(stream, formatter);
var result = SerializationHelper.Clone<Company>(graph, formatter);

// Act
// Assert
Assert.AreEqual("Initech Corporation", result.Name);
Assert.IsNotNull(result.Employees);
Assert.AreEqual(2, result.Employees.Count);
Assert.AreEqual("Peter Gibbons", result.Employees.ElementAt(0).Name);
Assert.AreEqual("Michael Bolton", result.Employees.ElementAt(1).Name);
}

[Test]
public void NonSerializedTypeIsNotConsideredKnown()
{
var formatter = new ProtoBufFormatter();
Assert.IsFalse(formatter.IsKnownType(typeof(Employee)));
}

[Test]
public void SerializedTypeIsConsideredKnown()
{
var formatter = new ProtoBufFormatter();
var graph = new Employee();
var stream = SerializationHelper.Serialize<Employee>(graph, formatter);
Assert.IsTrue(formatter.IsKnownType(typeof(Employee)));
}
}
}
44 changes: 0 additions & 44 deletions OrigoDB.Modules.Protobuf.Test/ProtoBufStreamHeaderTests.cs

This file was deleted.

Loading

0 comments on commit f675c64

Please sign in to comment.