Commit 31020b6

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-04-04 23:56:58
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@140 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent 3f96811
trunk/product/MyMoney/Infrastructure/transactions2/ChangeTracker.cs
@@ -1,10 +1,11 @@
 using System.Collections.Generic;
 using System.Linq;
+using MoMoney.Domain.Core;
 using MoMoney.Utility.Extensions;
 
 namespace MoMoney.Infrastructure.transactions2
 {
-    public class ChangeTracker<T> : IChangeTracker<T>
+    public class ChangeTracker<T> : IChangeTracker<T> where T : IEntity
     {
         readonly ITrackerEntryMapper<T> mapper;
         readonly IStatementRegistry registry;
@@ -37,7 +38,7 @@ namespace MoMoney.Infrastructure.transactions2
 
         public bool is_dirty()
         {
-            return items.Count(x => x.contains_changes()) > 0 || to_be_deleted.Count > 0;
+            return items.Count(x => x.has_changes()) > 0 || to_be_deleted.Count > 0;
         }
 
         public void Dispose()
@@ -47,7 +48,7 @@ namespace MoMoney.Infrastructure.transactions2
 
         void commit(ITrackerEntry<T> entry, IDatabase database)
         {
-            if (entry.contains_changes()) database.apply(registry.prepare_command_for(entry.current));
+            if (entry.has_changes()) database.apply(registry.prepare_command_for(entry.current));
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/ChangeTrackerFactory.cs
@@ -1,3 +1,4 @@
+using MoMoney.Domain.Core;
 using MoMoney.Infrastructure.Container;
 
 namespace MoMoney.Infrastructure.transactions2
@@ -13,7 +14,7 @@ namespace MoMoney.Infrastructure.transactions2
             this.registry = registry;
         }
 
-        public IChangeTracker<T> create_for<T>()
+        public IChangeTracker<T> create_for<T>() where T : IEntity
         {
             return new ChangeTracker<T>(registry.get_a<ITrackerEntryMapper<T>>(), statement_registry);
         }
trunk/product/MyMoney/Infrastructure/transactions2/ChangeTrackerSpecs.cs
@@ -35,7 +35,7 @@ namespace MoMoney.Infrastructure.transactions2
                             var entry = an<ITrackerEntry<IEntity>>();
 
                             when_the(mapper).is_told_to(x => x.map_from(item)).it_will_return(entry);
-                            when_the(entry).is_told_to(x => x.contains_changes()).it_will_return(true);
+                            when_the(entry).is_told_to(x => x.has_changes()).it_will_return(true);
                             when_the(entry).is_told_to(x => x.current).it_will_return(item);
                             when_the(registry).is_told_to(x => x.prepare_command_for(item)).it_will_return(statement);
                         };
@@ -61,7 +61,7 @@ namespace MoMoney.Infrastructure.transactions2
                             var registration = an<ITrackerEntry<IEntity>>();
 
                             when_the(mapper).is_told_to(x => x.map_from(item)).it_will_return(registration);
-                            when_the(registration).is_told_to(x => x.contains_changes()).it_will_return(true);
+                            when_the(registration).is_told_to(x => x.has_changes()).it_will_return(true);
                             when_the(registration).is_told_to(x => x.current).it_will_return(item);
                         };
 
@@ -85,7 +85,7 @@ namespace MoMoney.Infrastructure.transactions2
                             var entry = an<ITrackerEntry<IEntity>>();
 
                             when_the(mapper).is_told_to(x => x.map_from(item)).it_will_return(entry);
-                            when_the(entry).is_told_to(x => x.contains_changes()).it_will_return(false);
+                            when_the(entry).is_told_to(x => x.has_changes()).it_will_return(false);
                         };
 
         because b = () =>
