-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a CouchDocumentConvention class to encapsulate the default co…
…nventions. We also allow the user to override these conventions. We also did some refactoring and created a GuidIdentityGenerator.
- Loading branch information
Showing
11 changed files
with
188 additions
and
26 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/SineSignal.Ottoman.Specs/CouchDocumentConventionSpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
using NSubstitute; | ||
using NUnit.Framework; | ||
using NUnit.Framework.SyntaxHelpers; | ||
using SineSignal.Ottoman.Specs.Framework; | ||
|
||
using SineSignal.Ottoman; | ||
using SineSignal.Ottoman.Generators; | ||
|
||
namespace SineSignal.Ottoman.Specs | ||
{ | ||
public class CouchDocumentConventionSpecs | ||
{ | ||
public class When_retrieving_the_identity_property_info_for_a_given_type : ConcernFor<CouchDocumentConvention> | ||
{ | ||
private string defaultIdentityName; | ||
private Employee entity; | ||
private PropertyInfo identityProperty; | ||
|
||
protected override void Given() | ||
{ | ||
defaultIdentityName = "Id"; | ||
entity = new Employee { Id = Guid.NewGuid(), Name = "Bob", Login = "boblogin" }; | ||
} | ||
|
||
public override CouchDocumentConvention CreateSystemUnderTest () | ||
{ | ||
return new CouchDocumentConvention(); | ||
} | ||
|
||
protected override void When() | ||
{ | ||
identityProperty = Sut.GetIdentityPropertyFor(entity.GetType()); | ||
} | ||
|
||
[Test] | ||
public void Should_be_able_to_get_identity_property_info_given_the_name_of_the_property() | ||
{ | ||
Assert.That(identityProperty.Name, Is.EqualTo(defaultIdentityName)); | ||
} | ||
} | ||
|
||
public class When_generating_an_identity_value_for_a_given_identity_type : ConcernFor<CouchDocumentConvention> | ||
{ | ||
private Employee entity; | ||
private object generatedValue; | ||
|
||
protected override void Given() | ||
{ | ||
entity = new Employee { Name = "Bob", Login = "boblogin" }; | ||
} | ||
|
||
public override CouchDocumentConvention CreateSystemUnderTest () | ||
{ | ||
return new CouchDocumentConvention(); | ||
} | ||
|
||
protected override void When() | ||
{ | ||
generatedValue = Sut.GenerateIdentityFor(entity.Id.GetType()); | ||
} | ||
|
||
[Test] | ||
public void Should_return_a_Guid_type() | ||
{ | ||
Assert.That(generatedValue, Is.TypeOf(typeof(Guid))); | ||
} | ||
|
||
[Test] | ||
public void Should_not_return_an_empty_Guid() | ||
{ | ||
Assert.That((Guid)generatedValue, Is.Not.EqualTo(Guid.Empty)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
using SineSignal.Ottoman.Generators; | ||
|
||
namespace SineSignal.Ottoman | ||
{ | ||
public class CouchDocumentConvention : ICouchDocumentConvention | ||
{ | ||
private static IDictionary<Type, object> identityGenerators = InitializeIdentityGenerators(); | ||
|
||
public virtual PropertyInfo GetIdentityPropertyFor(Type entityType) | ||
{ | ||
return entityType.GetProperty("Id"); | ||
} | ||
|
||
public object GenerateIdentityFor(Type identityType) | ||
{ | ||
object generatedValue = null; | ||
|
||
if (identityType == typeof(Guid)) | ||
{ | ||
generatedValue = GetIdentityGeneratorFor<Guid>(identityType).Generate(); | ||
} | ||
|
||
return generatedValue; | ||
} | ||
|
||
private static IDictionary<Type, object> InitializeIdentityGenerators() | ||
{ | ||
IDictionary<Type, object> identityGenerators = new Dictionary<Type, object>(); | ||
|
||
identityGenerators.Add(typeof(Guid), new GuidIdentityGenerator()); | ||
|
||
return identityGenerators; | ||
} | ||
|
||
private static IIdentityGenerator<T> GetIdentityGeneratorFor<T>(Type type) | ||
{ | ||
object generator; | ||
|
||
if (identityGenerators.TryGetValue(type, out generator)) | ||
{ | ||
return generator as IIdentityGenerator<T>; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/SineSignal.Ottoman/Generators/GuidIdentityGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace SineSignal.Ottoman.Generators | ||
{ | ||
public class GuidIdentityGenerator : IIdentityGenerator<Guid> | ||
{ | ||
public Guid Generate() | ||
{ | ||
return Guid.NewGuid(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace SineSignal.Ottoman.Generators | ||
{ | ||
public interface IIdentityGenerator<T> | ||
{ | ||
T Generate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters