Be draconian about using readonly where appropriate

This commit is contained in:
Antony Male 2014-12-04 17:06:12 +00:00
parent 91eab0958f
commit 94d4a31b85
11 changed files with 22 additions and 22 deletions

View File

@ -19,7 +19,7 @@ namespace Stylet
/// </summary> /// </summary>
public class AllActive : ConductorBase<T> public class AllActive : ConductorBase<T>
{ {
private BindableCollection<T> items = new BindableCollection<T>(); private readonly BindableCollection<T> items = new BindableCollection<T>();
/// <summary> /// <summary>
/// All items associated with this conductor /// All items associated with this conductor

View File

@ -13,7 +13,7 @@ namespace Stylet
public class StackNavigation : ConductorBaseWithActiveItem<T> public class StackNavigation : ConductorBaseWithActiveItem<T>
{ {
// We need to remove arbitrary items, so no Stack<T> here! // We need to remove arbitrary items, so no Stack<T> here!
private List<T> history = new List<T>(); private readonly List<T> history = new List<T>();
/// <summary> /// <summary>
/// Activate the given item. This deactivates the previous item, and pushes it onto the history stack /// Activate the given item. This deactivates the previous item, and pushes it onto the history stack

View File

@ -15,7 +15,7 @@ namespace Stylet
/// </summary> /// </summary>
public class OneActive : ConductorBaseWithActiveItem<T> public class OneActive : ConductorBaseWithActiveItem<T>
{ {
private BindableCollection<T> items = new BindableCollection<T>(); private readonly BindableCollection<T> items = new BindableCollection<T>();
/// <summary> /// <summary>
/// Items owned by this Conductor, one of which is active /// Items owned by this Conductor, one of which is active

View File

@ -123,7 +123,7 @@ namespace Stylet
{ {
private readonly WeakReference target; private readonly WeakReference target;
private readonly List<HandlerInvoker> invokers = new List<HandlerInvoker>(); private readonly List<HandlerInvoker> invokers = new List<HandlerInvoker>();
private HashSet<string> channels = new HashSet<string>(); private readonly HashSet<string> channels = new HashSet<string>();
public Handler(object handler, string[] channels) public Handler(object handler, string[] channels)
{ {

View File

@ -29,7 +29,7 @@ namespace Stylet
/// <summary> /// <summary>
/// IModelValidator to use to validate properties. You're expected to write your own, using your favourite validation library /// IModelValidator to use to validate properties. You're expected to write your own, using your favourite validation library
/// </summary> /// </summary>
protected virtual IModelValidator validator protected virtual IModelValidator Validator
{ {
get { return this._validator; } get { return this._validator; }
set set
@ -98,7 +98,7 @@ namespace Stylet
/// <remarks>If you override this, you MUST fire ErrorsChanged as appropriate, and call ValidationStateChanged</remarks> /// <remarks>If you override this, you MUST fire ErrorsChanged as appropriate, and call ValidationStateChanged</remarks>
protected virtual async Task<bool> ValidateAsync() protected virtual async Task<bool> ValidateAsync()
{ {
if (this.validator == null) if (this.Validator == null)
throw new InvalidOperationException("Can't run validation if a validator hasn't been set"); throw new InvalidOperationException("Can't run validation if a validator hasn't been set");
bool anyChanged = false; bool anyChanged = false;
@ -107,7 +107,7 @@ namespace Stylet
// However this means that the stuff after the await can be run in parallel on multiple threads // However this means that the stuff after the await can be run in parallel on multiple threads
// Therefore, we need the lock // Therefore, we need the lock
// However, we can't raise PropertyChanged events from within the lock, otherwise deadlock // However, we can't raise PropertyChanged events from within the lock, otherwise deadlock
var results = await this.validator.ValidateAllPropertiesAsync().ConfigureAwait(false); var results = await this.Validator.ValidateAllPropertiesAsync().ConfigureAwait(false);
var changedProperties = new List<string>(); var changedProperties = new List<string>();
await this.propertyErrorsLock.WaitAsync().ConfigureAwait(false); await this.propertyErrorsLock.WaitAsync().ConfigureAwait(false);
{ {
@ -185,12 +185,12 @@ namespace Stylet
/// <remarks>If you override this, you MUST fire ErrorsChange and call OnValidationStateChanged() if appropriate</remarks> /// <remarks>If you override this, you MUST fire ErrorsChange and call OnValidationStateChanged() if appropriate</remarks>
protected virtual async Task<bool> ValidatePropertyAsync([CallerMemberName] string propertyName = null) protected virtual async Task<bool> ValidatePropertyAsync([CallerMemberName] string propertyName = null)
{ {
if (this.validator == null) if (this.Validator == null)
throw new InvalidOperationException("Can't run validation if a validator hasn't been set"); throw new InvalidOperationException("Can't run validation if a validator hasn't been set");
// To allow synchronous calling of this method, we need to resume on the ThreadPool. // To allow synchronous calling of this method, we need to resume on the ThreadPool.
// Therefore, we might resume on any thread, hence the need for a lock // Therefore, we might resume on any thread, hence the need for a lock
var newErrors = await this.validator.ValidatePropertyAsync(propertyName).ConfigureAwait(false); var newErrors = await this.Validator.ValidatePropertyAsync(propertyName).ConfigureAwait(false);
bool propertyErrorsChanged = false; bool propertyErrorsChanged = false;
await this.propertyErrorsLock.WaitAsync().ConfigureAwait(false); await this.propertyErrorsLock.WaitAsync().ConfigureAwait(false);
@ -222,7 +222,7 @@ namespace Stylet
// Save ourselves a little bit of work every time HasErrors is fired as the result of // Save ourselves a little bit of work every time HasErrors is fired as the result of
// the validation results changing. // the validation results changing.
if (this.validator != null && this.AutoValidate && propertyName != "HasErrors") if (this.Validator != null && this.AutoValidate && propertyName != "HasErrors")
await this.ValidatePropertyAsync(propertyName); await this.ValidatePropertyAsync(propertyName);
} }

View File

@ -8,7 +8,7 @@ namespace Stylet.Xaml
/// </summary> /// </summary>
public class ApplicationLoader : ResourceDictionary public class ApplicationLoader : ResourceDictionary
{ {
private ResourceDictionary styletResourceDictionary; private readonly ResourceDictionary styletResourceDictionary;
/// <summary> /// <summary>
/// Create a new ApplicationLoader instance /// Create a new ApplicationLoader instance

View File

@ -41,8 +41,8 @@ namespace Stylet.Xaml
private object target; private object target;
private ActionUnavailableBehaviour targetNullBehaviour; private readonly ActionUnavailableBehaviour targetNullBehaviour;
private ActionUnavailableBehaviour actionNonExistentBehaviour; private readonly ActionUnavailableBehaviour actionNonExistentBehaviour;
/// <summary> /// <summary>
/// Create a new ActionCommand /// Create a new ActionCommand

View File

@ -35,8 +35,8 @@ namespace Stylet.Xaml
private object target; private object target;
private ActionUnavailableBehaviour targetNullBehaviour; private readonly ActionUnavailableBehaviour targetNullBehaviour;
private ActionUnavailableBehaviour actionNonExistentBehaviour; private readonly ActionUnavailableBehaviour actionNonExistentBehaviour;
/// <summary> /// <summary>
/// Create a new EventAction /// Create a new EventAction

View File

@ -15,7 +15,7 @@ namespace Stylet.Xaml
/// <summary> /// <summary>
/// Singleton instance of this converter. Usage e.g. Converter="{x:Static s:IconToBitmapSourceConverter.Instance}" /// Singleton instance of this converter. Usage e.g. Converter="{x:Static s:IconToBitmapSourceConverter.Instance}"
/// </summary> /// </summary>
public static IconToBitmapSourceConverter Instance = new IconToBitmapSourceConverter(); public static readonly IconToBitmapSourceConverter Instance = new IconToBitmapSourceConverter();
/// <summary> /// <summary>
/// Converts a value /// Converts a value

View File

@ -17,10 +17,10 @@ namespace StyletUnitTests
{ {
private class MyScreen : Screen private class MyScreen : Screen
{ {
public IModelValidator Validator public new IModelValidator Validator
{ {
get { return base.validator; } get { return base.Validator; }
set { base.validator = value; } set { base.Validator = value; }
} }
public MyScreen() { } public MyScreen() { }

View File

@ -31,10 +31,10 @@ namespace StyletUnitTests
set { base.AutoValidate = value; } set { base.AutoValidate = value; }
} }
public IModelValidator Validator public new IModelValidator Validator
{ {
get { return this.validator; } get { return base.Validator; }
set { this.validator = value; } set { base.Validator = value; }
} }
public new bool Validate() public new bool Validate()