Permalink
Browse files

Added the ability to store an entity with an id already assigned.

  • Loading branch information...
1 parent 3a8c9c4 commit 0a5331a2da12bd3c214c6f19e4e164254ec446c5 @dragan committed Aug 1, 2010
@@ -65,11 +65,65 @@ public void Should_add_entity_to_identity_map()
Assert.That(entity, Is.EqualTo(entity1));
}
}
+
+ public class When_storing_an_entity_with_an_id_already_assigned : ConcernFor<CouchDocumentSession>
+ {
+ private Guid id;
+ private Employee entity1;
+ private Type entity1Type;
+ private PropertyInfo identityProperty;
+ private IDocumentConvention documentConvention;
+
+ protected override void Given()
+ {
+ id = Guid.NewGuid();
+ entity1 = new Employee { Id = id, Name = "Bob", Login = "boblogin" };
+ entity1Type = entity1.GetType();
+ identityProperty = entity1Type.GetProperty("Id");
+ documentConvention = Fake<IDocumentConvention>();
+ documentConvention.GetIdentityPropertyFor(entity1Type).Returns(identityProperty);
+ }
+
+ public override CouchDocumentSession CreateSystemUnderTest()
+ {
+ return new CouchDocumentSession(documentConvention);
+ }
+
+ protected override void When()
+ {
+ Sut.Store(entity1);
+ }
+
+ [Test]
+ public void Should_call_get_identity_property()
+ {
+ documentConvention.Received().GetIdentityPropertyFor(entity1Type);
+ }
+
+ [Test]
+ public void Should_not_call_generate_document_id()
+ {
+ documentConvention.DidNotReceive().GenerateIdentityFor(identityProperty.PropertyType);
+ }
+
+ [Test]
+ public void Should_not_modify_the_id()
+ {
+ Assert.That(id, Is.EqualTo(entity1.Id));
+ }
+
+ [Test]
+ public void Should_add_entity_to_identity_map()
+ {
+ Employee entity = Sut.Load<Employee>(id.ToString());
+ Assert.That(entity, Is.EqualTo(entity1));
+ }
+ }
}
public class Employee
{
- public Guid Id { get; private set; }
+ public Guid Id { get; set; }
public string Name { get; set; }
public string Login { get; set; }
}
@@ -17,16 +17,25 @@ public CouchDocumentSession(IDocumentConvention documentConvention)
public void Store(object entity)
{
- // What do we want the store method to do
- // 1. Using the default convention, find the id property
- // 2. Using the default convention, generate a new id
- // 3. Set our found id property to the generated id
- // 4. Add the entity to the identity map
Type entityType = entity.GetType();
PropertyInfo identityProperty = DocumentConvention.GetIdentityPropertyFor(entityType);
- var id = DocumentConvention.GenerateIdentityFor(identityProperty.PropertyType);
- identityProperty.SetValue(entity, id, null);
- IdentityMap[id.ToString()] = entity;
+
+ object id = null;
+ if (identityProperty != null)
+ {
+ id = GetIdentityValueFor(entity, identityProperty);
+
+ if (id == null)
+ {
+ id = DocumentConvention.GenerateIdentityFor(identityProperty.PropertyType);
+ identityProperty.SetValue(entity, id, null);
+ }
+ }
+
+ if (id != null)
+ {
+ IdentityMap[id.ToString()] = entity;
+ }
}
public T Load<T>(string id)
@@ -39,5 +48,19 @@ public T Load<T>(string id)
return default(T);
}
+
+ private static object GetIdentityValueFor(object entity, PropertyInfo identityProperty)
+ {
+ object id = identityProperty.GetValue(entity, null);
+
+ Type propertyType = identityProperty.PropertyType;
+ if (propertyType == typeof(Guid))
+ {
+ if ((Guid)id == Guid.Empty)
+ id = null;
+ }
+
+ return id;
+ }
}
}
@@ -6,6 +6,6 @@ namespace SineSignal.Ottoman
public interface IDocumentConvention
{
PropertyInfo GetIdentityPropertyFor(Type entityType);
- Guid GenerateIdentityFor(Type identityType);
+ object GenerateIdentityFor(Type identityType);
}
}

0 comments on commit 0a5331a

Please sign in to comment.