<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Berke Sökhan - MVVM</title>
    <link>http://www.berkesokhan.com/blog/</link>
    <description>düşünceler / thoughts</description>
    <language>en-us</language>
    <copyright>Berke Sökhan</copyright>
    <lastBuildDate>Sun, 06 Dec 2009 09:20:24 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>berkesokhan@gmail.com</managingEditor>
    <webMaster>berkesokhan@gmail.com</webMaster>
    <item>
      <trackback:ping>http://www.berkesokhan.com/blog/Trackback.aspx?guid=584899a5-488b-4202-ad93-2b1ab6c85de6</trackback:ping>
      <pingback:server>http://www.berkesokhan.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.berkesokhan.com/blog/PermaLink,guid,584899a5-488b-4202-ad93-2b1ab6c85de6.aspx</pingback:target>
      <dc:creator>Berke Sökhan</dc:creator>
      <wfw:comment>http://www.berkesokhan.com/blog/CommentView,guid,584899a5-488b-4202-ad93-2b1ab6c85de6.aspx</wfw:comment>
      <wfw:commentRss>http://www.berkesokhan.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=584899a5-488b-4202-ad93-2b1ab6c85de6</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">We recently started to build a desktop
application with WPF and MVVM. 
<br /><br />
When we had the need for a validation framework for our view inputs, I started to
look on internet to find "de facto" approach for MVVM and WPF in general. There are
lots of look-alike methods for validation. 
<br /><br />
For validation I commonly saw three approaches:<br /><br /><ul><li>
Validation Rules on views<br /></li><li>
Hand coded validation on ViewModel setters</li><li>
Using some validation framework attributes (mostly System.ComponentModel.DataAnnotations)
on binded Model properties or ViewModel properties.<br /></li></ul><br />
For validation error user notification, people complain about how WPF lacks old Windows
Forms built in Error Provider support and invent their custom ones with IDataErrorInfo
interface.<br /><br />
The problems with the implementations I saw so far was they were like toy examples
or have overly complex implementations. One implementation outstands for me from the
rest. 
<br /><br />
In <a href="http://weblogs.asp.net/marianor/archive/2009/04/17/wpf-validation-with-attributes-and-idataerrorinfo-interface-in-mvvm.aspx">Mariano
Omar Rodriguez'</a>s validation approach, ViewModel decorates its properties with
Data Annotations for validation, and to check these attributes are really valid, ViewModel
uses some reflection and Linq Expressions. To show errors ViewModel also implements
IDataErrorInfo interface. While Mariano's approach seemed a little complex, if I could
carry complexity (IDataErrorInfo implementation and reflection methods/properties)
to a ViewModelBase class, adding new properties and validations would become fairly
easy. 
<br /><br />
I modified Mariano's code with generics to move all those overhead to a base class.
Two things I couldn't carry without friction were IDataErrorInfo's members this[string]
indexer and Error property, because these properties send "this" instances to reflective
methods for gathering IsValid infos from Data Annotation attributes. I was stuck there
for a good solution. My collegue Niyazi came with a solution which suggests we should
declare a T type property in ViewModelBase, and use T property instead of "this" in
ViewModelBase and actual ViewModels which inherit from ViewModelBase should assign
their selves to this generic typed property in their constructors. We were not totally
ok with this yet another overhead for the ViewModel. But it's less crappy or less
code from other solutions out there. So we settled for this solution.<br /><br />
You can grab the solution attached below. Please feel free to comment on this solution's
possible drawbacks.<br /><br /><a href="http://www.berkesokhan.com/blog/content/binary/Samples.Validation.BerkesVariant.rar">Samples.Validation.BerkesVariant.rar
(83.76 KB)</a><img width="0" height="0" src="http://www.berkesokhan.com/blog/aggbug.ashx?id=584899a5-488b-4202-ad93-2b1ab6c85de6" /></body>
      <title>A WPF MVVM Validation Implementation</title>
      <guid isPermaLink="false">http://www.berkesokhan.com/blog/PermaLink,guid,584899a5-488b-4202-ad93-2b1ab6c85de6.aspx</guid>
      <link>http://www.berkesokhan.com/blog/2009/12/06/AWPFMVVMValidationImplementation.aspx</link>
      <pubDate>Sun, 06 Dec 2009 09:20:24 GMT</pubDate>
      <description>We recently started to build a desktop application with WPF and MVVM. &lt;br&gt;
&lt;br&gt;
When we had the need for a validation framework for our view inputs, I started to
look on internet to find "de facto" approach for MVVM and WPF in general. There are
lots of look-alike methods for validation. 
&lt;br&gt;
&lt;br&gt;
For validation I commonly saw three approaches:&lt;br&gt;
&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;
Validation Rules on views&lt;br&gt;
&lt;/li&gt;
&lt;li&gt;
Hand coded validation on ViewModel setters&lt;/li&gt;
&lt;li&gt;
Using some validation framework attributes (mostly System.ComponentModel.DataAnnotations)
on binded Model properties or ViewModel properties.&lt;br&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;br&gt;
For validation error user notification, people complain about how WPF lacks old Windows
Forms built in Error Provider support and invent their custom ones with IDataErrorInfo
interface.&lt;br&gt;
&lt;br&gt;
The problems with the implementations I saw so far was they were like toy examples
or have overly complex implementations. One implementation outstands for me from the
rest. 
&lt;br&gt;
&lt;br&gt;
In &lt;a href="http://weblogs.asp.net/marianor/archive/2009/04/17/wpf-validation-with-attributes-and-idataerrorinfo-interface-in-mvvm.aspx"&gt;Mariano
Omar Rodriguez'&lt;/a&gt;s validation approach, ViewModel decorates its properties with
Data Annotations for validation, and to check these attributes are really valid, ViewModel
uses some reflection and Linq Expressions. To show errors ViewModel also implements
IDataErrorInfo interface. While Mariano's approach seemed a little complex, if I could
carry complexity (IDataErrorInfo implementation and reflection methods/properties)
to a ViewModelBase class, adding new properties and validations would become fairly
easy. 
&lt;br&gt;
&lt;br&gt;
I modified Mariano's code with generics to move all those overhead to a base class.
Two things I couldn't carry without friction were IDataErrorInfo's members this[string]
indexer and Error property, because these properties send "this" instances to reflective
methods for gathering IsValid infos from Data Annotation attributes. I was stuck there
for a good solution. My collegue Niyazi came with a solution which suggests we should
declare a T type property in ViewModelBase, and use T property instead of "this" in
ViewModelBase and actual ViewModels which inherit from ViewModelBase should assign
their selves to this generic typed property in their constructors. We were not totally
ok with this yet another overhead for the ViewModel. But it's less crappy or less
code from other solutions out there. So we settled for this solution.&lt;br&gt;
&lt;br&gt;
You can grab the solution attached below. Please feel free to comment on this solution's
possible drawbacks.&lt;br&gt;
&lt;br&gt;
&lt;a href="http://www.berkesokhan.com/blog/content/binary/Samples.Validation.BerkesVariant.rar"&gt;Samples.Validation.BerkesVariant.rar
(83.76 KB)&lt;/a&gt;&lt;img width="0" height="0" src="http://www.berkesokhan.com/blog/aggbug.ashx?id=584899a5-488b-4202-ad93-2b1ab6c85de6" /&gt;</description>
      <comments>http://www.berkesokhan.com/blog/CommentView,guid,584899a5-488b-4202-ad93-2b1ab6c85de6.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>In English</category>
      <category>MVVM</category>
      <category>Teknik</category>
      <category>WPF</category>
    </item>
  </channel>
</rss>