Commit 1bfbfe5

mo k <mo@mokhan.ca>
2012-04-17 15:43:15
extract explicit query method for net production into a visitor.
1 parent 9f193e2
src/domain/utility/IVisitor.cs
@@ -0,0 +1,23 @@
+namespace utility
+{
+  public interface IVisitor<T>
+  {
+    void Visit(T item);
+  }
+
+  public interface IVisitable<T>
+  {
+    void Accept(IVisitor<T> visitor);
+  }
+  public interface IValueReturningVisitor<T, TResult> : IVisitor<T>
+  {
+    TResult Result();
+  }
+  public static class Visiting{
+    public static TResult AcceptAndReturnResultFrom<T, TResult>(this IVisitable<T> visitable, IValueReturningVisitor<T, TResult> visitor)
+    {
+      visitable.Accept(visitor);
+      return visitor.Result();
+    }
+  }
+}
src/domain/domain.csproj
@@ -55,6 +55,7 @@
     <Compile Include="Units.cs" />
     <Compile Include="Well.cs" />
     <Compile Include="IUnitOfMeasure.cs" />
+    <Compile Include="utility\IVisitor.cs" />
     <Compile Include="utility\iterating.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
src/domain/DrillSchedule.cs
@@ -5,7 +5,7 @@ namespace domain
   using System.Linq;
   using utility;
 
-  public class DrillSchedule
+  public class DrillSchedule : IVisitable<IWell>
   {
     ICollection<IWell> wells = new List<IWell>();
 
@@ -34,6 +34,11 @@ namespace domain
       return result;
     }
 
+    public void Accept(IVisitor<IWell> visitor )
+    {
+      Accept(visitor.Visit);
+    }
+
     void Accept(Action<IWell> visitor )
     {
       wells.Each(well =>
@@ -42,4 +47,25 @@ namespace domain
       });
     }
   }
+  public class EstimatedNetProductionFor<T> : IValueReturningVisitor<IWell, IQuantity> where T : ICommodity, new()
+  {
+    Month month;
+    IQuantity result;
+
+    public EstimatedNetProductionFor(Month month)
+    {
+      this.month = month;
+      result = new Quantity(0, new BOED());
+    }
+
+    public void Visit(IWell well)
+    {
+      result = result.Plus(well.NetProductionFor<T>(month));
+    }
+
+    public IQuantity Result()
+    {
+      return this.result;
+    }
+  }
 }
src/test/WellSpecs.cs
@@ -5,6 +5,7 @@
   using System.Linq;
   using Machine.Specifications;
   using domain;
+  using utility;
 
   public class WellSpecs
   {
@@ -86,7 +87,7 @@
 
         It should_be_able_to_tell_the_estimated_net_total_production_for_any_month = () =>
         {
-          sut.EstimatedNetProductionFor<All>(jan2013).ShouldEqual(75.BOED());
+          sut.AcceptAndReturnResultFrom(new EstimatedNetProductionFor<All>(jan2013)).ShouldEqual(75.BOED());
         };
 
         It should_be_able_to_tell_the_estimated_net_production_for_oil = ()=>
.gitignore
@@ -3,3 +3,4 @@
 *.suo
 *.pidb
 *.userprefs
+tags