From 2d44237ef495b4e2a0e72c7e58d9c01cf04dba3c Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 2 Aug 2014 17:59:22 +0100 Subject: [PATCH] Change Stringable to a struct, to avoid nullity issues --- .../Pages/UserViewModel.cs | 4 ++-- .../Stylet.Samples.ModelValidation/Stringable.cs | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs b/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs index 4727945..789ab14 100644 --- a/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs +++ b/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs @@ -58,8 +58,8 @@ namespace Stylet.Samples.ModelValidation.Pages RuleFor(x => x.Email).NotEmpty().EmailAddress(); RuleFor(x => x.Password).NotEmpty().Matches("[0-9]").WithMessage("{PropertyName} must contain a number"); RuleFor(x => x.PasswordConfirmation).Equal(s => s.Password).WithMessage("{PropertyName} should match Password"); - RuleFor(x => x.Age).Must(x => x != null && x.IsValid).WithMessage("{PropertyName} must be a valid number"); - When(x => x.Age != null && x.Age.IsValid, () => + RuleFor(x => x.Age).Must(x => x.IsValid).WithMessage("{PropertyName} must be a valid number"); + When(x => x.Age.IsValid, () => { RuleFor(x => x.Age.Value).GreaterThan(0).WithName("Age"); }); diff --git a/Samples/Stylet.Samples.ModelValidation/Stringable.cs b/Samples/Stylet.Samples.ModelValidation/Stringable.cs index 55aaa39..21ccdef 100644 --- a/Samples/Stylet.Samples.ModelValidation/Stringable.cs +++ b/Samples/Stylet.Samples.ModelValidation/Stringable.cs @@ -20,9 +20,9 @@ namespace Stylet.Samples.ModelValidation /// /// This type has an associated TypeConverter, StringableConverter, which will be used by WPF to convert this Stringable{T} to and from a string. /// - // I would make this a struct, but it will have to be boxed to go through the TypeConverter, so there's no point + // If this is a struct, we avoid null issues [TypeConverter(typeof(StringableConverter))] - public class Stringable : IEquatable> + public struct Stringable : IEquatable> { private readonly string _stringValue; @@ -104,20 +104,18 @@ namespace Stylet.Samples.ModelValidation public override bool Equals(object obj) { - return base.Equals(obj as Stringable); + if (!(obj is Stringable)) + return false; + return base.Equals((Stringable)obj); } public bool Equals(Stringable other) { - return other != null && EqualityComparer.Default.Equals(this.Value, other.Value) && this.StringValue == other.StringValue; + return EqualityComparer.Default.Equals(this.Value, other.Value) && this.StringValue == other.StringValue; } public static bool operator ==(Stringable o1, Stringable o2) { - if (Object.ReferenceEquals(o1, o2)) - return true; - if ((object)o1 == null || (object)o2 == null) - return false; return o1.Equals(o2); } @@ -161,7 +159,7 @@ namespace Stylet.Samples.ModelValidation // WPF instantiates us once, then uses us lots, so the overhead of doing this here is worth it var fromMethod = type.GetMethod("FromString", BindingFlags.Static | BindingFlags.Public); var param = Expression.Parameter(typeof(string)); - this.generator = Expression.Lambda>(Expression.Call(fromMethod, param), param).Compile(); + this.generator = Expression.Lambda>(Expression.TypeAs(Expression.Call(fromMethod, param), typeof(object)), param).Compile(); } public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)