Skip to content

Instantly share code, notes, and snippets.

@pawelpabich
Last active Aug 29, 2015
Embed
What would you like to do?
[TestMethod]
public void T()
{
var entries = new List<DataStreamEntry>();
var stopwatch1 = Stopwatch.StartNew();
for (var i = 0; i < 1500; i++)
{
var transfer = new AllocationTransferCreated(i, "asdfasdf", new AllocationIdentifier(1, 1, 2014), DateRange.EndOfMonth(DateTimeOffset.Now),
new AllocationIdentifier(1, 2, 2014), DateRange.EndOfMonth(DateTimeOffset.Now), 12, 12,
12, 12, 12, DateTimeOffset.Now, DateTimeOffset.Now);
var nested = new Nested() { AllocationTransferCreated = transfer, List = Create10Items() };
entries.Add(new DataStreamEntry(DataStreamType.AllocationTransfer, "12", "name", nested, 12));
}
stopwatch1.Stop();
var stopwatch2 = Stopwatch.StartNew();
var results = new List<Nested>();
for (int i = 0; i < 1500; i++)
{
var result = entries[i].DataAs<Nested>();
results.Add(result);
}
stopwatch2.Stop();
foreach (var result in results)
{
result.AllocationTransferCreated.ShouldNotBe(null);
result.List.Count.ShouldBe(10);
}
}
private IList<ComplexType> Create10Items()
{
var list = new List<ComplexType>();
for (int i = 0; i < 10; i++)
{
list.Add(new ComplexType() { Value1 = i.ToString(), Value2 = i });
}
return list;
}
public class Nested
{
public AllocationTransferCreated AllocationTransferCreated { get; set; }
public IList<ComplexType> List { get; set; }
}
public class ComplexType
{
public String Value1 { get; set; }
public int Value2 { get; set; }
}
public class DataStreamEntry : EntityFrameworkValueObject
{
private DataStreamEntry()
{
}
public DataStreamEntry(DataStreamType streamType, string streamId, string name, object body, int userId)
{
Guard.ThrowIfNull(name);
Guard.AssertIsNoLongerThan(name, 128, "name");
Data = JsonConvert.SerializeObject(body, CreateSettings());
StreamType = streamType;
StreamId = streamId;
Name = name;
Created = Clock.Now();
UserId = userId;
}
public string Name { get; private set; }
public int UserId { get; private set; }
public DateTimeOffset Created { get; private set; }
public string Data { get; private set; }
public string StreamId { get; private set; }
public DataStreamType StreamType { get; private set; }
public T DataAs<T>(params JsonConverter[] converters)
{
return JsonConvert.DeserializeObject<T>(Data, CreateSettings());
}
public object DataAs(Type type, params JsonConverter[] converters)
{
return JsonConvert.DeserializeObject(Data, type, CreateSettings());
}
public IDictionary<string, object> DataAsDictionary()
{
return JsonConvert.DeserializeObject<IDictionary<string, object>>(Data, new JsonConverter[] { new DictionaryConverter() });
}
JsonSerializerSettings CreateSettings()
{
var settings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.None,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
};
return settings;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment