Commit 9701078

mo <email@solidware.ca>
2011-03-20 05:55:51
implement the unit of work factory.
1 parent 3c90f65
product/service/orm/DB40UnitOfWork.cs
@@ -0,0 +1,21 @@
+using System;
+
+namespace solidware.financials.service.orm
+{
+    public class DB40UnitOfWork : UnitOfWork
+    {
+        public DB40UnitOfWork(Connection connection)
+        {
+        }
+
+        public void Dispose()
+        {
+            throw new NotImplementedException();
+        }
+
+        public void commit()
+        {
+            throw new NotImplementedException();
+        }
+    }
+}
\ No newline at end of file
product/service/orm/DB40UnitOfWorkFactory.cs
@@ -0,0 +1,25 @@
+using gorilla.utility;
+
+namespace solidware.financials.service.orm
+{
+    public class DB40UnitOfWorkFactory : UnitOfWorkFactory
+    {
+        readonly ConnectionFactory factory;
+        readonly Context context;
+
+        public DB40UnitOfWorkFactory(ConnectionFactory factory, Context context)
+        {
+            this.factory = factory;
+            this.context = context;
+        }
+
+        public UnitOfWork create()
+        {
+            if( context.contains(new TypedKey<Connection>()))
+                return new EmptyUnitOfWork();
+            var connection = factory.Open();
+            context.add(new TypedKey<Connection>(), connection);
+            return new DB40UnitOfWork(connection);
+        }
+    }
+}
\ No newline at end of file
product/service/orm/EmptyUnitOfWork.cs
@@ -0,0 +1,17 @@
+using System;
+
+namespace solidware.financials.service.orm
+{
+    public class EmptyUnitOfWork : UnitOfWork
+    {
+        public void Dispose()
+        {
+            throw new NotImplementedException();
+        }
+
+        public void commit()
+        {
+            throw new NotImplementedException();
+        }
+    }
+}
\ No newline at end of file
product/service/service.csproj
@@ -59,8 +59,11 @@
     <Compile Include="handlers\FindAllFamilyHandler.cs" />
     <Compile Include="orm\Connection.cs" />
     <Compile Include="orm\ConnectionFactory.cs" />
+    <Compile Include="orm\DB40UnitOfWork.cs" />
+    <Compile Include="orm\DB40UnitOfWorkFactory.cs" />
     <Compile Include="orm\DB4OPersonRepository.cs" />
     <Compile Include="orm\Disposable.cs" />
+    <Compile Include="orm\EmptyUnitOfWork.cs" />
     <Compile Include="orm\InMemoryDatabase.cs" />
     <Compile Include="orm\LastOpened.cs" />
     <Compile Include="orm\PersonRepository.cs" />
product/specs/unit/service/orm/DB4OUnitOfWorkFactorySpecs.cs
@@ -0,0 +1,81 @@
+using gorilla.utility;
+using Machine.Specifications;
+using Rhino.Mocks;
+using solidware.financials.service.orm;
+
+namespace specs.unit.service.orm
+{
+    public class DB4OUnitOfWorkFactorySpecs
+    {
+        public abstract class concern
+        {
+            Establish context = () =>
+            {
+                factory = Create.dependency<ConnectionFactory>();
+                the_context = Create.dependency<Context>();
+                sut = new DB40UnitOfWorkFactory(factory, the_context);
+            };
+
+            static protected ConnectionFactory factory;
+            static protected Context the_context;
+            static protected UnitOfWorkFactory sut;
+        }
+
+        [Subject(typeof(DB40UnitOfWorkFactory))]
+        public class when_starting_a_new_unit_of_work : concern
+        {
+            It should_bind_a_new_connection_to_the_current_context = () =>
+            {
+                the_context.AssertWasCalled(x => x.add(new TypedKey<Connection>(), connection));
+            };
+
+            It should_return_a_new_unit_of_work = () =>
+            {
+                result.ShouldBe(typeof(DB40UnitOfWork));
+            };
+
+            Establish context = () =>
+            {
+                connection = Create.an<Connection>();
+                factory.Stub(x => x.Open()).Return(connection);
+                the_context.Stub(x => x.contains(new TypedKey<Connection>())).Return(false);
+            };
+
+            Because of = () =>
+            {
+                result = sut.create();
+            };
+
+            static Connection connection;
+            static UnitOfWork result;
+        }
+        [Subject(typeof(DB40UnitOfWorkFactory))]
+        public class when_a_unit_of_work_has_already_been_started : concern
+        {
+            It should_not_bind_a_new_connection_to_the_current_context = () =>
+            {
+                the_context.AssertWasNotCalled(x => x.add(new TypedKey<Connection>(), connection));
+            };
+
+            It should_return_an_empty_unit_of_work = () =>
+            {
+                result.ShouldBe(typeof(EmptyUnitOfWork));
+            };
+
+            Establish context = () =>
+            {
+                connection = Create.an<Connection>();
+                factory.Stub(x => x.Open()).Return(connection);
+                the_context.Stub(x => x.contains(new TypedKey<Connection>())).Return(true);
+            };
+
+            Because of = () =>
+            {
+                result = sut.create();
+            };
+
+            static Connection connection;
+            static UnitOfWork result;
+        }
+    }
+}
\ No newline at end of file
product/specs/specs.csproj
@@ -62,6 +62,7 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Create.cs" />
     <Compile Include="unit\infrastructure\ProxyFactorySpecs.cs" />
+    <Compile Include="unit\service\orm\DB4OUnitOfWorkFactorySpecs.cs" />
     <Compile Include="unit\service\orm\UnitOfWorkInterceptorSpecs.cs" />
     <Compile Include="unit\ui\InMemoryApplicationStateSpecs.cs" />
     <Compile Include="unit\ui\presenters\AddFamilyMemberPresenterSpecs.cs" />