Showing posts with label Validation Application Block. Show all posts
Showing posts with label Validation Application Block. Show all posts

Tuesday, April 30, 2013

Enterprise Library 6 is out!

Enterprise Library provides an excellent way to keep the cross-cutting concerns outside your main application code. Microsoft Pattern & Practices team released new version of Enterprise Library last week. This was released on 25th April and this is Version 6 of the library. Full version of the library and source code can be downloaded from Microsoft's Download Center.



The documentation can be downloaded directly form Pattern & Practices team's codeplex page. The documentation is still in preview version and is not updated on MSDN. Still this can be downloaded from the main page. In addition to the Enterprise Library documentation, it also includes the migration guide for migrating applications from Enterprise Library 5. You can also find the reference PDF for Unity Application Block version 3 which is provided as part of the new version of Enterprise Library.



List of Application Blocks in Enterprise Library 6
The library seems to be released with exciting new features in existing application block. It includes a new application block with the name Semantic Logging Application Block. Transient Fault Handling application Block seems to have been added from Azure Integration Pack for Enterprise Library 5. Enterprise Library 6 includes the following application blocks.
  1. Data Access Application Block
  2. Exception Handling Application Block
  3. Logging Application Block
  4. Policy Injection Application Block
  5. Semantic Logging Application Block
  6. Transient Fault Handling Application Block
  7. Unity Application Block
  8. Validation Application Block
As discussed above, Semantic Application Block is the newest in the list. This is supposed to be built on top of EventSource provided in .net framework 4.5 for structured logging. EventSource provides structured logging support for ETW events. Semantic Application Block is supposed to add support structured logging by specializing EventSource, where the logs can be sinked to Azure or SQL Server database.

There are other application blocks which used to be in Enterprise Library 5 and haven't been migrated over to EntLib6. They include the following:
  1. Cryptography Application Block
  2. Caching Application Block
  3. Validation Application Block
Back in 2010, we did discuss about how we can use Validation Application Block in an MVVM based application.

Unity 3.0
The release also includes a new version of unity application block. This is version 3 of Unity. As we know Unity provides the support of Dependency Injection for .net applications. This is a container based approach to Inversion of Control. Unity 3 extends Dependency Injection Windows store Apps and deeper integration with ASP.net MVC and Web API.

