Commit f04c660
Changed files (6)
trunk
product
MyMoney
Infrastructure
caching
transactions2
trunk/product/MyMoney/Infrastructure/caching/IdentityMapSpecs.cs
@@ -20,8 +20,8 @@ namespace MoMoney.Infrastructure.caching
because b = () =>
{
- sut.Add(1, "1");
- result = sut.ItemThatBelongsTo(1);
+ sut.add(1, "1");
+ result = sut.item_that_belongs_to(1);
};
static string result;
@@ -31,7 +31,7 @@ namespace MoMoney.Infrastructure.caching
{
it should_return_the_default_value_for_that_type = () => result.should_be_equal_to(null);
- because b = () => { result = sut.ItemThatBelongsTo(2); };
+ because b = () => { result = sut.item_that_belongs_to(2); };
static string result;
}
@@ -43,8 +43,8 @@ namespace MoMoney.Infrastructure.caching
because b = () =>
{
- sut.Add(10, "10");
- result = sut.ContainsAnItemFor(10);
+ sut.add(10, "10");
+ result = sut.contains_an_item_for(10);
};
static bool result;
@@ -55,7 +55,7 @@ namespace MoMoney.Infrastructure.caching
{
it should_return_false = () => result.should_be_false();
- because b = () => { result = sut.ContainsAnItemFor(9); };
+ because b = () => { result = sut.contains_an_item_for(9); };
static bool result;
}
@@ -67,9 +67,9 @@ namespace MoMoney.Infrastructure.caching
because b = () =>
{
- sut.Add(6, "6");
- sut.UpdateTheItemFor(6, "7");
- result = sut.ItemThatBelongsTo(6);
+ sut.add(6, "6");
+ sut.update_the_item_for(6, "7");
+ result = sut.item_that_belongs_to(6);
};
static string result;
@@ -82,8 +82,8 @@ namespace MoMoney.Infrastructure.caching
because b = () =>
{
- sut.UpdateTheItemFor(3, "3");
- result = sut.ItemThatBelongsTo(3);
+ sut.update_the_item_for(3, "3");
+ result = sut.item_that_belongs_to(3);
};
static string result;
trunk/product/MyMoney/Infrastructure/caching/IIdentityMap.cs
@@ -4,50 +4,44 @@ namespace MoMoney.Infrastructure.caching
{
public interface IIdentityMap<TKey, TValue>
{
- void Add(TKey key, TValue value);
- void UpdateTheItemFor(TKey key, TValue newValue);
- bool ContainsAnItemFor(TKey key);
- TValue ItemThatBelongsTo(TKey key);
+ void add(TKey key, TValue value);
+ void update_the_item_for(TKey key, TValue new_value);
+ bool contains_an_item_for(TKey key);
+ TValue item_that_belongs_to(TKey key);
}
public class IdentityMap<TKey, TValue> : IIdentityMap<TKey, TValue>
{
- readonly IDictionary<TKey, TValue> itemsInMap;
+ readonly IDictionary<TKey, TValue> items_in_map;
public IdentityMap() : this(new Dictionary<TKey, TValue>())
{
}
- public IdentityMap(IDictionary<TKey, TValue> itemsInMap)
+ public IdentityMap(IDictionary<TKey, TValue> items_in_map)
{
- this.itemsInMap = itemsInMap;
+ this.items_in_map = items_in_map;
}
- public void Add(TKey key, TValue value)
+ public void add(TKey key, TValue value)
{
- itemsInMap.Add(key, value);
+ items_in_map.Add(key, value);
}
- public void UpdateTheItemFor(TKey key, TValue newValue)
+ public void update_the_item_for(TKey key, TValue new_value)
{
- if (ContainsAnItemFor(key))
- {
- itemsInMap[key] = newValue;
- }
- else
- {
- Add(key, newValue);
- }
+ if (contains_an_item_for(key)) items_in_map[key] = new_value;
+ else add(key, new_value);
}
- public bool ContainsAnItemFor(TKey key)
+ public bool contains_an_item_for(TKey key)
{
- return itemsInMap.ContainsKey(key);
+ return items_in_map.ContainsKey(key);
}
- public TValue ItemThatBelongsTo(TKey key)
+ public TValue item_that_belongs_to(TKey key)
{
- return ContainsAnItemFor(key) ? itemsInMap[key] : default(TValue);
+ return contains_an_item_for(key) ? items_in_map[key] : default(TValue);
}
}
}
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/IIdentityMapFactory.cs
@@ -0,0 +1,10 @@
+using System;
+using MoMoney.Infrastructure.caching;
+
+namespace MoMoney.Infrastructure.transactions2
+{
+ public interface IIdentityMapFactory
+ {
+ IIdentityMap<Guid, T> create_for<T>();
+ }
+}
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/Session.cs
@@ -0,0 +1,24 @@
+using MoMoney.Domain.Core;
+
+namespace MoMoney.Infrastructure.transactions2
+{
+ public interface ISession
+ {
+ void save<T>(T entity) where T : IEntity;
+ }
+
+ public class Session : ISession
+ {
+ IIdentityMapFactory factory;
+
+ public Session(IIdentityMapFactory factory)
+ {
+ this.factory = factory;
+ }
+
+ public void save<T>(T entity) where T : IEntity
+ {
+ factory.create_for<T>().add(entity.Id, entity);
+ }
+ }
+}
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/SessionSpecs.cs
@@ -0,0 +1,45 @@
+using System;
+using developwithpassion.bdd.contexts;
+using MoMoney.Domain.Core;
+using MoMoney.Infrastructure.caching;
+using MoMoney.Testing.spechelpers.contexts;
+using MoMoney.Testing.spechelpers.core;
+
+namespace MoMoney.Infrastructure.transactions2
+{
+ public class SessionSpecs
+ {
+ }
+
+ public class behaves_like_session : concerns_for<ISession, Session>
+ {
+ context c = () => { factory = the_dependency<IIdentityMapFactory>(); };
+
+ protected static IIdentityMapFactory factory;
+ }
+
+ public class when_saving_an_item_to_a_session : behaves_like_session
+ {
+ it should_add_the_entity_to_the_identity_map = () => map.was_told_to(x => x.add(guid, entity));
+
+ context c = () =>
+ {
+ guid = Guid.NewGuid();
+ entity = an<ITestEntity>();
+ map = an<IIdentityMap<Guid, ITestEntity>>();
+
+ when_the(entity).is_told_to(x => x.Id).it_will_return(guid);
+ when_the(factory).is_told_to(x => x.create_for<ITestEntity>()).it_will_return(map);
+ };
+
+ because b = () => { sut.save(entity); };
+
+ static ITestEntity entity;
+ static IIdentityMap<Guid, ITestEntity> map;
+ static Guid guid;
+ }
+
+ public interface ITestEntity : IEntity
+ {
+ }
+}
\ No newline at end of file
trunk/product/MyMoney/MyMoney.csproj
@@ -295,6 +295,9 @@
<Compile Include="Infrastructure\Threading\WorkerThread.cs">
<SubType>Component</SubType>
</Compile>
+ <Compile Include="Infrastructure\transactions2\IIdentityMapFactory.cs" />
+ <Compile Include="Infrastructure\transactions2\Session.cs" />
+ <Compile Include="Infrastructure\transactions2\SessionSpecs.cs" />
<Compile Include="Infrastructure\transactions\IUnitOfWorkRegistrationFactory.cs" />
<Compile Include="Infrastructure\transactions\NullUnitOfWork.cs" />
<Compile Include="Infrastructure\transactions\UnitOfWork.cs" />