Commit 9c9271d

mo <email@solidware.ca>
2011-03-20 06:07:23
implement db4o unit of work.
1 parent 9701078
product/service/orm/Connection.cs
@@ -1,6 +1,8 @@
-namespace solidware.financials.service.orm
+using Db4objects.Db4o;
+
+namespace solidware.financials.service.orm
 {
-    public interface Connection
+    public interface Connection : IObjectContainer, Disposable
     {
     }
 }
\ No newline at end of file
product/service/orm/DB40UnitOfWorkFactory.cs
@@ -6,6 +6,7 @@ namespace solidware.financials.service.orm
     {
         readonly ConnectionFactory factory;
         readonly Context context;
+        Key<Connection> key = new TypedKey<Connection>();
 
         public DB40UnitOfWorkFactory(ConnectionFactory factory, Context context)
         {
@@ -15,11 +16,11 @@ namespace solidware.financials.service.orm
 
         public UnitOfWork create()
         {
-            if( context.contains(new TypedKey<Connection>()))
-                return new EmptyUnitOfWork();
+            if( context.contains(key)) return new EmptyUnitOfWork();
+
             var connection = factory.Open();
-            context.add(new TypedKey<Connection>(), connection);
-            return new DB40UnitOfWork(connection);
+            context.add(key, connection);
+            return new DB4OUnitOfWork(connection);
         }
     }
 }
\ No newline at end of file
product/service/orm/DB4OUnitOfWork.cs
@@ -0,0 +1,25 @@
+namespace solidware.financials.service.orm
+{
+    public class DB4OUnitOfWork : UnitOfWork
+    {
+        readonly Connection connection;
+        bool was_committed;
+
+        public DB4OUnitOfWork(Connection connection)
+        {
+            this.connection = connection;
+        }
+
+        public void Dispose()
+        {
+            if (!was_committed) connection.Rollback();
+            connection.Dispose();
+        }
+
+        public void commit()
+        {
+            connection.Commit();
+            was_committed = true;
+        }
+    }
+}
\ No newline at end of file
product/service/service.csproj
@@ -59,7 +59,7 @@
     <Compile Include="handlers\FindAllFamilyHandler.cs" />
     <Compile Include="orm\Connection.cs" />
     <Compile Include="orm\ConnectionFactory.cs" />
-    <Compile Include="orm\DB40UnitOfWork.cs" />
+    <Compile Include="orm\DB4OUnitOfWork.cs" />
     <Compile Include="orm\DB40UnitOfWorkFactory.cs" />
     <Compile Include="orm\DB4OPersonRepository.cs" />
     <Compile Include="orm\Disposable.cs" />
product/specs/unit/service/orm/DB4OUnitOfWorkFactorySpecs.cs
@@ -31,7 +31,7 @@ namespace specs.unit.service.orm
 
             It should_return_a_new_unit_of_work = () =>
             {
-                result.ShouldBe(typeof(DB40UnitOfWork));
+                result.ShouldBe(typeof(DB4OUnitOfWork));
             };
 
             Establish context = () =>
product/specs/unit/service/orm/DB4OUnitOfWorkSpecs.cs
@@ -0,0 +1,70 @@
+using Machine.Specifications;
+using Rhino.Mocks;
+using solidware.financials.service.orm;
+
+namespace specs.unit.service.orm
+{
+    public class DB4OUnitOfWorkSpecs
+    {
+        public class concern
+        {
+            Establish context = () =>
+            {
+                session = Create.dependency<Connection>();
+                sut = new DB4OUnitOfWork(session);
+            };
+
+            static protected DB4OUnitOfWork sut;
+            static protected Connection session;
+        }
+
+        [Subject(typeof(DB4OUnitOfWork))]
+        public class when_disposing_a_unit_of_work_that_has_not_been_committed : concern
+        {
+            It should_roll_back_the_transaction = () =>
+            {
+                session.AssertWasCalled(x => x.Rollback());
+            };
+            It should_dispose_the_transaction = () =>
+            {
+                session.AssertWasCalled(x => x.Dispose());
+            };
+
+            Because of = () =>
+            {
+                sut.Dispose();
+            };
+        }
+        [Subject(typeof(DB4OUnitOfWork))]
+        public class when_disposing_a_unit_of_work_that_has_been_committed : concern
+        {
+            It should_not_roll_back_the_transaction = () =>
+            {
+                session.AssertWasNotCalled(x => x.Rollback());
+            };
+            It should_dispose_the_transaction = () =>
+            {
+                session.AssertWasCalled(x => x.Dispose());
+            };
+
+            Because of = () =>
+            {
+                sut.commit();
+                sut.Dispose();
+            };
+        }
+        [Subject(typeof(DB4OUnitOfWork))]
+        public class when_committing_a_unit_of_work : concern
+        {
+            It should_commit_the_transaction = () =>
+            {
+                session.AssertWasCalled(x => x.Commit());
+            };
+
+            Because of = () =>
+            {
+                sut.commit();
+            };
+        }
+    }
+}
\ No newline at end of file
product/specs/specs.csproj
@@ -36,6 +36,7 @@
     <Reference Include="Castle.Core">
       <HintPath>..\..\thirdparty\castle\Castle.Core.dll</HintPath>
     </Reference>
+    <Reference Include="Db4objects.Db4o, Version=8.0.184.15484, Culture=neutral, PublicKeyToken=6199cd4f203aa8eb, processorArchitecture=MSIL" />
     <Reference Include="gorilla.infrastructure, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL" />
     <Reference Include="gorilla.utility, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
@@ -63,6 +64,7 @@
     <Compile Include="Create.cs" />
     <Compile Include="unit\infrastructure\ProxyFactorySpecs.cs" />
     <Compile Include="unit\service\orm\DB4OUnitOfWorkFactorySpecs.cs" />
+    <Compile Include="unit\service\orm\DB4OUnitOfWorkSpecs.cs" />
     <Compile Include="unit\service\orm\UnitOfWorkInterceptorSpecs.cs" />
     <Compile Include="unit\ui\InMemoryApplicationStateSpecs.cs" />
     <Compile Include="unit\ui\presenters\AddFamilyMemberPresenterSpecs.cs" />