Requirements for Enterprise Library 6
Enterprise Library 6 only works with .net framework 4.5 and .net for Windows Store Apps (for Unity and Transient fault handling application block. It cannot be used side by side with existing Enterprise Library 5 binaries. If you are still on .net framework 4.0, you will need to make a decision about migrating to .net framework 4.5 first before moving to the new version of the enterprise library.

Using Enterprise Library Application Blocks
You can download the list of the binaries from the link provided in download section to use enterprise library's application blocks in your code. Pattern & Practices team suggests using Nuget for this purpose. For ease of developers, Pattern & Practices team has provided these application blocks in the form of Nuget packages. These Nuget packages can be used individually as per the requirement. Just add the appropriate package to your project using Nuget



Still To Come
This is the view of the release from top. There are other minor improvements and updates which require more detailed discussions. I am planning a few posts about the updates in Enterprise Library 6. You might find me blogging about them next month. You can find release notes here.

Sunday, July 18, 2010

WPF Validation - Using Validation Application Block

As most of us like to take advantage of Enterprise library for many of required cross cutting concerns, we wish to use them with every technology we use to develop our softwares. One of the most used application block in Enterprise library is Validation Application Block. The great feature of this application block is that we can specify validation logic in different places including code, attributes and configuration files. We love it!!!

The issue was that this application block was not providing any built-in support for Windows Presentation Foundation. As we know for validating data in a WPF form, we generally use one of the specializations of ValidationRule class. We needed support for hooking up the validation logic specified in Validation Application Block with these Validation rules. It might be a news for you that the updated Validation Block released with Enterprise library 5.0 supports this. Zindabad!!!

Enterprise library 5.0 can be downloaded from this location:
http://www.microsoft.com/downloads/details.aspx?FamilyId=bcb166f7-dd16-448b-a152-9845760d9b4c&displaylang=en


After downloading install it. You will have to reference the assemblies as specified below:



In this post, we will specify our validation logic in the following places individually and combinations of them and see how we can use them in our WPF XAML.
  1. Attributes
  2. Configuration
  3. Code

Validation specification in Attributes
Let us create our View Model. We name it as StudentViewModel. Please notice that we have included two namespaces System.Windows (for DependencyProperty) and Microsoft.Practices.EnterpriseLibrary.Validation.Validators (for Validation attributes).

namespace ValidationBlockExampleWPF
{
using System.Windows;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

class StudentViewModel : DependencyObject
{
[StringLengthValidator(5, RangeBoundaryType.Inclusive, 20, RangeBoundaryType.Inclusive,
MessageTemplate = "[{0}]Name must be between {3} and {5} characters.")]
public string StudentName
{
get { return (string) GetValue(StudentNameProperty); }
set { SetValue(StudentNameProperty, value); }
}

public static DependencyProperty StudentNameProperty =
DependencyProperty.Register("StudentName", typeof(string), typeof(StudentViewModel));
}
}


In this simple View model, we have created a property StudentName. We have registered it with WPF property system. We have specified validation for this property. These only validation is about limitations for width (5 to 20 characters). Here Inclusive means that boundary values (5 and 20) are included in Valid limits for length. In the case that this property exceeds this limit, error message can be obtained as provided in the MessageTemplate in validation attribute.

Now let us create a simple WPF window using this View model as its data context.

<Window x:Class="ValidationBlockExampleaWPF.StudentWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ValidationBlockExampleaWPF"
xmlns:vab="clr-namespace:Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WPF;assembly=Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WPF"
Title="StudentWindow" Height="300" Width="540"
DataContext="{DynamicResource ViewModel}" >
<Window.Resources>
<local:StudentViewModel x:Key="ViewModel" />
</Window.Resources>
<Grid>
<Label HorizontalAlignment="Left" Margin="12,20,0,0" Name="label1"
Width="94" Height="21" VerticalAlignment="Top">Student Name</Label>
<TextBox Height="24" Margin="112,20,12,0" Name="textBox1" VerticalAlignment="Top" >
<TextBox.Text>
<Binding Path="StudentName" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<vab:ValidatorRule SourceType="{x:Type local:StudentViewModel}" SourcePropertyName="StudentName" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
</Window>

Here we have added two namespaces local (for the current project) and vab (for using WPF integration features of Validation Application Block). We have bound the only text box in the window with StudentName property of the data context. The most important thing in the above code is following:
<vab:ValidatorRule SourceType="{x:Type local:StudentViewModel}" SourcePropertyName="StudentName" />

It is asking to use the validation logic as specified in the attributes of StudentName property of StudentViewModel class. Now this is to support the cases when we are binding with the view model but want to use validation as specified in any property of any other class (including model classes). In our case, since we have directly specified the validation in view model, we can update the text box definition as follows:
<TextBox Height="24" Margin="112,20,12,0" Name="textBox1" VerticalAlignment="Top" 
vab:Validate.BindingForProperty="Text">
<TextBox.Text>
<Binding Path="StudentName" UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>
</TextBox>

Here it is asking the runtime to use the validation logic from the property in the data context bound with Text property of the control. Basically, WPF integration in validation block, adds a validation rule whenever it sees this.

We are trying to adhere to MVVM to the best. The code behind of the above window is as follows:

namespace ValidationBlockExampleaWPF
{
///
/// Interaction logic for StudentWindow.xaml
///

public partial class StudentWindow : Window
{
public StudentWindow()
{
InitializeComponent();
}
}
}

Now we run the project. As we start typing the name, we can see the validation being done.



But as the number of characters reach the valid limit, the error adorner is no more displayed with the text box.



Validation specification in Configuration:
As we discussed above, we can also specify validation details in configuration file. Let us define configuration for our view model validation. We can define validation configuration in Enterprise library configuration utility. When you install Enterprise library it is available in start menu:



It must be remembered that in order to define validation configuration in the configuration utility, the type must be public. Let us make our StudentViewModel public and build the project. If we don't want to keep it public, we can later update it. Now it should be available as follows:



Now we add a regular expression validator for our StudentName property. Here we are specifying that StudentName can not start with any digit between 0 and 9. We have defined this validation in BasicInfoRuleSet rule set.



Now save the configuration as App.config. The configuration file is created in xml. If you open the configuration file, you can find that the configuration is defined as follows:
<configuration>
<configSections>
<section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<validation>
<type name="ValidationBlockExampleaWPF.StudentViewModel" assemblyName="ValidationBlockExampleaWPF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<ruleset name="BasicInfoRuleSet">
<properties>
<property name="StudentName">
<validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
pattern="^[^0-9]" name="Regular Expression Validator" />
</property>
</properties>
</ruleset>
</type>
</validation>
</configuration>

We update the StudentName property in StudentViewModel and specify the previously defined validation (length between 5 and 20) as follows:

[StringLengthValidator(5, RangeBoundaryType.Inclusive, 20, RangeBoundaryType.Inclusive,
MessageTemplate = "[{0}]Name must be between {3} and {5} characters.", Ruleset = "BasicInfoRuleSet")]
public string StudentName
{
get { return (string) GetValue(StudentNameProperty); }
set { SetValue(StudentNameProperty, value); }
}

If you have not noticed then let me explain. Some amazing thing has just happened. If you remember from above, we defined our regular expression validator to be part of BasicInfoRuleSet. Now we have added one more validation in the same rule set but now it is in the attributes in code. So the same ruleset can span in multiple places. It is the responsibility of Validation application block to get all the validations defined for the property and decide if the value is valid. Amazing!!!

In order to use the validations from all these different sources, we have to update the text box in StudentWindow as follows:

<TextBox Height="24" Margin="112,20,12,0" Name="textBox1" VerticalAlignment="Top"
vab:Validate.UsingRulesetName="BasicInfoRuleSet" vab:Validate.BindingForProperty="Text" vab:Validate.UsingSource="All">
<TextBox.Text>
<Binding Path="StudentName" UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>
</TextBox>

Here we have specified that the validation for data entered should be using BasicInfoRuleSet. It should be using the validation defined in all sources possible.

Note:
It must be remembered that in the whole discussion above, the validation would be executed for the converted value and the value would be committed to the bounded property. We can control which value to use (converted / committed etc) using ValidationStep attribute in ValidationRule. So if you want to control which value to use then use ValidationRule tag instead of defining validation in the TextBox itself.



Note:
We can not use Self Validation feature of Validation Application Block with ValidationRule in XAML.