├── README.md ├── AsyncValidation ├── AsyncValidation.Demo │ ├── App.config │ ├── Properties │ │ ├── Settings.settings │ │ ├── Settings.Designer.cs │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ └── Resources.resx │ ├── MainWindow.xaml.cs │ ├── App.xaml.cs │ ├── App.xaml │ ├── Styles.xaml │ ├── MainWindow.xaml │ ├── DemoViewModel.cs │ └── AsyncValidation.Demo.csproj ├── AsyncValidation.Tests │ ├── packages.config │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── AsyncValidation.Tests.csproj │ └── ValidatableViewModelTests.cs ├── AsyncValidation │ ├── PropertyHelper.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── AsyncValidation.csproj │ └── ValidatableViewModel.cs └── AsyncValidation.sln ├── LICENSE └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # AsyncValidation 2 | Asynchronous Validation for WPF MVVM. 3 | 4 | Code for the acticle 5 | https://www.codeproject.com/Articles/1167558/Async-Validation-in-WPF 6 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Tests/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | namespace AsyncValidation.Demo 4 | { 5 | /// 6 | /// Interaction logic for MainWindow.xaml 7 | /// 8 | public partial class MainWindow : Window 9 | { 10 | public MainWindow() 11 | { 12 | InitializeComponent(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | namespace AsyncValidation.Demo 4 | { 5 | /// 6 | /// Interaction logic for App.xaml 7 | /// 8 | public partial class App : Application 9 | { 10 | private void App_OnStartup(object sender, StartupEventArgs e) 11 | { 12 | var mainWindow = new MainWindow(); 13 | mainWindow.Show(); 14 | mainWindow.DataContext = new DemoViewModel(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/App.xaml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation/PropertyHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace AsyncValidation 5 | { 6 | public class PropertyHelper 7 | { 8 | public static string GetPropertyName(Expression> propertyLambda) 9 | { 10 | var me = propertyLambda.Body as MemberExpression; 11 | 12 | if (me == null) 13 | { 14 | throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'"); 15 | } 16 | 17 | return me.Member.Name; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Valerii Tereshchenko 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace AsyncValidation.Demo.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("AsyncValidation")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("AsyncValidation")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("132f61db-c6f3-4214-953f-455fa2521a99")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("AsyncValidation.Tests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("AsyncValidation.Tests")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("270d9bf7-7131-4b34-910d-9fa032e85d0e")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/Styles.xaml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | 24 | 25 | 36 | 37 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncValidation", "AsyncValidation\AsyncValidation.csproj", "{13C23B3D-399D-464D-9C68-23514219E6DD}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2A692F55-669E-4C2E-99DA-C37F11EBB351}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncValidation.Tests", "AsyncValidation.Tests\AsyncValidation.Tests.csproj", "{270D9BF7-7131-4B34-910D-9FA032E85D0E}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncValidation.Demo", "AsyncValidation.Demo\AsyncValidation.Demo.csproj", "{F82EDB46-6D87-460F-BA38-EBDF01FBF8F4}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {13C23B3D-399D-464D-9C68-23514219E6DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {13C23B3D-399D-464D-9C68-23514219E6DD}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {13C23B3D-399D-464D-9C68-23514219E6DD}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {13C23B3D-399D-464D-9C68-23514219E6DD}.Release|Any CPU.Build.0 = Release|Any CPU 24 | {270D9BF7-7131-4B34-910D-9FA032E85D0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {270D9BF7-7131-4B34-910D-9FA032E85D0E}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {270D9BF7-7131-4B34-910D-9FA032E85D0E}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {270D9BF7-7131-4B34-910D-9FA032E85D0E}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {F82EDB46-6D87-460F-BA38-EBDF01FBF8F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {F82EDB46-6D87-460F-BA38-EBDF01FBF8F4}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {F82EDB46-6D87-460F-BA38-EBDF01FBF8F4}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {F82EDB46-6D87-460F-BA38-EBDF01FBF8F4}.Release|Any CPU.Build.0 = Release|Any CPU 32 | EndGlobalSection 33 | GlobalSection(SolutionProperties) = preSolution 34 | HideSolutionNode = FALSE 35 | EndGlobalSection 36 | EndGlobal 37 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // General Information about an assembly is controlled through the following 8 | // set of attributes. Change these attribute values to modify the information 9 | // associated with an assembly. 10 | [assembly: AssemblyTitle("AsyncValidation.Demo")] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct("AsyncValidation.Demo")] 15 | [assembly: AssemblyCopyright("Copyright © 2017")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | // Setting ComVisible to false makes the types in this assembly not visible 20 | // to COM components. If you need to access a type in this assembly from 21 | // COM, set the ComVisible attribute to true on that type. 22 | [assembly: ComVisible(false)] 23 | 24 | //In order to begin building localizable applications, set 25 | //CultureYouAreCodingWith in your .csproj file 26 | //inside a . For example, if you are using US english 27 | //in your source files, set the to en-US. Then uncomment 28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in 29 | //the line below to match the UICulture setting in the project file. 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly: ThemeInfo( 35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 36 | //(used if a resource is not found in the page, 37 | // or application resource dictionaries) 38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 39 | //(used if a resource is not found in the page, 40 | // app, or any theme specific resource dictionaries) 41 | )] 42 | 43 | 44 | // Version information for an assembly consists of the following four values: 45 | // 46 | // Major Version 47 | // Minor Version 48 | // Build Number 49 | // Revision 50 | // 51 | // You can specify all the values or you can default the Build and Revision Numbers 52 | // by using the '*' as shown below: 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion("1.0.0.0")] 55 | [assembly: AssemblyFileVersion("1.0.0.0")] 56 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation/AsyncValidation.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {13C23B3D-399D-464D-9C68-23514219E6DD} 8 | Library 9 | Properties 10 | AsyncValidation 11 | AsyncValidation 12 | v4.5 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 57 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/MainWindow.xaml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 58 | 59 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/DemoViewModel.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | 4 | namespace AsyncValidation.Demo 5 | { 6 | class DemoViewModel : ValidatableViewModel 7 | { 8 | private string _name; 9 | public string Name 10 | { 11 | get 12 | { 13 | return _name; 14 | } 15 | 16 | set 17 | { 18 | Set(ref _name, value); 19 | } 20 | } 21 | 22 | private string _description; 23 | public string Description 24 | { 25 | get 26 | { 27 | return _description; 28 | } 29 | 30 | set 31 | { 32 | Set(ref _description, value); 33 | } 34 | } 35 | 36 | private int _number; 37 | public int Number 38 | { 39 | get 40 | { 41 | return _number; 42 | } 43 | 44 | set 45 | { 46 | Set(ref _number, value); 47 | } 48 | } 49 | 50 | public List AvailableNumbers => new List(new[] { 1, 2, 3, 5, 7, 11 }); 51 | 52 | public DemoViewModel() 53 | { 54 | RegisterValidator(() => Name, ValidateName); 55 | RegisterValidator(() => Description, ValidateDescription); 56 | RegisterValidator(() => Number, ValidateNumber); 57 | ValidateAll(); 58 | } 59 | 60 | private async Task> ValidateName() 61 | { 62 | await Task.Delay(3000); 63 | 64 | if (string.IsNullOrWhiteSpace(Name)) 65 | { 66 | return new List { "Name cannot be empty" }; 67 | } 68 | 69 | if (Name.Length > 10) 70 | { 71 | return new List { "Name cannot be more than 10 characters" }; 72 | } 73 | 74 | return new List(); 75 | } 76 | 77 | private async Task> ValidateDescription() 78 | { 79 | await Task.Delay(4000); 80 | 81 | if (string.IsNullOrWhiteSpace(Description)) 82 | { 83 | return new List { "Description cannot be empty" }; 84 | } 85 | 86 | if (Description.Length > 50) 87 | { 88 | return new List { "Name cannot be more than 50 characters" }; 89 | } 90 | 91 | return new List(); 92 | } 93 | 94 | private async Task> ValidateNumber() 95 | { 96 | await Task.Delay(2000); 97 | 98 | if (Number > 5) 99 | { 100 | return new List { "Name cannot be more than 5" }; 101 | } 102 | 103 | return new List(); 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace AsyncValidation.Demo.Properties 12 | { 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// Returns the cached ResourceManager instance used by this class. 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("AsyncValidation.Demo.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// Overrides the current thread's CurrentUICulture property for all 56 | /// resource lookups using this strongly typed resource class. 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Tests/AsyncValidation.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {270D9BF7-7131-4B34-910D-9FA032E85D0E} 8 | Library 9 | Properties 10 | AsyncValidation.Tests 11 | AsyncValidation.Tests 12 | v4.6.1 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | ..\packages\NSubstitute.1.10.0.0\lib\net45\NSubstitute.dll 35 | True 36 | 37 | 38 | ..\packages\NUnit.3.5.0\lib\net45\nunit.framework.dll 39 | True 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | {13c23b3d-399d-464d-9c68-23514219e6dd} 61 | AsyncValidation 62 | 63 | 64 | 65 | 72 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/AsyncValidation.Demo.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F82EDB46-6D87-460F-BA38-EBDF01FBF8F4} 8 | WinExe 9 | Properties 10 | AsyncValidation.Demo 11 | AsyncValidation.Demo 12 | v4.6.1 13 | 512 14 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15 | 4 16 | true 17 | 18 | 19 | AnyCPU 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | 28 | 29 | AnyCPU 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 4.0 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | MSBuild:Compile 56 | Designer 57 | 58 | 59 | MSBuild:Compile 60 | Designer 61 | 62 | 63 | App.xaml 64 | Code 65 | 66 | 67 | 68 | MainWindow.xaml 69 | Code 70 | 71 | 72 | Designer 73 | MSBuild:Compile 74 | 75 | 76 | 77 | 78 | Code 79 | 80 | 81 | True 82 | True 83 | Resources.resx 84 | 85 | 86 | True 87 | Settings.settings 88 | True 89 | 90 | 91 | ResXFileCodeGenerator 92 | Resources.Designer.cs 93 | 94 | 95 | SettingsSingleFileGenerator 96 | Settings.Designer.cs 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | {13C23B3D-399D-464D-9C68-23514219E6DD} 106 | AsyncValidation 107 | 108 | 109 | 110 | 117 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation/ValidatableViewModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.Linq; 6 | using System.Linq.Expressions; 7 | using System.Runtime.CompilerServices; 8 | using System.Threading.Tasks; 9 | 10 | namespace AsyncValidation 11 | { 12 | public abstract class ValidatableViewModel : INotifyDataErrorInfo, INotifyPropertyChanged 13 | { 14 | private bool _isValidating; 15 | public bool IsValidating 16 | { 17 | get { return _isValidating; } 18 | protected set { Set(ref _isValidating, value); } 19 | } 20 | 21 | private bool _isValid = true; 22 | public bool IsValid 23 | { 24 | get { return _isValid; } 25 | protected set { Set(ref _isValid, value); } 26 | } 27 | 28 | private readonly Dictionary> _validationErrors = new Dictionary>(); 29 | private readonly Dictionary _lastValidationProcesses = new Dictionary(); 30 | private readonly Dictionary>>> _validators = new Dictionary>>>(); 31 | 32 | protected ValidatableViewModel() 33 | { 34 | PropertyChanged += (sender, args) => Validate(args.PropertyName); 35 | } 36 | 37 | #region INotifyDataErrorInfo 38 | public event EventHandler ErrorsChanged; 39 | public IEnumerable GetErrors(string propertyName) 40 | { 41 | if (string.IsNullOrEmpty(propertyName) || !_validationErrors.ContainsKey(propertyName)) 42 | { 43 | return new List(); 44 | } 45 | 46 | return _validationErrors[propertyName]; 47 | } 48 | 49 | public bool HasErrors => _validationErrors.Count > 0; 50 | #endregion 51 | 52 | #region INotifyPropertyChanged 53 | public event PropertyChangedEventHandler PropertyChanged; 54 | #endregion 55 | 56 | public List GetErrors() 57 | { 58 | return _validationErrors.SelectMany(p => p.Value).ToList(); 59 | } 60 | 61 | protected void Set(ref T storage, T value, [CallerMemberName] string property = null) 62 | { 63 | if (Equals(storage, value)) 64 | { 65 | return; 66 | } 67 | 68 | storage = value; 69 | RaisePropertyChanged(property); 70 | } 71 | 72 | protected void RaisePropertyChanged(string property) 73 | { 74 | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); 75 | } 76 | 77 | protected void RegisterValidator(Expression> propertyExpression, Func>> validatorFunc) 78 | { 79 | RegisterValidator(PropertyHelper.GetPropertyName(propertyExpression), validatorFunc); 80 | } 81 | 82 | protected void RegisterValidator(string propertyName, Func>> validatorFunc) 83 | { 84 | if (_validators.ContainsKey(propertyName)) 85 | { 86 | _validators.Remove(propertyName); 87 | } 88 | 89 | _validators[propertyName] = validatorFunc; 90 | } 91 | 92 | protected async Task Validate(string property) 93 | { 94 | if (string.IsNullOrWhiteSpace(property)) 95 | { 96 | throw new ArgumentException(); 97 | } 98 | 99 | Func>> validator; 100 | if (!_validators.TryGetValue(property, out validator)) 101 | { 102 | return; 103 | } 104 | 105 | var validationProcessKey = Guid.NewGuid(); 106 | _lastValidationProcesses[property] = validationProcessKey; 107 | IsValidating = true; 108 | try 109 | { 110 | var errors = await validator(); 111 | if (_lastValidationProcesses.ContainsKey(property) && 112 | _lastValidationProcesses[property] == validationProcessKey) 113 | { 114 | if (errors != null && errors.Any()) 115 | { 116 | _validationErrors[property] = errors; 117 | } 118 | else if (_validationErrors.ContainsKey(property)) 119 | { 120 | _validationErrors.Remove(property); 121 | } 122 | } 123 | } 124 | catch (Exception ex) 125 | { 126 | _validationErrors[property] = new List(new[] { ex.Message }); 127 | } 128 | finally 129 | { 130 | if (_lastValidationProcesses.ContainsKey(property) && 131 | _lastValidationProcesses[property] == validationProcessKey) 132 | { 133 | _lastValidationProcesses.Remove(property); 134 | } 135 | 136 | IsValidating = _lastValidationProcesses.Any(); 137 | IsValid = !_lastValidationProcesses.Any() && !_validationErrors.Any(); 138 | OnErrorsChanged(property); 139 | } 140 | } 141 | 142 | protected async Task ValidateAll() 143 | { 144 | var validators = _validators; 145 | foreach (var propertyName in validators.Keys) 146 | { 147 | await Validate(propertyName); 148 | } 149 | } 150 | 151 | private void OnErrorsChanged(string propertyName) 152 | { 153 | ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); 154 | } 155 | } 156 | } -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Demo/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /AsyncValidation/AsyncValidation.Tests/ValidatableViewModelTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Linq.Expressions; 5 | using System.Threading.Tasks; 6 | using NSubstitute; 7 | using NUnit.Framework; 8 | 9 | namespace AsyncValidation.Tests 10 | { 11 | [TestFixture] 12 | public class ValidatableViewModelTests 13 | { 14 | private class ValidatableViewModelStub : ValidatableViewModel 15 | { 16 | private string _propertyToValidate1; 17 | 18 | public string PropertyToValidate1 19 | { 20 | get { return _propertyToValidate1; } 21 | set { Set(ref _propertyToValidate1, value); } 22 | } 23 | 24 | private string _propertyToValidate2; 25 | 26 | public string PropertyToValidate2 27 | { 28 | get { return _propertyToValidate2; } 29 | set { Set(ref _propertyToValidate2, value); } 30 | } 31 | 32 | 33 | public new void RegisterValidator(Expression> propertyExpression, 34 | Func>> validatorFunc) => base.RegisterValidator(propertyExpression, validatorFunc); 35 | 36 | public new Task ValidateAll() => base.ValidateAll(); 37 | 38 | public new Task Validate(string property) => base.Validate(property); 39 | } 40 | 41 | private readonly string _prop1Error1 = "Property 1 Error 1"; 42 | private readonly string _prop2Error1 = "Property 2 Error 1"; 43 | private readonly string _prop2Error2 = "Property 2 Error 2"; 44 | 45 | [Test] 46 | public void GetErrorsWhenNoErrors() 47 | { 48 | var viewModel = CreateTestViewModel(new List(), new List()); 49 | 50 | viewModel.PropertyToValidate1 = "Test"; 51 | viewModel.PropertyToValidate2 = "Test"; 52 | var errors = viewModel.GetErrors(); 53 | 54 | Assert.IsFalse(viewModel.HasErrors); 55 | Assert.IsTrue(viewModel.IsValid); 56 | Assert.IsFalse(viewModel.IsValidating); 57 | Assert.AreEqual(0, errors.Count); 58 | } 59 | 60 | [Test] 61 | public void GetErrorsWhenOnePropertyHasError() 62 | { 63 | var viewModel = CreateTestViewModel(new List { _prop1Error1 }, new List()); 64 | 65 | viewModel.PropertyToValidate1 = "Test"; 66 | var errors = viewModel.GetErrors(); 67 | var prop1Errors = viewModel.GetErrors("PropertyToValidate1").Cast().ToList(); 68 | 69 | Assert.IsTrue(viewModel.HasErrors); 70 | Assert.IsFalse(viewModel.IsValid); 71 | Assert.IsFalse(viewModel.IsValidating); 72 | Assert.AreEqual(1, errors.Count); 73 | CollectionAssert.Contains(errors, _prop1Error1); 74 | Assert.AreEqual(1, prop1Errors.Count); 75 | CollectionAssert.Contains(prop1Errors, _prop1Error1); 76 | } 77 | 78 | [Test] 79 | public void GetErrorsWhenTwoPropertiesHaveErrors() 80 | { 81 | var viewModel = CreateTestViewModel( 82 | new List { _prop1Error1 }, 83 | new List { _prop2Error1 }); 84 | 85 | viewModel.PropertyToValidate1 = _prop1Error1; 86 | viewModel.PropertyToValidate2 = _prop2Error1; 87 | var errors = viewModel.GetErrors(); 88 | 89 | var prop1Errors = viewModel.GetErrors("PropertyToValidate1").Cast().ToList(); 90 | var prop2Errors = viewModel.GetErrors("PropertyToValidate2").Cast().ToList(); 91 | Assert.AreEqual(2, errors.Count); 92 | Assert.AreEqual(1, prop1Errors.Count); 93 | Assert.AreEqual(1, prop2Errors.Count); 94 | } 95 | 96 | [Test] 97 | public void GetErrorsWhenTwoPropertiesHaveErrorsButOnlyOneWasValidated() 98 | { 99 | var viewModel = CreateTestViewModel(new List { _prop1Error1 }, 100 | new List { _prop2Error1, _prop2Error2 }); 101 | 102 | viewModel.PropertyToValidate2 = "Test"; 103 | var errors = viewModel.GetErrors(); 104 | var prop2Errors = viewModel.GetErrors("PropertyToValidate2").Cast().ToList(); 105 | 106 | Assert.IsTrue(viewModel.HasErrors); 107 | Assert.IsFalse(viewModel.IsValid); 108 | Assert.IsFalse(viewModel.IsValidating); 109 | Assert.AreEqual(2, errors.Count); 110 | CollectionAssert.Contains(errors, _prop2Error1); 111 | CollectionAssert.Contains(errors, _prop2Error2); 112 | Assert.AreEqual(2, prop2Errors.Count); 113 | CollectionAssert.Contains(prop2Errors, _prop2Error1); 114 | CollectionAssert.Contains(prop2Errors, _prop2Error1); 115 | } 116 | 117 | [Test] 118 | public void GetErrorsWhenValidatorException() 119 | { 120 | var viewModel = CreateTestViewModel(new List { _prop1Error1 }, new List()); 121 | var validatorProp1Mock = Substitute.For>>>(); 122 | validatorProp1Mock.Invoke().Returns(Task.FromException>(new Exception("Exception Message"))); 123 | viewModel.RegisterValidator(() => viewModel.PropertyToValidate1, validatorProp1Mock); 124 | 125 | viewModel.PropertyToValidate1 = "Test"; 126 | var errors = viewModel.GetErrors(); 127 | var prop1Errors = viewModel.GetErrors("PropertyToValidate1").Cast().ToList(); 128 | 129 | Assert.IsTrue(viewModel.HasErrors); 130 | Assert.IsFalse(viewModel.IsValid); 131 | Assert.IsFalse(viewModel.IsValidating); 132 | Assert.AreEqual(1, errors.Count); 133 | Assert.AreEqual("Exception Message", errors[0]); 134 | Assert.AreEqual(1, prop1Errors.Count); 135 | CollectionAssert.DoesNotContain(errors, _prop1Error1); 136 | Assert.AreEqual("Exception Message", prop1Errors[0]); 137 | } 138 | 139 | [TestCase(null)] 140 | [TestCase("")] 141 | [TestCase(" ")] 142 | [TestCase("UnexistingProperty")] 143 | public void GetErrorsWhenWrongPropertyName(string propertyName) 144 | { 145 | var viewModel = CreateTestViewModel(new List { _prop1Error1 }, 146 | new List { _prop2Error1, _prop2Error2 }); 147 | 148 | var errors = viewModel.GetErrors(propertyName); 149 | 150 | CollectionAssert.IsEmpty(errors); 151 | } 152 | 153 | [Test] 154 | public void UseLastRegisteredValidator() 155 | { 156 | var viewModel = CreateTestViewModel(new List { _prop1Error1 }, new List()); 157 | viewModel.PropertyToValidate1 = "Test"; 158 | viewModel.RegisterValidator(() => viewModel.PropertyToValidate1, () => Task.FromResult(new List())); 159 | viewModel.PropertyToValidate1 = "Test 2"; 160 | 161 | var errors = viewModel.GetErrors(); 162 | var prop1Errors = viewModel.GetErrors("PropertyToValidate1"); 163 | 164 | Assert.IsFalse(viewModel.HasErrors); 165 | Assert.IsTrue(viewModel.IsValid); 166 | Assert.IsFalse(viewModel.IsValidating); 167 | CollectionAssert.IsEmpty(errors); 168 | CollectionAssert.IsEmpty(prop1Errors); 169 | } 170 | 171 | [Test] 172 | public void ValidateAll() 173 | { 174 | var viewModel = CreateTestViewModel(new List { _prop1Error1 }, 175 | new List { _prop2Error1, _prop2Error2 }); 176 | 177 | viewModel.ValidateAll(); 178 | var errors = viewModel.GetErrors(); 179 | var prop1Errors = viewModel.GetErrors("PropertyToValidate1").Cast().ToList(); 180 | var prop2Errors = viewModel.GetErrors("PropertyToValidate2").Cast().ToList(); 181 | 182 | Assert.IsTrue(viewModel.HasErrors); 183 | Assert.IsFalse(viewModel.IsValid); 184 | Assert.IsFalse(viewModel.IsValidating); 185 | Assert.AreEqual(3, errors.Count); 186 | CollectionAssert.Contains(errors, _prop1Error1); 187 | CollectionAssert.Contains(errors, _prop2Error1); 188 | CollectionAssert.Contains(errors, _prop2Error2); 189 | Assert.AreEqual(1, prop1Errors.Count); 190 | CollectionAssert.Contains(prop1Errors, _prop1Error1); 191 | Assert.AreEqual(2, prop2Errors.Count); 192 | CollectionAssert.Contains(prop2Errors, _prop2Error1); 193 | CollectionAssert.Contains(prop2Errors, _prop2Error1); 194 | } 195 | 196 | [TestCase(null)] 197 | [TestCase("")] 198 | [TestCase(" ")] 199 | public void ValidateWhenEmptyProperty(string propertyName) 200 | { 201 | var viewModel = CreateTestViewModel(new List { _prop1Error1 }, 202 | new List { _prop2Error1, _prop2Error2 }); 203 | 204 | Assert.That(() => viewModel.Validate(propertyName), Throws.TypeOf()); 205 | } 206 | 207 | [Test] 208 | public void IgnorePreviousValidationResult() 209 | { 210 | var viewModel = new ValidatableViewModelStub(); 211 | var isFirstCall = true; 212 | var task = Task.Run(async () => 213 | { 214 | await Task.Delay(1000); 215 | 216 | return new List { "First Error!!!!" }; 217 | }); 218 | viewModel.RegisterValidator(() => viewModel.PropertyToValidate1, () => 219 | { 220 | if (isFirstCall) 221 | { 222 | isFirstCall = false; 223 | 224 | return task; 225 | } 226 | 227 | return Task.FromResult(new List { "Second Error!!!!" }); 228 | }); 229 | 230 | viewModel.Validate("PropertyToValidate1"); 231 | viewModel.Validate("PropertyToValidate1"); 232 | task.Wait(); 233 | var errors = viewModel.GetErrors(); 234 | 235 | Assert.AreEqual("Second Error!!!!", errors[0]); 236 | } 237 | 238 | private ValidatableViewModelStub CreateTestViewModel(List property1Errors, List property2Errors) 239 | { 240 | var viewModel = new ValidatableViewModelStub(); 241 | viewModel.RegisterValidator(() => viewModel.PropertyToValidate1, () => Task.FromResult(property1Errors)); 242 | viewModel.RegisterValidator(() => viewModel.PropertyToValidate2, () => Task.FromResult(property2Errors)); 243 | 244 | return viewModel; 245 | } 246 | } 247 | } --------------------------------------------------------------------------------