Permalink
Browse files

Added functionality to throw an exception if a user store's a differe…

…nt entity with the same id as one already stored.
  • Loading branch information...
1 parent 0a5331a commit 2b05b760c199e09f9e3fb75f663f6b1544cff78f @dragan dragan committed Aug 1, 2010
@@ -5,6 +5,8 @@
using NUnit.Framework;
using SineSignal.Ottoman.Specs.Framework;
+using SineSignal.Ottoman.Exceptions;
+
namespace SineSignal.Ottoman.Specs
{
public class CouchDocumentSessionSpecs
@@ -119,6 +121,54 @@ public void Should_add_entity_to_identity_map()
Assert.That(entity, Is.EqualTo(entity1));
}
}
+
+ public class When_attempting_to_store_a_different_entity_with_the_same_id_as_another : ConcernFor<CouchDocumentSession>
+ {
+ private Guid id;
+ private Employee entity1;
+ private Employee entity2;
+ private Type entity2Type;
+ private PropertyInfo identityProperty;
+ private IDocumentConvention documentConvention;
+ private NonUniqueEntityException thrownException;
+
+ protected override void Given()
+ {
+ id = Guid.NewGuid();
+ entity1 = new Employee { Id = id, Name = "Bob", Login = "boblogin" };
+ entity2 = new Employee { Id = id, Name = "Carl", Login = "carllogin" };
+ entity2Type = entity2.GetType();
+ identityProperty = entity2Type.GetProperty("Id");
+ documentConvention = Fake<IDocumentConvention>();
+ documentConvention.GetIdentityPropertyFor(entity2Type).Returns(identityProperty);
+ }
+
+ public override CouchDocumentSession CreateSystemUnderTest()
+ {
+ var couchDocumentSession = new CouchDocumentSession(documentConvention);
+ couchDocumentSession.Store(entity1);
+ return couchDocumentSession;
+ }
+
+ protected override void When()
+ {
+ try
+ {
+ Sut.Store(entity2);
+ }
+ catch (NonUniqueEntityException ex)
+ {
+ thrownException = ex;
+ }
+ }
+
+ [Test]
+ public void Should_throw_non_unique_entity_exception()
+ {
+ Assert.That(thrownException, Is.Not.Null);
+ Assert.That(thrownException.Message, Is.EqualTo("Attempted to associate a different entity with id '" + id + "'."));
+ }
+ }
}
public class Employee
@@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Reflection;
+using SineSignal.Ottoman.Exceptions;
+
namespace SineSignal.Ottoman
{
public class CouchDocumentSession
@@ -34,6 +36,14 @@ public void Store(object entity)
if (id != null)
{
+ if (IdentityMap.ContainsKey(id.ToString()))
+ {
+ if (ReferenceEquals(IdentityMap[id.ToString()], entity))
+ return;
+
+ throw new NonUniqueEntityException("Attempted to associate a different entity with id '" + id + "'.");
+ }
+
IdentityMap[id.ToString()] = entity;
}
}
@@ -0,0 +1,19 @@
+using System;
+
+namespace SineSignal.Ottoman.Exceptions
+{
+ public class NonUniqueEntityException : Exception
+ {
+ public NonUniqueEntityException()
+ {
+ }
+
+ public NonUniqueEntityException(string message) : base(message)
+ {
+ }
+
+ public NonUniqueEntityException(string message, Exception innerException) : base(message, innerException)
+ {
+ }
+ }
+}
@@ -33,10 +33,14 @@
<Compile Include="AssemblyInfo.cs" />
<Compile Include="CouchDocumentSession.cs" />
<Compile Include="IDocumentConvention.cs" />
+ <Compile Include="Exceptions\NonUniqueEntityException.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <ItemGroup>
+ <Folder Include="Exceptions\" />
+ </ItemGroup>
</Project>

0 comments on commit 2b05b76

Please sign in to comment.