trunk/product/MyMoney/Infrastructure/transactions2/ConfigureDatabaseStep.cs
@@ -0,0 +1,20 @@
+using Db4objects.Db4o.Config;
+using MoMoney.Utility.Core;
+
+namespace MoMoney.Infrastructure.transactions2
+{
+    public interface IConfigureDatabaseStep : IConfiguration<IConfiguration>
+    {
+    }
+
+    public class ConfigureDatabaseStep : IConfigureDatabaseStep
+    {
+        public void configure(IConfiguration item)
+        {
+            item.LockDatabaseFile(false);
+            item.UpdateDepth(10);
+            item.WeakReferences(true);
+            item.AutomaticShutDown(true);
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/ConnectionFactory.cs
@@ -0,0 +1,35 @@
+using Db4objects.Db4o;
+using Db4objects.Db4o.Events;
+using MoMoney.Infrastructure.Extensions;
+using MoMoney.Presentation.Model.Projects;
+
+namespace MoMoney.Infrastructure.transactions2
+{
+    public interface IConnectionFactory
+    {
+        IObjectContainer open_connection_to(IFile the_path_to_the_database_file);
+    }
+
+    public class ConnectionFactory : IConnectionFactory
+    {
+        readonly IConfigureDatabaseStep setup;
+
+        public ConnectionFactory(IConfigureDatabaseStep setup)
+        {
+            this.setup = setup;
+        }
+
+        public IObjectContainer open_connection_to(IFile the_path_to_the_database_file)
+        {
+            var configuration = Db4oFactory.NewConfiguration();
+            setup.configure(configuration);
+
+            var container = Db4oFactory.OpenFile(configuration, the_path_to_the_database_file.path);
+
+            var registry = EventRegistryFactory.ForObjectContainer(container);
+            registry.Creating += (sender, args) => this.log().debug("{0}", args.Object);
+            registry.QueryStarted += (sender, args) => this.log().debug("{0}", args.Query);
+            return container;
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/Database.cs
@@ -1,4 +1,3 @@
-using System;
 using System.Collections.Generic;
 using MoMoney.Domain.Core;
 
@@ -6,14 +5,30 @@ namespace MoMoney.Infrastructure.transactions2
 {
     public class Database : IDatabase
     {
+        readonly IDatabaseConfiguration configuration;
+        readonly IConnectionFactory factory;
+
+        public Database(IDatabaseConfiguration configuration, IConnectionFactory factory)
+        {
+            this.configuration = configuration;
+            this.factory = factory;
+        }
+
         public IEnumerable<T> fetch_all<T>() where T : IEntity
         {
-            throw new NotImplementedException();
+            using (var connection = factory.open_connection_to(configuration.path_to_database()))
+            {
+                return connection.Query<T>();
+            }
         }
 
         public void apply(IStatement statement)
         {
-            throw new NotImplementedException();
+            using (var connection = factory.open_connection_to(configuration.path_to_database()))
+            {
+                statement.prepare(connection);
+                connection.Commit();
+            }
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/DatabaseConfiguration.cs
@@ -0,0 +1,32 @@
+using System.IO;
+using MoMoney.Infrastructure.eventing;
+using MoMoney.Presentation.Model.messages;
+using MoMoney.Presentation.Model.Projects;
+
+namespace MoMoney.Infrastructure.transactions2
+{
+    public interface IDatabaseConfiguration
+    {
+        IFile path_to_database();
+    }
+
+    public class DatabaseConfiguration : IDatabaseConfiguration, IEventSubscriber<NewProjectOpened>
+    {
+        IFile path;
+
+        public DatabaseConfiguration()
+        {
+            path = new ApplicationFile(Path.GetTempFileName());
+        }
+
+        public IFile path_to_database()
+        {
+            return path;
+        }
+
+        public void notify(NewProjectOpened message)
+        {
+            path = new ApplicationFile(Path.GetTempFileName());
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/IChangeTracker.cs
@@ -1,4 +1,5 @@
 using System;
+using MoMoney.Domain.Core;
 
 namespace MoMoney.Infrastructure.transactions2
 {
@@ -8,7 +9,7 @@ namespace MoMoney.Infrastructure.transactions2
         void commit_to(IDatabase database);
     }
 
-    public interface IChangeTracker<T> : IChangeTracker, IDisposable
+    public interface IChangeTracker<T> : IChangeTracker, IDisposable where T : IEntity
     {
         void register(T value);
         void delete(T entity);
trunk/product/MyMoney/Infrastructure/transactions2/IChangeTrackerFactory.cs
@@ -1,7 +1,9 @@
+using MoMoney.Domain.Core;
+
 namespace MoMoney.Infrastructure.transactions2
 {
     public interface IChangeTrackerFactory
     {
-        IChangeTracker<T> create_for<T>();
+        IChangeTracker<T> create_for<T>() where T : IEntity;
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/IStatement.cs
@@ -1,7 +1,9 @@
+using Db4objects.Db4o;
+
 namespace MoMoney.Infrastructure.transactions2
 {
     public interface IStatement
     {
-        
+        void prepare(IObjectContainer connection);
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/IStatementRegistry.cs
@@ -1,8 +1,10 @@
+using MoMoney.Domain.Core;
+
 namespace MoMoney.Infrastructure.transactions2
 {
     public interface IStatementRegistry
     {
-        IStatement prepare_delete_statement_for<T>(T entity);
-        IStatement prepare_command_for<T>(T entity);
+        IStatement prepare_delete_statement_for<T>(T entity) where T : IEntity;
+        IStatement prepare_command_for<T>(T entity) where T : IEntity;
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/ITrackerEntry.cs
@@ -3,6 +3,6 @@ namespace MoMoney.Infrastructure.transactions2
     public interface ITrackerEntry<T>
     {
         T current { get; }
-        bool contains_changes();
+        bool has_changes();
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/transactions2/StatementRegistry.cs
@@ -1,17 +1,51 @@
-using System;
+using Db4objects.Db4o;
+using MoMoney.Domain.Core;
+using MoMoney.Utility.Extensions;
 
 namespace MoMoney.Infrastructure.transactions2
 {
     public class StatementRegistry : IStatementRegistry
     {
-        public IStatement prepare_delete_statement_for<T>(T entity)
+        public IStatement prepare_delete_statement_for<T>(T entity) where T : IEntity
         {
-            throw new NotImplementedException();
+            return new DeletionStatement<T>(entity);
         }
 
-        public IStatement prepare_command_for<T>(T entity)
+        public IStatement prepare_command_for<T>(T entity) where T : IEntity
         {
-            throw new NotImplementedException();
+            return new SaveOrUpdateStatement<T>(entity);
+        }
+    }
+
+    public class SaveOrUpdateStatement<T> : IStatement where T : IEntity
+    {
+        readonly T entity;
+
+        public SaveOrUpdateStatement(T entity)
+        {
+            this.entity = entity;
+        }
+
+        public void prepare(IObjectContainer connection)
+        {
+            var query = connection.Query<T>(x => x.Id.Equals(entity.Id));
+            query.each(x => connection.Delete(x));
+            connection.Store(entity);
+        }
+    }
+
+    public class DeletionStatement<T> : IStatement
+    {
+        readonly T entity;
+
+        public DeletionStatement(T entity)
+        {
+            this.entity = entity;
+        }
+
+        public void prepare(IObjectContainer connection)
+        {
+            connection.Delete(entity);
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/MyMoney.csproj
@@ -215,7 +215,10 @@
     <Compile Include="Infrastructure\transactions2\ChangeTrackerFactory.cs" />
     <Compile Include="Infrastructure\transactions2\ChangeTrackerFactorySpecs.cs" />
     <Compile Include="Infrastructure\transactions2\ChangeTrackerSpecs.cs" />
+    <Compile Include="Infrastructure\transactions2\ConfigureDatabaseStep.cs" />
+    <Compile Include="Infrastructure\transactions2\ConnectionFactory.cs" />
     <Compile Include="Infrastructure\transactions2\Database.cs" />
+    <Compile Include="Infrastructure\transactions2\DatabaseConfiguration.cs" />
     <Compile Include="Infrastructure\transactions2\IChangeTracker.cs" />
     <Compile Include="Infrastructure\transactions2\IChangeTrackerFactory.cs" />
     <Compile Include="Infrastructure\transactions2\IdentityMapProxy.cs" />