Commits

Martin Eden  committed eb12e4e

When constructing a Change<A, B> object, check that IObjectChange.ChangeSet
property is not null, and throw a more informative exception if it is.

(This code is untested - I wrote it on a machine without SQL Server and I need
to run the tests when I get back into the office.)

  • Participants
  • Parent commits 90d4a8e
  • Branches issue38

Comments (0)

Files changed (4)

File FrameLog/Exceptions/InvalidFrameLogRecordException.cs

+using System;
+
+namespace FrameLog.Exceptions
+{
+    public class InvalidFrameLogRecordException : Exception
+    {
+        public readonly object Record;
+
+        public InvalidFrameLogRecordException(string message, object record)
+            : base(string.Format(message, record))
+        {
+            Record = record;
+        }
+    }
+}

File FrameLog/FrameLog.csproj

     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Exceptions\InvalidFrameLogRecordException.cs" />
     <Compile Include="Exceptions\ObjectTypeDoesNotExistInDataModelException.cs" />
     <Compile Include="Exceptions\ConflictingTransactionException.cs" />
     <Compile Include="Exceptions\FailedToRetrieveObjectException.cs" />

File FrameLog/History/Change.cs

-using System;
+using FrameLog.Exceptions;
+using FrameLog.Models;
+using System;
 
 namespace FrameLog.History
 {
             return string.Format("{0}:{1}:{2}", Author, Timestamp, Value);
         }
     }
+
+    /// <summary>
+    /// Static helper methods for the generic Change type
+    /// </summary>
+    public static class Change
+    {
+        public static Change<TValue, TPrincipal> FromObjectChange<TValue, TPrincipal>(TValue value, IObjectChange<TPrincipal> objectChange)
+        {
+            var changeSet = objectChange.ChangeSet;
+            if (changeSet == null)
+            {
+                throw new InvalidFrameLogRecordException("IObjectChange '{0}'has a null ChangeSet property", objectChange);
+            }
+            return new Change<TValue, TPrincipal>(value, changeSet.Author, changeSet.Timestamp);
+        }
+    }
 }

File FrameLog/History/HistoryExplorer.cs

                 .Where(p => p.PropertyName == propertyName)
                 .OrderByDescending(p => p.ObjectChange.ChangeSet.Timestamp)
                 .AsEnumerable()
-                .Select(p => new Change<TValue, TPrincipal>(binder.Bind<TValue>(p.Value), p.ObjectChange.ChangeSet.Author, p.ObjectChange.ChangeSet.Timestamp));
+                .Select(p => Change.FromObjectChange(binder.Bind<TValue>(p.Value), p.ObjectChange));
         }
         /// <summary>
         /// Rehydrates versions of the object, one for each logged change to the object,
             if (firstChange == null || !isCreation(model, firstChange))
                 throw new CreationDoesNotExistInLogException(model);
             else
-            {
-                var set = firstChange.ChangeSet;
-                return new Change<TModel, TPrincipal>(model, set.Author, set.Timestamp);
-            }
+                return Change.FromObjectChange(model, firstChange);
         }
 
         /// <summary>
                 var value = binder.Bind(propertyChange.Value, property.PropertyType);
                 property.SetValue(newVersion, value, null);
             }
-            return new Change<TModel, TPrincipal>(newVersion, 
-                change.ChangeSet.Author, 
-                change.ChangeSet.Timestamp);
+            return Change.FromObjectChange(newVersion, change);
         }
 
         /// <summary>