Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracted interfaces from UndoService and ChangeFactory for IoC container registration #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 13 additions & 53 deletions src/MonitoredUndo/ChangeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace MonitoredUndo
{

public class ChangeFactory
/// <inheritdoc cref="IChangeFactory"/>
public class ChangeFactory : IChangeFactory
{

/// <inheritdoc cref="IChangeFactory.ThrowExceptionOnCollectionResets"/>
public bool ThrowExceptionOnCollectionResets
{
get { return _ThrowExceptionOnCollectionResets; }
Expand All @@ -23,14 +23,7 @@ public bool ThrowExceptionOnCollectionResets
private bool _ThrowExceptionOnCollectionResets = true;


/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that changed. (Case sensitive, used by reflection.)</param>
/// <param name="oldValue">The old value of the property.</param>
/// <param name="newValue">The new value of the property.</param>
/// <returns>A Change that can be added to the UndoRoot's undo stack.</returns>
/// <inheritdoc cref="IChangeFactory.GetChange(object, string, object, object)"/>
public virtual Change GetChange(object instance, string propertyName, object oldValue, object newValue)
{
var undoMetadata = instance as IUndoMetadata;
Expand All @@ -45,26 +38,13 @@ public virtual Change GetChange(object instance, string propertyName, object old
return change;
}

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that changed. (Case sensitive, used by reflection.)</param>
/// <param name="oldValue">The old value of the property.</param>
/// <param name="newValue">The new value of the property.</param>
/// <inheritdoc cref="IChangeFactory.OnChanging(object, string, object, object)"/>
public virtual void OnChanging(object instance, string propertyName, object oldValue, object newValue)
{
OnChanging(instance, propertyName, oldValue, newValue, propertyName);
}

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that changed. (Case sensitive, used by reflection.)</param>
/// <param name="oldValue">The old value of the property.</param>
/// <param name="newValue">The new value of the property.</param>
/// <param name="descriptionOfChange">A description of this change.</param>
/// <inheritdoc cref="IChangeFactory.OnChanging(object, string, object, object, string)"/>
public virtual void OnChanging(object instance, string propertyName, object oldValue, object newValue, string descriptionOfChange)
{
var supportsUndo = instance as ISupportsUndo;
Expand All @@ -80,14 +60,7 @@ public virtual void OnChanging(object instance, string propertyName, object oldV
UndoService.Current[root].AddChange(change, descriptionOfChange);
}

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that exposes the collection that changed. (Case sensitive, used by reflection.)</param>
/// <param name="collection">The collection that had an item added / removed.</param>
/// <param name="e">The NotifyCollectionChangedEventArgs event args parameter, with info about the collection change.</param>
/// <returns>A Change that can be added to the UndoRoot's undo stack.</returns>
/// <inheritdoc cref="IChangeFactory.GetCollectionChange(object, string, object, NotifyCollectionChangedEventArgs)"/>
public virtual IList<Change> GetCollectionChange(object instance, string propertyName, object collection, NotifyCollectionChangedEventArgs e)
{
var undoMetadata = instance as IUndoMetadata;
Expand All @@ -104,7 +77,7 @@ public virtual IList<Change> GetCollectionChange(object instance, string propert
case NotifyCollectionChangedAction.Add:
foreach (var item in e.NewItems)
{
Change change = null;
Change change = null;
if (collection as IList != null)
{
change = new CollectionAddChange(instance, propertyName, (IList)collection,
Expand All @@ -116,7 +89,7 @@ public virtual IList<Change> GetCollectionChange(object instance, string propert
var keyProperty = item.GetType().GetProperty("Key");
var valueProperty = item.GetType().GetProperty("Value");
change = new DictionaryAddChange(instance, propertyName, (IDictionary)collection,
keyProperty.GetValue(item, null), valueProperty.GetValue(item, null));
keyProperty.GetValue(item, null), valueProperty.GetValue(item, null));
}
ret.Add(change);
}
Expand Down Expand Up @@ -146,7 +119,7 @@ public virtual IList<Change> GetCollectionChange(object instance, string propert
break;

case NotifyCollectionChangedAction.Move:
var moveChange = new CollectionMoveChange(instance, propertyName, (IList) collection,
var moveChange = new CollectionMoveChange(instance, propertyName, (IList)collection,
e.NewStartingIndex,
e.OldStartingIndex);
ret.Add(moveChange);
Expand All @@ -156,7 +129,7 @@ public virtual IList<Change> GetCollectionChange(object instance, string propert
for (int i = 0; i < e.OldItems.Count; i++)
{
Change change = null;

if (collection as IList != null)
{
change = new CollectionReplaceChange(instance, propertyName, (IList)collection,
Expand Down Expand Up @@ -188,26 +161,13 @@ public virtual IList<Change> GetCollectionChange(object instance, string propert
return ret;
}

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that exposes the collection that changed. (Case sensitive, used by reflection.)</param>
/// <param name="collection">The collection that had an item added / removed.</param>
/// <param name="e">The NotifyCollectionChangedEventArgs event args parameter, with info about the collection change.</param>
/// <inheritdoc cref="IChangeFactory.OnCollectionChanged(object, string, object, NotifyCollectionChangedEventArgs)"/>
public virtual void OnCollectionChanged(object instance, string propertyName, object collection, NotifyCollectionChangedEventArgs e)
{
OnCollectionChanged(instance, propertyName, collection, e, propertyName);
}

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that exposes the collection that changed. (Case sensitive, used by reflection.)</param>
/// <param name="collection">The collection that had an item added / removed.</param>
/// <param name="e">The NotifyCollectionChangedEventArgs event args parameter, with info about the collection change.</param>
/// <param name="descriptionOfChange">A description of the change.</param>
/// <inheritdoc cref="IChangeFactory.OnCollectionChanged(object, string, object, NotifyCollectionChangedEventArgs, string)"/>
public virtual void OnCollectionChanged(object instance, string propertyName, object collection, NotifyCollectionChangedEventArgs e, string descriptionOfChange)
{
var supportsUndo = instance as ISupportsUndo;
Expand Down
74 changes: 74 additions & 0 deletions src/MonitoredUndo/IChangeFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Generic;
using System.Collections.Specialized;

namespace MonitoredUndo
{
/// <summary>
/// A factory for creating <see cref="Change"/> instances.
/// </summary>
public interface IChangeFactory
{
/// <summary>
/// Whether to throw an exception when attempting to create a collection change for a collection that has been reset.
/// </summary>
bool ThrowExceptionOnCollectionResets { get; set; }

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that changed. (Case sensitive, used by reflection.)</param>
/// <param name="oldValue">The old value of the property.</param>
/// <param name="newValue">The new value of the property.</param>
/// <returns>A Change that can be added to the UndoRoot's undo stack.</returns>
Change GetChange(object instance, string propertyName, object oldValue, object newValue);

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that exposes the collection that changed. (Case sensitive, used by reflection.)</param>
/// <param name="collection">The collection that had an item added / removed.</param>
/// <param name="e">The NotifyCollectionChangedEventArgs event args parameter, with info about the collection change.</param>
/// <returns>A Change that can be added to the UndoRoot's undo stack.</returns>
IList<Change> GetCollectionChange(object instance, string propertyName, object collection, NotifyCollectionChangedEventArgs e);

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that changed. (Case sensitive, used by reflection.)</param>
/// <param name="oldValue">The old value of the property.</param>
/// <param name="newValue">The new value of the property.</param>
void OnChanging(object instance, string propertyName, object oldValue, object newValue);

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that changed. (Case sensitive, used by reflection.)</param>
/// <param name="oldValue">The old value of the property.</param>
/// <param name="newValue">The new value of the property.</param>
/// <param name="descriptionOfChange">A description of this change.</param>
void OnChanging(object instance, string propertyName, object oldValue, object newValue, string descriptionOfChange);

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that exposes the collection that changed. (Case sensitive, used by reflection.)</param>
/// <param name="collection">The collection that had an item added / removed.</param>
/// <param name="e">The NotifyCollectionChangedEventArgs event args parameter, with info about the collection change.</param>
void OnCollectionChanged(object instance, string propertyName, object collection, NotifyCollectionChangedEventArgs e);

/// <summary>
/// Construct a Change instance with actions for undo / redo.
/// </summary>
/// <param name="instance">The instance that changed.</param>
/// <param name="propertyName">The property name that exposes the collection that changed. (Case sensitive, used by reflection.)</param>
/// <param name="collection">The collection that had an item added / removed.</param>
/// <param name="e">The NotifyCollectionChangedEventArgs event args parameter, with info about the collection change.</param>
/// <param name="descriptionOfChange">A description of the change.</param>
void OnCollectionChanged(object instance, string propertyName, object collection, NotifyCollectionChangedEventArgs e, string descriptionOfChange);
}
}
21 changes: 21 additions & 0 deletions src/MonitoredUndo/IUndoService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace MonitoredUndo
{
/// <summary>
/// A service representing the top level of the undo / redo system.
/// It contains one or more UndoRoots, accessible via an indexer.
/// </summary>
public interface IUndoService
{
/// <summary>
/// Get (or create) an UndoRoot for the specified object or document instance.
/// </summary>
/// <param name="root">The object that represents the root of the document or object hierarchy.</param>
/// <returns>An UndoRoot instance for this object.</returns>
UndoRoot this[object root] { get; }

/// <summary>
/// Clear the cached UndoRoots.
/// </summary>
void Clear();
}
}
26 changes: 10 additions & 16 deletions src/MonitoredUndo/UndoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace MonitoredUndo
{

public class UndoService
{
/// <inheritdoc cref="IUndoService"/>
public class UndoService : IUndoService
{

private static UndoService _Current;
private static IDictionary<Type, WeakReference> _CurrentRootInstances;
Expand Down Expand Up @@ -88,7 +88,7 @@ public static void SetCurrentDocumentInstance<T>(T instance) where T : class
}
}



// Use a weak reference for the key, to prevent memory leaks.
private IDictionary<WeakReference, UndoRoot> _Roots;
Expand All @@ -99,15 +99,11 @@ public UndoService()
// Use a custom comparer to compare the WeakReference keys.
_Roots = new Dictionary<WeakReference, UndoRoot>(new WeakReferenceComparer());
}




/// <summary>
/// Get (or create) an UndoRoot for the specified object or document instance.
/// </summary>
/// <param name="root">The object that represents the root of the document or object hierarchy.</param>
/// <returns>An UndoRoot instance for this object.</returns>


/// <inheritdoc cref="IUndoService.this[object]"/>
public UndoRoot this[object root]
{
get
Expand All @@ -129,11 +125,9 @@ public UndoRoot this[object root]

return ret;
}
}

/// <summary>
/// Clear the cached UndoRoots.
/// </summary>
}

/// <inheritdoc cref="IUndoService.Clear"/>
public void Clear()
{
this._Roots.Clear();
Expand Down