Commit 6f41a5d

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-04-02 00:36:44
implementing the session.
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@132 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent a38fdf9
Changed files (6)
trunk/product/MyMoney/Infrastructure/transactions2/IDatabase.cs
@@ -0,0 +1,9 @@
+using System.Collections.Generic;
+
+namespace MoMoney.Infrastructure.transactions2
+{
+    public interface IDatabase
+    {
+        IEnumerable<T> fetch_all<T>();
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/Session.cs
@@ -10,6 +10,7 @@ namespace MoMoney.Infrastructure.transactions2
     {
         IEnumerable<T> all<T>();
         void save<T>(T entity) where T : IEntity;
+        void update<T>(T entity) where T : IEntity;
         void flush();
     }
 
@@ -17,18 +18,20 @@ namespace MoMoney.Infrastructure.transactions2
     {
         readonly IIdentityMapFactory factory;
         ITransaction transaction;
+        IDatabase database;
         readonly IDictionary<Type, object> identity_maps;
 
-        public Session(IIdentityMapFactory factory, ITransaction transaction)
+        public Session(IIdentityMapFactory factory, ITransaction transaction, IDatabase database)
         {
             this.factory = factory;
+            this.database = database;
             this.transaction = transaction;
             identity_maps = new Dictionary<Type, object>();
         }
 
         public IEnumerable<T> all<T>()
         {
-            return get_identity_map_for<T>().all();
+            return get_identity_map_for<T>().all().join_with(database.fetch_all<T>());
         }
 
         public void save<T>(T entity) where T : IEntity
@@ -37,6 +40,11 @@ namespace MoMoney.Infrastructure.transactions2
             transaction.add_transient(entity);
         }
 
+        public void update<T>(T entity) where T : IEntity
+        {
+            get_identity_map_for<T>().update_the_item_for(entity.Id, entity);
+        }
+
         public void flush()
         {
             transaction.commit_changes();
trunk/product/MyMoney/Infrastructure/transactions2/SessionSpecs.cs
@@ -18,10 +18,12 @@ namespace MoMoney.Infrastructure.transactions2
                         {
                             factory = the_dependency<IIdentityMapFactory>();
                             transaction = the_dependency<ITransaction>();
+                            database = the_dependency<IDatabase>();
                         };
 
         protected static IIdentityMapFactory factory;
         protected static ITransaction transaction;
+        protected static IDatabase database;
     }
 
     public class when_saving_a_transient_item_to_a_session : behaves_like_session
@@ -80,7 +82,8 @@ namespace MoMoney.Infrastructure.transactions2
                                                                  ///?how
                                                              };
 
-        context c = () => {
+        context c = () =>
+                        {
                             guid = Guid.NewGuid();
                             entity = an<ITestEntity>();
                             map = an<IIdentityMap<Guid, ITestEntity>>();
@@ -88,7 +91,7 @@ namespace MoMoney.Infrastructure.transactions2
                             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);
                             when_the(map).is_told_to(x => x.all()).it_will_return(entity);
-        };
+                        };
 
         because b = () =>
                         {
@@ -102,6 +105,36 @@ namespace MoMoney.Infrastructure.transactions2
         static IIdentityMap<Guid, ITestEntity> map;
     }
 
+    public class when_updating_a_persistent_entity : behaves_like_session
+    {
+        it should_update_the_item_in_the_map = () => map.was_told_to(x => x.update_the_item_for(guid,modified));
+
+        context c = () =>
+                        {
+                            guid = Guid.NewGuid();
+                            original = an<ITestEntity>();
+                            modified = an<ITestEntity>();
+
+                            map = an<IIdentityMap<Guid, ITestEntity>>();
+                            when_the(original).is_told_to(x => x.Id).it_will_return(guid);
+                            when_the(modified).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);
+                            when_the(database).is_told_to(x => x.fetch_all<ITestEntity>()).it_will_return(original);
+                            when_the(map).is_told_to(x => x.all()).it_will_return_nothing();
+                        };
+
+        because b = () =>
+                        {
+                            sut.all<ITestEntity>();
+                            sut.update(modified);
+                        };
+
+        static Guid guid;
+        static ITestEntity original;
+        static ITestEntity modified;
+        static IIdentityMap<Guid, ITestEntity> map;
+    }
+
 
     public interface ITestEntity : IEntity
     {
trunk/product/MyMoney/Utility/Extensions/EnumerableExtensions.cs
@@ -39,5 +39,15 @@ namespace MoMoney.Utility.Extensions
         {
             foreach (var item in items ?? new List<T>()) action(item);
         }
+
+        public static IEnumerable<T> join_with<T>(this IEnumerable<T> left, IEnumerable<T> right)
+        {
+            if (null == right) return left;
+
+            var list = new List<T>();
+            list.AddRange(left);
+            list.AddRange(right);
+            return list;
+        }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Utility/Extensions/EnumerableExtensionsSpecs.cs
@@ -0,0 +1,29 @@
+using System.Collections.Generic;
+using developwithpassion.bdd.contexts;
+using MoMoney.Testing.spechelpers.contexts;
+using MoMoney.Testing.spechelpers.core;
+
+namespace MoMoney.Utility.Extensions
+{
+    public class EnumerableExtensionsSpecs
+    {
+    }
+
+    public class when_joining_one_collection_with_another : concerns
+    {
+        it should_return_the_items_from_both = () =>
+                                                   {
+                                                       results.should_contain(1);
+                                                       results.should_contain(2);
+                                                   };
+
+        because b = () => { results = new List<int> {1}.join_with(new List<int> {2}); };
+
+        static IEnumerable<int> results;
+    }
+
+    public class when_attemping_to_join_a_list_with_a_null_value : concerns
+    {
+        it should_return_the_original_list = () => new List<int> {1}.join_with(null).should_contain(1);
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/MyMoney.csproj
@@ -296,6 +296,7 @@
     <Compile Include="Infrastructure\Threading\WorkerThread.cs">
       <SubType>Component</SubType>
     </Compile>
+    <Compile Include="Infrastructure\transactions2\IDatabase.cs" />
     <Compile Include="Infrastructure\transactions2\IIdentityMapFactory.cs" />
     <Compile Include="Infrastructure\transactions2\ITransaction.cs" />
     <Compile Include="Infrastructure\transactions2\Session.cs" />
@@ -638,6 +639,7 @@
     <Compile Include="Utility\Core\OrSpecificationSpecs.cs" />
     <Compile Include="Utility\Core\PredicateSpecification.cs" />
     <Compile Include="Utility\Core\PredicateSpecificationSpecs.cs" />
+    <Compile Include="Utility\Extensions\EnumerableExtensionsSpecs.cs" />
     <Compile Include="Utility\Extensions\RegistryExtensions.cs" />
     <Compile Include="Presentation\Model\Menu\File\FileMenu.cs" />
     <Compile Include="Presentation\Model\Menu\Help\HelpMenu.cs" />