{"id":1293,"date":"2013-09-11T06:40:22","date_gmt":"2013-09-11T05:40:22","guid":{"rendered":"http:\/\/www.minddriven.de\/?p=1293"},"modified":"2013-09-12T13:20:51","modified_gmt":"2013-09-12T12:20:51","slug":"async-validation-attributes","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/windows-8\/winrt\/async-validation-attributes","title":{"rendered":"WinRT XAML Validation library: Async Validation Attributes"},"content":{"rendered":"<p>Last posts have shown how basic validation attributes and property-group based validation attributes can be used to define custom validation logic, that goes beyond the default capabilities of Data Annotations. There&#8217;s another validation attribute base class in the WinRT XAML Validation library and that&#8217;s the <code>AsyncValidationAttribute<\/code> and its sister, the <code>AsyncGroupValidationAttribute<\/code>.<\/p>\n<p>Both attributes define the abstract method <code>IsValidAsync()<\/code>, which you can use in your derived validation attribute classes to perform asynchronous operations during validation. The following code gives an example for the <code>AsyncValidationAttribute<\/code> class:<\/p>\n<pre class=\"brush:csharp\">[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]\r\npublic sealed class ValidateActualCurrentBidAttribute : AsyncValidationAttribute\r\n{\r\n    protected override async Task&lt;ValidationResult&gt; IsValidAsync(object value, ValidationContext validationContext)\r\n    {\r\n        var newBid = (double)value;\r\n        var auctionBid = (AuctionBid)validationContext.ObjectInstance;\r\n            \r\n        var service = new AuctionService();\r\n        var currentBid = await service.GetCurrentBidAsync(auctionBid);\r\n\r\n        return (newBid &gt; auctionBid.CurrentBid && newBid &lt;= currentBid)\r\n            ? new ValidationResult(\"Your bid has been surpassed.\")\r\n            : ValidationResult.Success;\r\n    }\r\n}<\/pre>\n<p>This sample async validation attribute calls the async service method <code>AuctionService.GetCurrentBidAsync()<\/code> to retrieve actual data from a server. It gets await&#8217;ed and returns the validation result after the async operations has returned. Thus you&#8217;re able to define custom validation logic in our asynchronous app world.<\/p>\n<p><code>AsyncGroupValidationAttribute<\/code> is a specialization of <code>AsyncValidationAttribute<\/code>, its usage doesn&#8217;t differ, so I will not show it here. The usage of an async validation attribute equals the usage of any other attribute on your entity and you&#8217;re able to use the functionality of the ExtendedValidationAttribute.<\/p>\n<pre class=\"brush:csharp;\">public class AuctionBid : ValidationBindableBase\r\n{\r\n    [ValidateActualCurrentBid(UseInImplicitValidation = false)]\r\n    public double NewBid { ... }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Last posts have shown how basic validation attributes and property-group based validation attributes can be used to define custom validation logic, that goes beyond the default capabilities of Data Annotations. There&#8217;s another validation attribute base class in the WinRT XAML Validation library and that&#8217;s the AsyncValidationAttribute and its sister, the AsyncGroupValidationAttribute. Both attributes define the &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/windows-8\/winrt\/async-validation-attributes\" class=\"more-link\"><span class=\"screen-reader-text\">WinRT XAML Validation library: Async Validation Attributes<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,300],"tags":[309,310,308,143,322,76],"class_list":["post-1293","post","type-post","status-publish","format-standard","hentry","category-windows-8","category-winrt","tag-dot-net","tag-c-sharp","tag-user-input","tag-validation","tag-winrt","tag-xaml"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/comments?post=1293"}],"version-history":[{"count":5,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1293\/revisions"}],"predecessor-version":[{"id":1308,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1293\/revisions\/1308"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=1293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=1293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=1293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}