Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored the Store method some more. Made things a little easier fo…

…r when we implement Load.
  • Loading branch information...
commit 6fe98250598d567e31c24a1d7a83bea9543de4d9 1 parent ad91925
@dragan dragan authored
View
49 src/SineSignal.Ottoman.Specs/CouchDocumentSpecs.cs
@@ -9,11 +9,12 @@ namespace SineSignal.Ottoman.Specs
{
public class CouchDocumentSpecs
{
- public class When_instantiating_a_new_couch_document : ConcernFor<CouchDocument>
+ public class When_dehydrating_an_entity_into_a_couch_document_with_no_revision : StaticConcern
{
private Employee entity1;
private Type entity1Type;
private PropertyInfo identityProperty;
+ private CouchDocument couchDocument;
protected override void Given()
{
@@ -22,18 +23,50 @@ protected override void Given()
identityProperty = entity1Type.GetProperty("Id");
}
- public override CouchDocument CreateSystemUnderTest()
+ protected override void When()
{
- return new CouchDocument(entity1, identityProperty);
+ couchDocument = CouchDocument.Dehydrate(entity1, identityProperty, String.Empty);
}
[Test]
- public void Should_copy_passed_in_entity()
+ public void Should_create_couch_document_without_a_revision()
{
- Assert.That(Sut["_id"], Is.EqualTo(entity1.Id));
- Assert.That(Sut["Type"], Is.EqualTo(entity1Type.Name));
- Assert.That(Sut["Name"], Is.EqualTo(entity1.Name));
- Assert.That(Sut["Login"], Is.EqualTo(entity1.Login));
+ Assert.That(couchDocument["_id"], Is.EqualTo(entity1.Id));
+ Assert.That(couchDocument["Type"], Is.EqualTo(entity1Type.Name));
+ Assert.That(couchDocument["Name"], Is.EqualTo(entity1.Name));
+ Assert.That(couchDocument["Login"], Is.EqualTo(entity1.Login));
+ }
+ }
+
+ public class When_dehydrating_an_entity_into_a_couch_document_with_a_revision : StaticConcern
+ {
+ private Employee entity1;
+ private Type entity1Type;
+ private PropertyInfo identityProperty;
+ private string revision;
+ private CouchDocument couchDocument;
+
+ protected override void Given()
+ {
+ entity1 = new Employee { Id = Guid.NewGuid(), Name = "Bob", Login = "boblogin" };
+ entity1Type = entity1.GetType();
+ identityProperty = entity1Type.GetProperty("Id");
+ revision = "abc123";
+ }
+
+ protected override void When()
+ {
+ couchDocument = CouchDocument.Dehydrate(entity1, identityProperty, revision);
+ }
+
+ [Test]
+ public void Should_create_couch_document_with_revision()
+ {
+ Assert.That(couchDocument["_id"], Is.EqualTo(entity1.Id));
+ Assert.That(couchDocument["_rev"], Is.EqualTo(revision));
+ Assert.That(couchDocument["Type"], Is.EqualTo(entity1Type.Name));
+ Assert.That(couchDocument["Name"], Is.EqualTo(entity1.Name));
+ Assert.That(couchDocument["Login"], Is.EqualTo(entity1.Login));
}
}
}
View
3  src/SineSignal.Ottoman.Specs/Framework/StaticConcern.cs
@@ -1,5 +1,8 @@
+using NUnit.Framework;
+
namespace SineSignal.Ottoman.Specs.Framework
{
+ [TestFixture]
public abstract class StaticConcern : BaseConcern
{
}
View
2  src/SineSignal.Ottoman/AssemblyInfo.cs
@@ -10,5 +10,7 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
+[assembly: InternalsVisibleTo("SineSignal.Ottoman.Specs")]
+
[assembly: AssemblyVersion("0.0.0.0")]
[assembly: AssemblyFileVersion("0.0.0.0")]
View
6 src/SineSignal.Ottoman/Commands/BulkDocsCommand.cs
@@ -9,7 +9,7 @@
namespace SineSignal.Ottoman.Commands
{
- public class BulkDocsCommand : ICouchCommand
+ internal class BulkDocsCommand : ICouchCommand
{
private readonly BulkDocsMessage _message;
@@ -38,7 +38,7 @@ public void HandleError(string serverAddress, CommandErrorResult errorResult, Un
}
}
- public class BulkDocsMessage
+ internal class BulkDocsMessage
{
[JsonMember("non_atomic")]
public bool NonAtomic { get; private set; }
@@ -61,7 +61,7 @@ public BulkDocsMessage(bool nonAtomic, bool allOrNothing, IEnumerable<CouchDocum
}
}
- public class BulkDocsResult
+ internal class BulkDocsResult
{
[JsonMember("id")]
public string Id { get; set; }
View
32 src/SineSignal.Ottoman/CouchDocument.cs
@@ -5,29 +5,29 @@
namespace SineSignal.Ottoman
{
- public sealed class CouchDocument : Dictionary<string, object>, IDictionary<string, object>
+ internal class CouchDocument : Dictionary<string, object>, IDictionary<string, object>
{
- private Dictionary<string, object> Members { get; set; }
- private PropertyInfo IdentityProperty { get; set; }
-
- public CouchDocument(object entity, PropertyInfo identityProperty)
+ public static CouchDocument Dehydrate(object entity, PropertyInfo identityProperty, string revision)
{
- IdentityProperty = identityProperty;
- CopyPropertiesFrom(entity);
- this["Type"] = entity.GetType().Name;
- }
-
- private void CopyPropertiesFrom(object source)
- {
- foreach (PropertyInfo property in source.GetType().GetProperties().Where(p => p.CanRead))
+ var couchDocument = new CouchDocument();
+ var entityType = entity.GetType();
+
+ couchDocument["Type"] = entityType.Name;
+
+ if (revision != String.Empty)
+ couchDocument["_rev"] = revision;
+
+ foreach (PropertyInfo property in entityType.GetProperties().Where(p => p.CanRead))
{
var key = property.Name;
- if (key == IdentityProperty.Name)
+ if (key == identityProperty.Name)
key = "_id";
- var value = property.GetValue(source, null);
- this[key] = value;
+ var propertyValue = property.GetValue(entity, null);
+ couchDocument[key] = propertyValue;
}
+
+ return couchDocument;
}
}
}
View
40 src/SineSignal.Ottoman/CouchDocumentSession.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Reflection;
using SineSignal.Ottoman.Commands;
@@ -13,13 +14,13 @@ public class CouchDocumentSession : ICouchDocumentSession
public ICouchDatabase CouchDatabase { get; private set; }
private Dictionary<string, object> IdentityMap { get; set; }
- private Dictionary<string, DocumentMetadata> MetaDataMap { get; set; }
+ private Dictionary<object, EntityMetadata> EntityMetadataMap { get; set; }
public CouchDocumentSession(ICouchDatabase couchDatabase)
{
CouchDatabase = couchDatabase;
IdentityMap = new Dictionary<string, object>();
- MetaDataMap = new Dictionary<string, DocumentMetadata>();
+ EntityMetadataMap = new Dictionary<object, EntityMetadata>();
}
public void Store(object entity)
@@ -49,6 +50,8 @@ public void Store(object entity)
throw new NonUniqueEntityException("Attempted to associate a different entity with id '" + id + "'.");
}
+ var entityMetadata = new EntityMetadata{ Key = id.ToString(), Revision = String.Empty, OriginalEntity = entity };
+ EntityMetadataMap.Add(entity, entityMetadata);
IdentityMap[id.ToString()] = entity;
}
}
@@ -67,21 +70,32 @@ public T Load<T>(string id)
public void SaveChanges()
{
var docs = new List<CouchDocument>();
- foreach (object entity in IdentityMap.Values)
+ var entities = new List<object>();
+ foreach (var entity in EntityMetadataMap.Where(pair => IsEntityDirty(pair.Key, pair.Value)))
{
- PropertyInfo identityProperty = CouchDatabase.CouchDocumentConvention.GetIdentityPropertyFor(entity.GetType());
- var couchDocument = new CouchDocument(entity, identityProperty);
- docs.Add(couchDocument);
+ Type entityType = entity.Key.GetType();
+ PropertyInfo identityProperty = CouchDatabase.CouchDocumentConvention.GetIdentityPropertyFor(entityType);
+ docs.Add(CouchDocument.Dehydrate(entity.Key, identityProperty, entity.Value.Revision));
+ entities.Add(entity.Key);
}
var bulkDocsMessage = new BulkDocsMessage(docs);
var bulkDocsCommand = new BulkDocsCommand(CouchDatabase.Name, bulkDocsMessage);
+
BulkDocsResult[] results = CouchDatabase.CouchProxy.Execute<BulkDocsResult[]>(bulkDocsCommand);
for (int index = 0; index < results.Length; index++)
{
BulkDocsResult result = results[index];
- MetaDataMap[result.Id] = new DocumentMetadata { Id = result.Id, Rev = result.Rev };
+ object entity = entities[index];
+
+ EntityMetadata entityMetadata;
+ if (EntityMetadataMap.TryGetValue(entity, out entityMetadata) == false)
+ continue;
+
+ IdentityMap[result.Id] = entity;
+ entityMetadata.Revision = result.Rev;
+ entityMetadata.OriginalEntity = entity;
}
}
@@ -98,10 +112,16 @@ private static object GetIdentityValueFor(object entity, PropertyInfo identityPr
return id;
}
- public class DocumentMetadata
+ private bool IsEntityDirty(object entity, EntityMetadata entityMetadata)
+ {
+ return true;
+ }
+
+ private class EntityMetadata
{
- public string Id { get; set; }
- public string Rev { get; set; }
+ public string Key { get; set; }
+ public string Revision { get; set; }
+ public object OriginalEntity { get; set; }
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.