<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6069495622049300789</id><updated>2012-02-16T23:56:51.661+03:30</updated><category term='Resharper'/><category term='LINQ'/><category term='jQuery'/><category term='MVVM'/><category term='Prism'/><category term='REST'/><category term='Windsor'/><category term='Code Analysis'/><category term='ESB'/><category term='Cracking'/><category term='VS.NET'/><category term='UnitTest'/><category term='MSBuild'/><category term='Security'/><category term='Interoperation'/><category term='Java'/><category term='ASP.NET MVC'/><category term='Add-Ins'/><category term='Testing'/><category term='xamDataGrid'/><category term='Ribbon'/><category term='Reporting'/><category term='C#'/><category term='Rhino Tools'/><category term='Json'/><category term='NDepend'/><category term='NHibernate Validator'/><category term='TDD'/><category term='General'/><category term='Caliburn'/><category term='WCF'/><category term='PDC'/><category term='Tools'/><category term='NHibernate'/><category term='Calendars'/><category term='FarsiLibrary'/><category term='Events'/><category term='WinForms'/><category term='WPF'/><category term='.NET'/><category term='Silverlight'/><category term='Windows 7'/><title type='text'>SeeSharp</title><subtitle type='html'>A C# Development Blog</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>89</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-3007853588280764861</id><published>2009-09-08T15:49:00.004+04:30</published><updated>2009-09-08T15:54:18.557+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET MVC'/><title type='text'>Anatomy of an ASP.NET MVC Control</title><content type='html'>One of the great things about ASP.NET WebForms was that it wasy very easy to creat reuseable custom controls and components, but with appearance of ASP.NET MVC and lots of developers moving towards it – which is a good thing – what happens to component oriented development? Is is possible to create custom controls in MVC? Can we port our existing controls to MVC? ASP.NET MVC while may not be as good as WebForms when it comes to drag-and-drop style of using components, but it is fairly easy to use and to create custom controls in MVC! Let’s see how.&lt;br /&gt;&lt;br /&gt;To create a control in WebForms world, you’d extend the existing “WebControl” class and override Render method which takes a HtmlTextWriter that you can use to output the desired HTML code to the browser. A control rendering “Hello World” on the screen would be as simple as this:&lt;br /&gt;&lt;pre class="c-sharp" name="code"&gt;public class HelloWorldControl : WebControl&lt;br /&gt;{&lt;br /&gt;    protected override void Render(HtmlTextWriter writer)&lt;br /&gt;    {&lt;br /&gt;        base.Render(writer);&lt;br /&gt;&lt;br /&gt;        writer.Write("&amp;lt;span&amp;gt;HelloWorld&amp;lt;/span&amp;gt;");&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;You’d put this on your web page and you’re all set. But what’s the best approach in MVC controls? There’s no WebControl or Control classes inside Mvc assemblies, so can either use any existing class you have as a base class or it from scratch. &lt;br /&gt;&lt;pre class="c-sharp" name="code"&gt;public class WebControl&lt;br /&gt;{&lt;br /&gt;    /// &amp;lt;summary&amp;gt;&lt;br /&gt;    /// Renders the control by writing the html code into the text writer.&lt;br /&gt;    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;    /// &amp;lt;param name="textWriter"&amp;gt;&amp;lt;/param&amp;gt;&lt;br /&gt;    public virtual void Render(TextWriter textWriter)&lt;br /&gt;    {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /// &amp;lt;summary&amp;gt;&lt;br /&gt;    /// Renders the control and outputs the generated HTML code.&lt;br /&gt;    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;    /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;br /&gt;    public string OnRender()&lt;br /&gt;    {&lt;br /&gt;        var tw = new StringWriter();&lt;br /&gt;&lt;br /&gt;        Render(tw);&lt;br /&gt;&lt;br /&gt;        return tw.ToString();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;With this, you have a ready to use base class for your controls. You need to override the Render method and construct the right HTML code. The code would look almost identical:&lt;br /&gt;&lt;pre class="c-sharp" name="code"&gt;public class HelloWorldControl : WebControl&lt;br /&gt;{&lt;br /&gt;    public override void Render(TextWriter writer)&lt;br /&gt;    {&lt;br /&gt;        base.Render(writer);&lt;br /&gt;&lt;br /&gt;        writer.Write("&amp;lt;span&amp;gt;Hello World!&amp;lt;/span&amp;gt;");&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Note: There’s a catch here. We’d use existing methods on HtmlTextWriter in WebForms, which emitted correct HTML code per Browser. If you’re writing the HTML code manually (as I did in this example) you’d be careful in case you need to support different browsers.&lt;br /&gt;&lt;br /&gt;The only thing remaining is, how do we use this in an MVC application? The way to do this, as it is already done in the MVC framework with other controls such as TextBox, CheckBox, etc. is to create an extension method on HtmlHelper class. HtmlHelper is just there as a waypoint for extension method and has no other uses:&lt;br /&gt;&lt;pre class="c-sharp" name="code"&gt;public static class HelloWorldControlExtensions&lt;br /&gt;{&lt;br /&gt;    public static string HelloWorld(this HtmlHelper helper)&lt;br /&gt;    {&lt;br /&gt;        var control = new HelloWorldControl();&lt;br /&gt;&lt;br /&gt;        return control.OnRender();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Now you can add this controls to your views, as easy as other controls such as TextBoxes:&lt;br /&gt;&lt;pre class="c-sharp" name="code"&gt;&amp;lt;%= Html.HelloWorld() %&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;That’s almost it. The only thing remaining here is to implement control specific logic of your components and that part is left to you.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-3007853588280764861?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/3007853588280764861/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=3007853588280764861' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/3007853588280764861'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/3007853588280764861'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/09/anatomy-of-aspnet-mvc-control.html' title='Anatomy of an ASP.NET MVC Control'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-6350178076442278528</id><published>2009-09-02T11:59:00.003+04:30</published><updated>2009-09-02T14:23:19.199+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='MVVM'/><category scheme='http://www.blogger.com/atom/ns#' term='WPF'/><category scheme='http://www.blogger.com/atom/ns#' term='Caliburn'/><title type='text'>Caliburn = Less Code?</title><content type='html'>&lt;a href="http://www.codeplex.com/caliburn"&gt;Caliburn&lt;/a&gt; is an application framework for WPF / Silverlight. If you’re developing applications for these platforms, there are many reasons why you definitely need to be using this framework, but today, I noticed how using this framework resulted writing less code while doing more. Let’s see how writing a small WPF application using &lt;a href="http://en.wikipedia.org/wiki/Model_View_ViewModel"&gt;MVVM&lt;/a&gt; pattern is different when using &lt;a href="http://www.codeplex.com/caliburn"&gt;Caliburn&lt;/a&gt;.&lt;br /&gt;&lt;i&gt;&lt;b&gt;Note: There are many other ways to skin a cat. This is how I do things, there may be other, better ways to do the same thing.&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0080c0; font-size: 100%;"&gt;&lt;b&gt;Application Startup&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;When creating an application using MVVM and “&lt;a href="http://wildermuth.com/2009/05/22/Which_came_first_the_View_or_the_Model"&gt;ViewModel First&lt;/a&gt;” development, you’d create the root View and VM, bind them through DataContext and show the main window of the application.  &lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;///&lt;br /&gt;/// Interaction logic for App.xaml&lt;br /&gt;///&lt;br /&gt;public partial class App : Application&lt;br /&gt;{&lt;br /&gt;   protected override void OnStartup(StartupEventArgs e)&lt;br /&gt;   {&lt;br /&gt;      base.OnStartup(e);&lt;br /&gt;&lt;br /&gt;      var container = new WindsorContainer();&lt;br /&gt;      var rootView = new RootView();&lt;br /&gt;      var rootModel = new RootViewModel();&lt;br /&gt;&lt;br /&gt;      rootView.DataContext = rootModel;&lt;br /&gt;      this.MainWindow = rootView;&lt;br /&gt;      this.MainWindow.Show();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;With caliburn, all you need to do is to create the root VM. There’s no trace of any View creation and binding the DataContext to VM. All this is done automatically by Caliburn. To do this, use “&lt;a href="http://caliburn.codeplex.com/Wiki/View.aspx?title=Application"&gt;CaliburnApplication&lt;/a&gt;” as your base application class which will configure Caliburn framework upon startup. There are &lt;a href="http://caliburn.codeplex.com/Wiki/View.aspx?title=Project%20Setup&amp;amp;referringTitle=Home"&gt;other ways&lt;/a&gt; to do the configuration but let’s stick to this simple configuration scenario that installs default implementations of Calburn “Components”.&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;///&lt;br /&gt;/// Interaction logic for App.xaml&lt;br /&gt;///&lt;br /&gt;public partial class App : CaliburnApplication&lt;br /&gt;{&lt;br /&gt;   protected override IServiceLocator CreateContainer()&lt;br /&gt;   {&lt;br /&gt;      var container = new WindsorContainer();&lt;br /&gt;      var adapter = new WindsorAdapter(container);&lt;br /&gt;&lt;br /&gt;      return adapter;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   protected override object CreateRootModel()&lt;br /&gt;   {&lt;br /&gt;      return new RootViewModel();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;One thing a little different is that Caliburn uses &lt;a href="http://msdn.microsoft.com/en-us/library/dd458913.aspx"&gt;IServiceLocator&lt;/a&gt; insterface (from &lt;a href="http://www.codeplex.com/CommonServiceLocator/"&gt;Common Service Locator&lt;/a&gt;) and has no direct dependency on a vendor specific &lt;a href="http://martinfowler.com/articles/injection.html"&gt;IoC Container&lt;/a&gt;, which is a good thing. This means, you can choose various IoC containers and adapters for major IoC containers like &lt;a href="http://www.castleproject.org/container/index.html"&gt;Windsor&lt;/a&gt;, &lt;a href="http://ninject.org/"&gt;Ninject&lt;/a&gt;, &lt;a href="http://code.google.com/p/autofac/"&gt;Autofac&lt;/a&gt;, etc. are already included in Caliburn bits. If you're not a fan of IoC containers and won't be using one in your application (now, come on!), Caliburn uses a lightweight built-in container, in case you don't specify any other.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0080c0; font-size: 100%;"&gt;&lt;b&gt;Actions and Commands&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;Should you bind an action to a ICommand instance on the VM, in pure MVVM application you would bind the &lt;a href="http://msdn.microsoft.com/en-us/library/ms752308.aspx"&gt;Command&lt;/a&gt; property to an &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx"&gt;ICommand&lt;/a&gt; instance on the VM. You can specify the condition on which the command will be executable and you’ll also implement the action execute by the command.&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;&amp;lt;Button Command="{Binding ExitCommand}" Content="Exit Application"&amp;gt;&lt;br /&gt;&lt;br /&gt;public class RootViewModel : BaseViewModel&lt;br /&gt;{&lt;br /&gt;   private ICommand _exitCommand;&lt;br /&gt;   public ICommand ExitCommand&lt;br /&gt;   {&lt;br /&gt;      get&lt;br /&gt;      {&lt;br /&gt;         if (_exitCommand == null)&lt;br /&gt;           _exitCommand = new RelayCommand(this.Exit, this.CanExit);&lt;br /&gt;&lt;br /&gt;         return _exitCommand;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public bool CanExit()&lt;br /&gt;   {&lt;br /&gt;      return true;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void Exit()&lt;br /&gt;   {&lt;br /&gt;      Application.Current.Shutdown();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Notice there are other “hidden” code here too. You’ll need to create a new Command, or use &lt;a href="http://www.codeproject.com/KB/WPF/VMCommanding.aspx"&gt;RelayCommand&lt;/a&gt; (kudos to &lt;a href="http://joshsmithonwpf.wordpress.com/"&gt;Josh&lt;/a&gt;) and you probably need a common &lt;a href="http://martinfowler.com/eaaCatalog/layerSupertype.html"&gt;layer supertype&lt;/a&gt; that implements &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx"&gt;INotifyPropertyChanged&lt;/a&gt;&lt;i&gt;&lt;/i&gt; interface. Using Caliburn you'd bind the &lt;a href="http://caliburn.codeplex.com/Wiki/View.aspx?title=Action%20Basics&amp;amp;referringTitle=Home"&gt;action&lt;/a&gt; directly to the VM and there's no dependency to ICommand instances. The good thing is, you’re not limited to Command property anymore and you can bind various other events such as Click, MouseOver, etc. to actions on your VM.&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;&amp;lt;Button cal:Message.Attach="[Event Click] = [Action Exit]" Content="Exit Application"&amp;gt;&lt;br /&gt;&lt;br /&gt;public class RootViewModel : Presenter&lt;br /&gt;{&lt;br /&gt;   public bool CanExit()&lt;br /&gt;   {&lt;br /&gt;      return true;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void Exit()&lt;br /&gt;   {&lt;br /&gt;      Application.Current.Shutdown();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;There are lots of existing boilerplate functionalities already available in Caliburn, so you can drop BaseViewModel and use &lt;a href="http://caliburn.codeplex.com/Wiki/View.aspx?title=IPresenter%20Component%20Model"&gt;Presenter&lt;/a&gt; class (IPresenter implementation).&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0080c0; font-size: 100%;"&gt;&lt;b&gt;Displaying Views&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;To display other views in the RootView (e.g. Shell), we’d constructed a new VM and set it to a Content property of a ContentControl. This will actually pick up the view specified in the DataTemplate from Application Resources, and display it on the UI. You have to maintain a mapping between your Views and your VMs in a resource dictionary and as your application grows this gets harder to maintain.&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;&amp;lt;Application.Resources&amp;gt;&lt;br /&gt;   &amp;lt;DataTemplate DataType="{x:Type vm:CustomerViewModel}"&amp;gt;&lt;br /&gt;      &amp;lt;view:CustomerView /&amp;gt;&lt;br /&gt;   &amp;lt;/DataTemplate&amp;gt;&lt;br /&gt;&amp;lt;/Application.Resources&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;ContentControl Content="{Binding Path=CurrentView}"/&amp;gt;&lt;br /&gt;&lt;br /&gt;public class RootViewModel : BaseViewModel&lt;br /&gt;{&lt;br /&gt;   public void ShowCustomer()&lt;br /&gt;   {&lt;br /&gt;      CurrentView = IoC.Resolve&amp;lt;Customerviewmodel&amp;gt;();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   private BaseViewModel _currentView;&lt;br /&gt;   public BaseViewModel CurrentView&lt;br /&gt;   {&lt;br /&gt;      get { return _currentView; }&lt;br /&gt;      set&lt;br /&gt;      {&lt;br /&gt;         _currentView = value;&lt;br /&gt;         RaisePropertyChanged("CurrentView");&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Let’s see how we can do this using Caliburn. First off, we do not need to maintain any ViewModel mapping if we name our Views and ViewModels according to the Caliburn &lt;a href="http://caliburn.codeplex.com/Wiki/View.aspx?title=IBinder%20and%20Convention%20over%20Configuration"&gt;Conventions&lt;/a&gt; that is, your view names should ending in View, presenters / VMs ending in “ViewModel” or “Presenter” and each should be in a separate namespace, namingly “Views” and “ViewModels” or “Presenters”. This is just by convention and you can always override if you need to. Caliburn will automatically create the view for you, set the “CurrentPresenter” property on the main window (i.e. &lt;a href="http://caliburn.codeplex.com/Wiki/View.aspx?title=IPresenter%20Component%20Model"&gt;IPresenterHost&lt;/a&gt;) and do any required bindings. Did I mention you don’t need the whole mapping of View &amp;lt;-&amp;gt; &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.datatemplate.aspx"&gt;DataTemplate&lt;/a&gt; in your Application / Form resource?&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;&amp;lt;ContentControl x:Name="CurrentPresenter" &amp;gt;&lt;br /&gt;&lt;br /&gt;[Singleton("RootViewModel")]&lt;br /&gt;public class RootViewModel : PresenterManager&lt;br /&gt;{&lt;br /&gt;   public void ShowCustomer()&lt;br /&gt;   {&lt;br /&gt;      var presenter = ServiceLocator.Current.GetInstance&amp;lt;Customerviewmodel&amp;gt;();&lt;br /&gt;      this.Open(presenter);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span style="color: #0080c0; font-size: 100%;"&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;What’s the catch? How does Caliburn do this? Caliburn uses “&lt;a href="http://caliburn.codeplex.com/Wiki/View.aspx?title=IBinder%20and%20Convention%20over%20Configuration"&gt;Convention over Configuration&lt;/a&gt;” pattern, so if you do things according to the convention you’ll end up saving a lot of code. There’s always the possibility to change the default behavior too. By complying to these conventions, clearly you’ll end up writing less code, but more importantly the code you do write is cleaner, more robust and probably with better testabilty. With the help of “Binding Tests” utilities existing in Caliburn, you can test your bindings the easy way, which is some thought for later posts.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-6350178076442278528?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/6350178076442278528/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=6350178076442278528' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6350178076442278528'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6350178076442278528'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/09/caliburn-less-code.html' title='Caliburn = Less Code?'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-6810951255549317937</id><published>2009-08-20T12:08:00.002+04:30</published><updated>2009-08-20T12:14:12.273+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Windsor'/><title type='text'>Windsor Registration : Service Interface</title><content type='html'>&lt;p&gt;Sometimes when registering classes on Windsor container, that extend an existing base class with an interface, using auto registration and FirstInterface method is useless, because first interface finds the interface implemented by base class:&lt;/p&gt;  &lt;pre class="code"&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;TestFixture&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;RegistrationFixture &lt;/span&gt;: &lt;span style="color: rgb(166, 83, 0);"&gt;ContainerTest&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   [&lt;span style="color: rgb(166, 83, 0);"&gt;Test&lt;/span&gt;]&lt;br /&gt;   &lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Can_Find_Service_Using_Interface&lt;/span&gt;()&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;service &lt;/span&gt;= &lt;span style="color:maroon;"&gt;TryResolve&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IService&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;       &lt;span style="color: rgb(166, 83, 0);"&gt;Assert&lt;/span&gt;.&lt;span style="color:maroon;"&gt;That&lt;/span&gt;(&lt;span style="color:maroon;"&gt;service&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;Is&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Not&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Null&lt;/span&gt;);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;protected override void &lt;/span&gt;&lt;span style="color:maroon;"&gt;OnSetup&lt;/span&gt;()&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;base&lt;/span&gt;.&lt;span style="color:maroon;"&gt;OnSetup&lt;/span&gt;();&lt;br /&gt;&lt;br /&gt;       &lt;span style="color:maroon;"&gt;Container&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Register&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;AllTypes&lt;/span&gt;.&lt;span style="color:maroon;"&gt;From&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ServiceImpl&lt;/span&gt;))&lt;br /&gt;                .&lt;span style="color:maroon;"&gt;Where&lt;/span&gt;(&lt;span style="color:navy;"&gt;null&lt;/span&gt;)&lt;br /&gt;                .&lt;span style="color:maroon;"&gt;Configure&lt;/span&gt;(&lt;span style="color:maroon;"&gt;x &lt;/span&gt;=&amp;gt; &lt;span style="color:maroon;"&gt;x&lt;/span&gt;.&lt;span style="color:maroon;"&gt;LifeStyle&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Is&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;LifestyleType&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Transient&lt;/span&gt;))&lt;br /&gt;                .&lt;span style="color:maroon;"&gt;WithService&lt;/span&gt;.&lt;span style="color:maroon;"&gt;FirstInterface&lt;/span&gt;());&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;private interface &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IBaseService&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;private interface &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IService&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;private class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;BaseService &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IBaseService&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;private class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ServiceImpl &lt;/span&gt;: &lt;span style="color: rgb(166, 83, 0);"&gt;BaseService&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;IService&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;If you run the test, it will fail, because ServiceImpl is registered with IBaseService interface, which is the first interface on the class hierarchy. But that is not what I intended to do. I wanted the to register it using the first interface on the same type, e.g. IService. Extension methods come to the rescue:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public static class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ContainerExtensions&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;BasedOnDescriptor &lt;/span&gt;&lt;span style="color:maroon;"&gt;FirstInterfaceOnClass&lt;/span&gt;(&lt;span style="color:navy;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ServiceDescriptor &lt;/span&gt;&lt;span style="color:maroon;"&gt;serviceDescriptor&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;serviceDescriptor&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Select&lt;/span&gt;((&lt;span style="color:maroon;"&gt;t&lt;/span&gt;, &lt;span style="color:maroon;"&gt;bt&lt;/span&gt;) =&amp;gt;&lt;br /&gt;       {&lt;br /&gt;           &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;baseInterfaces &lt;/span&gt;= &lt;span style="color:maroon;"&gt;t&lt;/span&gt;.&lt;span style="color:maroon;"&gt;BaseType&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetInterfaces&lt;/span&gt;();&lt;br /&gt;           &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;interfaces &lt;/span&gt;= &lt;span style="color:maroon;"&gt;t&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetInterfaces&lt;/span&gt;().&lt;span style="color:maroon;"&gt;Except&lt;/span&gt;(&lt;span style="color:maroon;"&gt;baseInterfaces&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;           &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;interfaces&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Count&lt;/span&gt;() != &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt; ? &lt;span style="color:navy;"&gt;new&lt;/span&gt;[] {&lt;span style="color:maroon;"&gt;interfaces&lt;/span&gt;.&lt;span style="color:maroon;"&gt;First&lt;/span&gt;()} : &lt;span style="color:navy;"&gt;null&lt;/span&gt;;&lt;br /&gt;       });&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;Changing the registration to this would pass the failing test:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:maroon;"&gt;Container&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Register&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;AllTypes&lt;/span&gt;.&lt;span style="color:maroon;"&gt;From&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ServiceImpl&lt;/span&gt;))&lt;br /&gt;        .&lt;span style="color:maroon;"&gt;Where&lt;/span&gt;(&lt;span style="color:navy;"&gt;null&lt;/span&gt;)&lt;br /&gt;        .&lt;span style="color:maroon;"&gt;Configure&lt;/span&gt;(&lt;span style="color:maroon;"&gt;x &lt;/span&gt;=&amp;gt; &lt;span style="color:maroon;"&gt;x&lt;/span&gt;.&lt;span style="color:maroon;"&gt;LifeStyle&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Is&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;LifestyleType&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Transient&lt;/span&gt;))&lt;br /&gt;        .&lt;span style="color:maroon;"&gt;WithService&lt;/span&gt;.&lt;span style="color:maroon;"&gt;FirstInterfaceOfClass&lt;/span&gt;());&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-6810951255549317937?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/6810951255549317937/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=6810951255549317937' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6810951255549317937'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6810951255549317937'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/08/windsor-registration-service-interface.html' title='Windsor Registration : Service Interface'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-1082298095406063296</id><published>2009-07-30T13:11:00.002+04:30</published><updated>2009-07-30T13:25:43.419+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='WPF'/><category scheme='http://www.blogger.com/atom/ns#' term='General'/><title type='text'>LOB Application in WPF</title><content type='html'>&lt;p&gt;I’ve been working on a small LoB application to manage sales of a small sales office. I thought it’d be a good idea to put to use my WPF knowledge and use WPF to create this application. The reason is obvious : Programming in WPF is as easy as it gets, more testable (as in Unit Testing and Acceptance Testing) and more end-user friendly. &lt;/p&gt;  &lt;p&gt;Following applicaiton components and patterns were used:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;NHibernate &lt;/li&gt;    &lt;li&gt;NHibernate Validator to validate both entities and ViewModels &lt;/li&gt;    &lt;li&gt;FarsiLibrary (globalizing Dates, DatePickers, etc.) &lt;/li&gt;    &lt;li&gt;MVVM pattern &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;span style="color:#0080ff;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;One part that took more effort than expected was localization and globalization of the application. In general, there are three things you need to check to achieve full localization, if you provide both Left-To-Right reading order languages (English) as well as Right-To-Left (Farsi, Arabic) :&lt;/p&gt;  &lt;h4&gt;&lt;strong&gt;&lt;span style="color:#0080ff;"&gt;Flow Direction&lt;/span&gt;&lt;/strong&gt;&lt;/h4&gt;  &lt;p&gt;Thanks to WPF engine, most of the calculation of LTR reading order to RTL for Forms, UserControls and even Custom Controls is done automagically, although in custom controls, you might need to add some triggers to switch things like shadows, etc. and if you use a 3rd party custom control or library, you need to make sure component vendor has take care of issue like that.&lt;/p&gt;    &lt;h4&gt;&lt;strong&gt;&lt;span style="color:#0080ff;"&gt;Texts&lt;/span&gt;&lt;/strong&gt;&lt;/h4&gt;    &lt;p&gt;WPF uses LocaBaml to extract localizable properties’ values to an excel file and  after translation (e.g. assigning values for other languages), puts them in a  satelite assembly. This approach looks rough to maintain but there are lots of  libraries on Codeplex that will help you do this with xml files or .resx files. My suggestion is to use &lt;a href="http://wpflocalizeextension.codeplex.com/"&gt;WPF Localization Extension&lt;/a&gt; which supports localizing FlowDirection, Brush, Texts, Images, etc.&lt;br /&gt;&lt;/p&gt;  &lt;h4&gt;&lt;strong&gt;&lt;span style="color:#0080ff;"&gt;Dates&lt;/span&gt;&lt;/strong&gt;&lt;/h4&gt;  &lt;p&gt;Not eveyone use Gregorian Calendar available in english language cultures. For example in Arabic language HijriCalendar is used and for Farsi, PersianCalendar is used and sometimes just setting thread’s culture to the language (e.g. Farsi) would not be enough. If you use dates in your applications (who doesn’t?) you need to use date controls that work fluently with various languages and cultures. I used my own &lt;a href="http://blog.hightech.ir/search/label/FarsiLibrary"&gt;FarsiLibrary control for WPF&lt;/a&gt; which allows you to work with PersianCalendar, HijriCalendar and GregorianCalendar seamlessly.&lt;/p&gt;    &lt;h4&gt;&lt;strong&gt;&lt;span style="color:#0080ff;"&gt;System Messages&lt;/span&gt;&lt;/strong&gt;&lt;/h4&gt;  &lt;p&gt;With all the efforts and steps mentioned above, one part of the application still remains in native language of the Windows Installation. That is when using Windows’s MessageBox to alert user and display messages. Fortunately, I already have my Expression Clone styles which come with a Expression style messagebox that come with localizable icons / button texts. &lt;/p&gt; &lt;center&gt;   &lt;table border="0" cellpadding="0" cellspacing="0" width="500"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td align="left" valign="top"&gt;&lt;a href="http://lh3.ggpht.com/_Z5KTIfnfuNs/SnFbnFTpc4I/AAAAAAAAAQA/kXkyVSgQMzk/s1600-h/ScreenShot003%5B5%5D.png"&gt;&lt;img title="Waring_English_MsgBox" alt="Waring_English_MsgBox" src="http://lh4.ggpht.com/_Z5KTIfnfuNs/SnFbpAM22II/AAAAAAAAAQE/ZmmOhorU3xM/ScreenShot003_thumb%5B3%5D.png?imgmax=800" border="0" height="93" width="218" /&gt;&lt;/a&gt;&lt;/td&gt;          &lt;td align="left" valign="top"&gt;&lt;a href="http://lh4.ggpht.com/_Z5KTIfnfuNs/SnFbqh50bFI/AAAAAAAAAQI/KSQGM9kar1o/s1600-h/ScreenShot002%5B9%5D.png"&gt;&lt;img title="Warning_Persian_MsgBox" alt="Warning_Persian_MsgBox" src="http://lh4.ggpht.com/_Z5KTIfnfuNs/SnFbsPkK3bI/AAAAAAAAAQM/d7XeMazXDqE/ScreenShot002_thumb%5B7%5D.png?imgmax=800" border="0" height="94" width="244" /&gt;&lt;/a&gt;&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/center&gt;  &lt;p&gt;&lt;strong&gt;&lt;span style="color:#0080ff;"&gt;Styling&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;      &lt;p&gt;No doubt one of the features that comes with WPF is the ability to style everything that makes sense to provide a better UX. I used my &lt;a href="http://blog.hightech.ir/2008/04/expression-clone.html"&gt;Expression styles&lt;/a&gt; which you can download &lt;a href="http://blog.hightech.ir/2008/04/expression-clone.html"&gt;here&lt;/a&gt; to style your WPF applications very easily to make it look like Expression series.&lt;br /&gt;&lt;/p&gt;  &lt;p align="center"&gt;&lt;a href="http://lh6.ggpht.com/_Z5KTIfnfuNs/SnFbvKJcDuI/AAAAAAAAAQQ/YV8S_HkzPd0/s1600-h/ScreenShot001%5B7%5D.png"&gt;&lt;img style="border-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="Before Styling" alt="Before Styling" src="http://lh5.ggpht.com/_Z5KTIfnfuNs/SnFbyHL7C0I/AAAAAAAAAQU/aD5Sm5efyT8/ScreenShot001_thumb%5B5%5D.png?imgmax=800" border="0" height="361" width="500" /&gt;&lt;/a&gt;&lt;span style="font-style: italic;font-size:78%;" &gt;Before Styling&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;     &lt;div style="text-align: center;"&gt;&lt;a href="http://lh5.ggpht.com/_Z5KTIfnfuNs/SnFb4LjrRLI/AAAAAAAAAQY/oBAonC9WY_Q/s1600-h/ScreenShot001%5B8%5D.png"&gt;&lt;img style="border-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="After Styling" alt="After Styling" src="http://lh3.ggpht.com/_Z5KTIfnfuNs/SnFczyKmthI/AAAAAAAAAQg/OfxoBPEQoB0/ScreenShot001_thumb%5B6%5D.png?imgmax=800" border="0" height="403" width="500" /&gt;&lt;/a&gt;&lt;span style="font-style: italic;font-size:78%;" &gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;Using Expression Styles&lt;/span&gt;  &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-1082298095406063296?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/1082298095406063296/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=1082298095406063296' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1082298095406063296'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1082298095406063296'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/07/lob-application-in-wpf.html' title='LOB Application in WPF'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Z5KTIfnfuNs/SnFbpAM22II/AAAAAAAAAQE/ZmmOhorU3xM/s72-c/ScreenShot003_thumb%5B3%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-481966823041763779</id><published>2009-07-22T17:09:00.003+04:30</published><updated>2009-07-22T17:16:24.703+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='WPF'/><category scheme='http://www.blogger.com/atom/ns#' term='NHibernate Validator'/><category scheme='http://www.blogger.com/atom/ns#' term='xamDataGrid'/><title type='text'>XamDataGrid Validation with NHibernate Validator</title><content type='html'>&lt;p&gt;One of the good grid controls in WPF world belongs to Infragistics. With all the bells and whistles, there are still some limitation, some are WPF engine limitation and some are not. One thing that is very limiting is validation of data prior them being added to datasource. This is rather a broad subject, but I intended to use NHibernate entities (POCO objects) decorated with NHiberante validator attributes to do the validation for me. &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;  &lt;h3&gt;&lt;strong&gt;&lt;span&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 128, 255);"&gt;NHibernate Validator&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;NHibernate Validator is a subproject in &lt;a href="http://sourceforge.net/projects/nhcontrib/"&gt;NHibernate Contrib&lt;/a&gt; which allows you validate plain business classes by using predefined set of validation attributes. The good thing is, as good as NHibernate uses &lt;a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object"&gt;POCO&lt;/a&gt; (Plain Old CLR Object) objects you can also validate any kind of simple classes using NHibernate validator. There are other validation frameworks out there but I didn’t wanted to clutter my object with the validation mechanism and I also didn’t want to write unnecessary properties on my domain objects (e.g. IsValid, etc.) so NHibernate Validator was a right choice for me. Let’s see how a POCO entity along with validation looks like:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Product&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color:maroon;"&gt;Product&lt;/span&gt;()&lt;br /&gt;  {&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public long &lt;/span&gt;&lt;span style="color:maroon;"&gt;ProductId&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color:navy;"&gt;private set&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  [&lt;span style="color: rgb(166, 83, 0);"&gt;NotNullNotEmpty&lt;/span&gt;]&lt;br /&gt;  &lt;span style="color:navy;"&gt;public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Title&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  [&lt;span style="color: rgb(166, 83, 0);"&gt;NotNull&lt;/span&gt;]&lt;br /&gt;  &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category &lt;/span&gt;&lt;span style="color:maroon;"&gt;Category&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Description&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  [&lt;span style="color: rgb(166, 83, 0);"&gt;Min&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;)]&lt;br /&gt;  &lt;span style="color:navy;"&gt;public long &lt;/span&gt;&lt;span style="color:maroon;"&gt;Quantity&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;h3&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 128, 255);"&gt;Binding To XamDataGrid&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;&lt;a href="http://www.infragistics.com/dotnet/netadvantage/wpf/xamdatagrid.aspx"&gt;XamDataGrid&lt;/a&gt; supports binding to &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx"&gt;IEnumerable&lt;/a&gt;&lt;em&gt;&lt;/em&gt; interface, so an &lt;em&gt;IList&amp;lt;Product&amp;gt;&lt;/em&gt; would work. Also, XamDataGrid needs a public parameterless constructor if you’re going to add to that collection from the grid control. If this requirement is not met, grid control will silently ignore the NewLine setting value. Here’s the xaml snippet to display a grid with predefined fields (columns) and a NewLine row:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;XamDataGrid &lt;/span&gt;&lt;span style="color:red;"&gt;DataSource&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Binding &lt;/span&gt;&lt;span style="color:red;"&gt;ProductList&lt;/span&gt;&lt;span style="color:blue;"&gt;}"&amp;gt;&lt;br /&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;XamDataGrid.FieldLayouts&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;FieldLayout&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;FieldLayout.Settings&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;              &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;FieldLayoutSettings &lt;/span&gt;&lt;span style="color:red;"&gt;AddNewRecordLocation&lt;/span&gt;&lt;span style="color:blue;"&gt;="OnTopFixed" &lt;/span&gt;&lt;span style="color:red;"&gt;AllowDelete&lt;/span&gt;&lt;span style="color:blue;"&gt;="False"&lt;br /&gt;                                        &lt;/span&gt;&lt;span style="color:red;"&gt;AllowAddNew&lt;/span&gt;&lt;span style="color:blue;"&gt;="True" &lt;/span&gt;&lt;span style="color:red;"&gt;ExpansionIndicatorDisplayMode&lt;/span&gt;&lt;span style="color:blue;"&gt;="Never"&lt;br /&gt;                                        /&amp;gt;&lt;br /&gt;          &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;FieldLayout.Settings&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;FieldLayout.Fields&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;              &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Field &lt;/span&gt;&lt;span style="color:red;"&gt;Name&lt;/span&gt;&lt;span style="color:blue;"&gt;="Title"       &lt;/span&gt;&lt;span style="color:red;"&gt;Label&lt;/span&gt;&lt;span style="color:blue;"&gt;="Product Name" /&amp;gt;&lt;br /&gt;              &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Field &lt;/span&gt;&lt;span style="color:red;"&gt;Name&lt;/span&gt;&lt;span style="color:blue;"&gt;="Category"    &lt;/span&gt;&lt;span style="color:red;"&gt;Label&lt;/span&gt;&lt;span style="color:blue;"&gt;="Category" /&amp;gt;&lt;br /&gt;              &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Field &lt;/span&gt;&lt;span style="color:red;"&gt;Name&lt;/span&gt;&lt;span style="color:blue;"&gt;="Description" &lt;/span&gt;&lt;span style="color:red;"&gt;Label&lt;/span&gt;&lt;span style="color:blue;"&gt;="Comment"/&amp;gt;&lt;br /&gt;              &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Field &lt;/span&gt;&lt;span style="color:red;"&gt;Name&lt;/span&gt;&lt;span style="color:blue;"&gt;="Quantity"    &lt;/span&gt;&lt;span style="color:red;"&gt;Label&lt;/span&gt;&lt;span style="color:blue;"&gt;="Qtty" /&amp;gt;&lt;br /&gt;              &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Field &lt;/span&gt;&lt;span style="color:red;"&gt;Name&lt;/span&gt;&lt;span style="color:blue;"&gt;="ProductId"   &lt;/span&gt;&lt;span style="color:red;"&gt;Visibility&lt;/span&gt;&lt;span style="color:blue;"&gt;="Collapsed"/&amp;gt;&lt;br /&gt;          &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;FieldLayout.Fields&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;      &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;FieldLayout&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;  &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;XamDataGrid.FieldLayouts&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;XamDataGrid&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;Now when you run the application and add a new Product object to the datasource, there’s no way to check if all the predefined validation rules are okay. So, how do we change the behavior of the grid control to:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Validate the product object PRIOR being added to the datasource so if an object is not validate, it won’t get added to the data source. &lt;/li&gt;&lt;br /&gt;&lt;li&gt;No redundant validation code should be written. We need to use NHibernate Validator infrastructure we already have in place. &lt;/li&gt;&lt;br /&gt;&lt;li&gt;A reusable mechanism. Our application might end up with lots of grid controls. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;p&gt;One way would be to subclass the grid control which although might work, is a heavy handed solution to our rather little problem. It is not right to put the code in our ViewModel (I’m using &lt;a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx"&gt;MVVM pattern&lt;/a&gt;), which introduces coupling betwee UI and ViewModel. Fortunately, this grid control exposes good events we can tap into and change the default validation behavior and we can do this by creating a new Attached Behavior.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;h3&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 128, 255);"&gt;Validation Attached Behavior&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;Attaching a behavior to an object, as the name puts it, means making the object do something that it would not do by default. According to &lt;a href="http://joshsmithonwpf.wordpress.com/"&gt;Josh Smith&lt;/a&gt;:&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;“The idea is that you set an attached property on an element so that you can gain access to the element from the class that exposes the attached property. Once that class has access to the element, it can hook events on it and, in response to those events firing, make the element do things that it normally would not do. It is a very convenient alternative to creating and using subclasses, and is very XAML-friendly.”&lt;/p&gt;&lt;/blockquote&gt;…and that’s exactly what we’re going to do. We’re going to listen to one event in particular : “RecordUpdating” which fires when the user had edited an item and the control wants to submit the changes back to the data source. Here’s the part that does the validation:&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;private static void &lt;/span&gt;&lt;span style="color:maroon;"&gt;OnRecordUpdating&lt;/span&gt;(&lt;span style="color:navy;"&gt;object &lt;/span&gt;&lt;span style="color:maroon;"&gt;sender&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;RoutedEventArgs &lt;/span&gt;&lt;span style="color:maroon;"&gt;e&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;arg &lt;/span&gt;= (&lt;span style="color: rgb(166, 83, 0);"&gt;RecordUpdatingEventArgs&lt;/span&gt;)&lt;span style="color:maroon;"&gt;e&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;dataobject &lt;/span&gt;= &lt;span style="color:maroon;"&gt;arg&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Record&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DataItem&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;rules &lt;/span&gt;= &lt;span style="color:maroon;"&gt;GetAllInvalidRules&lt;/span&gt;(&lt;span style="color:maroon;"&gt;dataobject&lt;/span&gt;);&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;grid &lt;/span&gt;= (&lt;span style="color: rgb(166, 83, 0);"&gt;XamDataGrid&lt;/span&gt;)&lt;span style="color:maroon;"&gt;sender&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;rules &lt;/span&gt;!= &lt;span style="color:navy;"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; &lt;span style="color:maroon;"&gt;rules&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Count &lt;/span&gt;&amp;gt; &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;propertyName &lt;/span&gt;= &lt;span style="color:maroon;"&gt;rules&lt;/span&gt;[&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;].&lt;span style="color:maroon;"&gt;PropertyPath&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;propertyTitle &lt;/span&gt;= &lt;span style="color:maroon;"&gt;grid&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DefaultFieldLayout&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Fields&lt;/span&gt;[&lt;span style="color:maroon;"&gt;propertyName&lt;/span&gt;].&lt;span style="color:maroon;"&gt;Label&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:maroon;"&gt;arg&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Action &lt;/span&gt;= &lt;span style="color: rgb(43, 145, 175);"&gt;RecordUpdatingAction&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CancelUpdateRetainChanges&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color:maroon;"&gt;arg&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Record&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Tag &lt;/span&gt;= &lt;span style="color:navy;"&gt;string&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Format&lt;/span&gt;("Check Field : {0}", &lt;span style="color:maroon;"&gt;propertyTitle&lt;/span&gt;);&lt;br /&gt;  }&lt;br /&gt;  &lt;span style="color:navy;"&gt;else&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:maroon;"&gt;arg&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Record&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Tag &lt;/span&gt;= &lt;span style="color:navy;"&gt;null&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:maroon;"&gt;e&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Handled &lt;/span&gt;= &lt;span style="color:navy;"&gt;true&lt;/span&gt;;&lt;br /&gt;}&lt;/pre&gt;&lt;h4&gt;&lt;em&gt;Note : We only display one error at a time. If you need to display all of them at once, you can do so by changing the code a little bit.&lt;/em&gt;&lt;/h4&gt;We place the error information on the Record object’s Tag information. In our little RecordSelector style, we’ll display an error icon in case Tag property is set to some error information:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Style &lt;/span&gt;&lt;span style="color:red;"&gt;TargetType&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Type &lt;/span&gt;&lt;span style="color:red;"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;RecordSelector&lt;/span&gt;&lt;span style="color:blue;"&gt;}"&amp;gt;&lt;br /&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Setter &lt;/span&gt;&lt;span style="color:red;"&gt;Property&lt;/span&gt;&lt;span style="color:blue;"&gt;="Template"&amp;gt;&lt;br /&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Setter.Value&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ControlTemplate &lt;/span&gt;&lt;span style="color:red;"&gt;TargetType&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Type &lt;/span&gt;&lt;span style="color:red;"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;RecordSelector&lt;/span&gt;&lt;span style="color:blue;"&gt;}"&amp;gt;&lt;br /&gt;              &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Image &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Name&lt;/span&gt;&lt;span style="color:blue;"&gt;="ErrorIcon" &lt;/span&gt;&lt;span style="color:red;"&gt;Source&lt;/span&gt;&lt;span style="color:blue;"&gt;="pack://application:,,,/GridValidation;component/Images/FieldError.png"&lt;br /&gt;                 &lt;/span&gt;&lt;span style="color:red;"&gt;Width&lt;/span&gt;&lt;span style="color:blue;"&gt;="16" &lt;/span&gt;&lt;span style="color:red;"&gt;Height&lt;/span&gt;&lt;span style="color:blue;"&gt;="16" &lt;/span&gt;&lt;span style="color:red;"&gt;Margin&lt;/span&gt;&lt;span style="color:blue;"&gt;="4,0,0,0" &lt;/span&gt;&lt;span style="color:red;"&gt;Visibility&lt;/span&gt;&lt;span style="color:blue;"&gt;="Collapsed" &lt;/span&gt;&lt;span style="color:red;"&gt;ToolTip&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Binding &lt;/span&gt;&lt;span style="color:red;"&gt;Tag&lt;/span&gt;&lt;span style="color:blue;"&gt;}" /&amp;gt;&lt;br /&gt;              &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ControlTemplate.Triggers&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;                  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;DataTrigger &lt;/span&gt;&lt;span style="color:red;"&gt;Binding&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Binding &lt;/span&gt;&lt;span style="color:red;"&gt;Path&lt;/span&gt;&lt;span style="color:blue;"&gt;=Tag, &lt;/span&gt;&lt;span style="color:red;"&gt;Converter&lt;/span&gt;&lt;span style="color:blue;"&gt;={&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StaticResource &lt;/span&gt;&lt;span style="color:red;"&gt;DefaultNullConverter&lt;/span&gt;&lt;span style="color:blue;"&gt;}}" &lt;/span&gt;&lt;span style="color:red;"&gt;Value&lt;/span&gt;&lt;span style="color:blue;"&gt;="False"&amp;gt;&lt;br /&gt;                      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Setter &lt;/span&gt;&lt;span style="color:red;"&gt;TargetName&lt;/span&gt;&lt;span style="color:blue;"&gt;="ErrorIcon" &lt;/span&gt;&lt;span style="color:red;"&gt;Property&lt;/span&gt;&lt;span style="color:blue;"&gt;="Visibility" &lt;/span&gt;&lt;span style="color:red;"&gt;Value&lt;/span&gt;&lt;span style="color:blue;"&gt;="Visible" /&amp;gt;&lt;br /&gt;                  &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;DataTrigger&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;              &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ControlTemplate.Triggers&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;          &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ControlTemplate&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;      &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Setter.Value&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;  &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Setter&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Style&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;One last thing that is remaining, is to connect our attached behavior to our xamDataGrid control. The great thing about the attached behaviors (one of them, at least) is that you can use them from XAML code directly and no code-behind is required at all. So back to our xamDataGrid code, here’s how to do the trick:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;igdp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;XamDataGrid &lt;/span&gt;&lt;span style="color:red;"&gt;DataSource&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Binding &lt;/span&gt;&lt;span style="color:red;"&gt;ProductList&lt;/span&gt;&lt;span style="color:blue;"&gt;}" &lt;/span&gt;&lt;span style="color:red;"&gt;bhv&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;DataGridValidationBehavior.HasRowValidation&lt;/span&gt;&lt;span style="color:blue;"&gt;="true"&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h3&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 128, 255);"&gt;Conclusion&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;The final application looks like this picture. If you try to enter something wrong, either when creating a new item or editing one, validation mechanism will automagically kick in and does the work for you. The very important point is that data is retained in the new line but is it not added to the datasource, because the data is not validated. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Z5KTIfnfuNs/SmcIfVGFI1I/AAAAAAAAAP4/REI4BmqG1C4/s1600-h/xamGrid-Validation%5B7%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="xamGrid-Validation" alt="xamGrid-Validation" src="http://lh5.ggpht.com/_Z5KTIfnfuNs/SmcIhWEs9yI/AAAAAAAAAP8/CMK6zn-7le0/xamGrid-Validation_thumb%5B5%5D.png?imgmax=800" border="0" height="292" width="464" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;You can download the source code from &lt;a href="http://cid-4962b6ceabc2cbd7.skydrive.live.com/self.aspx/BlogFiles/Misc/Infragistics.GridValidation.zip"&gt;here&lt;/a&gt;. In order to fully compile and run the application, you need to install Infragistics WPF components which you can download &lt;a href="http://www.infragistics.com/downloads/default.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-481966823041763779?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/481966823041763779/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=481966823041763779' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/481966823041763779'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/481966823041763779'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/07/xamdatagrid-validation-with-nhibernate.html' title='XamDataGrid Validation with NHibernate Validator'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_Z5KTIfnfuNs/SmcIhWEs9yI/AAAAAAAAAP8/CMK6zn-7le0/s72-c/xamGrid-Validation_thumb%5B5%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-7233172311906810299</id><published>2009-07-13T15:18:00.003+04:30</published><updated>2009-07-21T11:31:05.280+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='WPF'/><category scheme='http://www.blogger.com/atom/ns#' term='Reporting'/><title type='text'>Infragistics WPF Reporting</title><content type='html'>&lt;p&gt;I’ve been looking for flexible Reporting components for the WPF application I’m working on, and strangely enough, there are just a few. I think WPF component in general is still a work in progress, and even if you have the chance to find a working component, there ALL have a lot of short comings and lack design-mode features, developer-friendliness, etc. and when it comes down to reporting component, there is &lt;a href="http://www.infragistics.com/dotnet/netadvantage/wpf/reporting.aspx"&gt;Infragistics&lt;/a&gt; with very basic feature set, &lt;a href="http://www.blogger.com/www.componentone.com"&gt;ComponentOne&lt;/a&gt; with years of background in reporting tools and &lt;a href="http://www.stimulsoft.com/"&gt;Stimulsoft&lt;/a&gt; with complete reporting set for WinForm, WPF and Web. &lt;/p&gt;  &lt;p&gt;So, I start evaluating each one. Stimusoft WPF reporting looked very promising with all bells and whistles like end-user WPF designer and the whole UI (designer and preview) were in WPF, but If I’m going to buy a component I need more that just a reporting tool, how about having bunch of other controls as well? I skipped to ComponentOne and after downloading their package (around 100 MB download), the damn installation did not work and I was staring at the installation error message that had to do something with packaging. So in reality, I had only one more choice : Infragistics. &lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 255);font-size:130%;" &gt;&lt;strong&gt;Infragistics Reporting&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;With very basic feature, Infragistics reporting is also easy to use. Since there is no designer (yet?) you have to code your report and create various report sections (Header, Footer, etc.) and add related stuff into that sections, all in code. The great thing about this reporting control (also a feature of ComponentOne) is that you can print any kind of UIElement, this means you can print your whole UserControl or Form which comes in handy. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Z5KTIfnfuNs/SlsQzr0-TmI/AAAAAAAAAPs/07hfZWtQtKw/s1600-h/Infragistics-Reporting%5B11%5D.jpg"&gt;&lt;img style="border: 0px none ; display: block; float: none; margin-left: auto; margin-right: auto;" title="Infragistics-Reporting" alt="Infragistics-Reporting" src="http://lh4.ggpht.com/_Z5KTIfnfuNs/SlsQ5HsoPII/AAAAAAAAAPw/nyiJgngkmrY/Infragistics-Reporting_thumb%5B9%5D.jpg?imgmax=800" border="0" width="396" height="269" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The real power of Infragistics reporting is when combined with their “Data Presenter” or “XamDataGrid” control. End-user can apply filtering, grouping, sorting, etc. and just print the result, easy as that.&lt;/p&gt;  &lt;p&gt;&lt;span style="color: rgb(0, 128, 255);font-size:130%;" &gt;&lt;strong&gt;RightToLeft Issue&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;When evaluating my application which is a multi-lingual application that supports RightToLeft and LeftToRight reading ordered languages, I came across a problem when printing controls with RightToLeft flow direction. The problem was that the whole control is “Mirrored”. Not that this mirroring is an issue, but all the texts and values printed are also mirrored which makes the whole report useless.&lt;/p&gt;  &lt;p&gt;&lt;img style="border: 0px none ; display: block; float: none; margin-left: auto; margin-right: auto;" title="Mirrorred Report" alt="Mirrorred Report" src="http://lh6.ggpht.com/_Z5KTIfnfuNs/SlsQ74ejcdI/AAAAAAAAAP0/X9Z4uML9wzI/IFG-Mirrorred%20Report_thumb%5B3%5D.jpg?imgmax=800" border="0" width="819" height="113" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="color: rgb(0, 128, 255);font-size:130%;" &gt;&lt;strong&gt;Workaround&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;The workaround is, of course, not to print the control in RightToLeft and use LeftToRight for the time being until the bug is fixed. As easy as this sounds, it is not very easy to do for some reasons:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;RightToLeft is usually set on the parent (Form), and controls just “inherit” the setting from their parent.&lt;/li&gt;    &lt;li&gt;If you do change parent’s RightToLeft end-user will see the effect. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;But you can always clone the object and change the behavior, right? But you can not do so with Visual Elements. There’s no Copy / Clone method on Visual elements in WPF, so how do we “clone” the control, change the FlowDirection, and hand it to the reporting?&lt;/p&gt;  &lt;p&gt;Fortunately, WPF comes with two neglected classes : “XamlWriter” and “XamlReader” each of which do as exactly as the name puts it. Using “XamlWriter” you can write a Visual object (UIElement) into a string, then use “XamlReader” ton construct it back. This is just about the UI, so you’ll have to re-attach the “DataSource” and “DataContext” you might have. Here’s the snippet to do the trick:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;XamDataGrid &lt;/span&gt;&lt;span style="color:maroon;"&gt;Clone&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;XamDataGrid &lt;/span&gt;&lt;span style="color:maroon;"&gt;printable&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:green;"&gt;//Clone the grid control&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;o &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;XamlWriter&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Save&lt;/span&gt;(&lt;span style="color:maroon;"&gt;printable&lt;/span&gt;);&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;sr &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;StringReader&lt;/span&gt;(&lt;span style="color:maroon;"&gt;o&lt;/span&gt;);&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;reader &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;XmlReader&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Create&lt;/span&gt;(&lt;span style="color:maroon;"&gt;sr&lt;/span&gt;, &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;XmlReaderSettings&lt;/span&gt;());&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;result &lt;/span&gt;= (&lt;span style="color: rgb(166, 83, 0);"&gt;XamDataGrid&lt;/span&gt;)&lt;span style="color: rgb(166, 83, 0);"&gt;XamlReader&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Load&lt;/span&gt;(&lt;span style="color:maroon;"&gt;reader&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:green;"&gt;//It's a clone, need to reassign data source&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:maroon;"&gt;result&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DataSource &lt;/span&gt;= &lt;span style="color:maroon;"&gt;printable.DataSource&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:green;"&gt;//Set flow direction to LTR&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:maroon;"&gt;result&lt;/span&gt;.&lt;span style="color:maroon;"&gt;FlowDirection &lt;/span&gt;= &lt;span style="color: rgb(43, 145, 175);"&gt;FlowDirection&lt;/span&gt;.&lt;span style="color:maroon;"&gt;LeftToRight&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;result&lt;/span&gt;;&lt;br /&gt;}&lt;/pre&gt;You can &lt;a href="http://cid-4962b6ceabc2cbd7.skydrive.live.com/self.aspx/BlogFiles/Misc/Infragistics.ReportingRTLDemo.zip"&gt;download&lt;/a&gt; the whole sample that shows the issue and the workaround. To compile and run it you need to have Infragistics 2009.1 installed. You can download Infragistics package &lt;a href="http://www.infragistics.com/downloads/default.aspx"&gt;here&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-7233172311906810299?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/7233172311906810299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=7233172311906810299' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7233172311906810299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7233172311906810299'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/07/infragistics-wpf-reporting.html' title='Infragistics WPF Reporting'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Z5KTIfnfuNs/SlsQ5HsoPII/AAAAAAAAAPw/nyiJgngkmrY/s72-c/Infragistics-Reporting_thumb%5B9%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-6537315962989435480</id><published>2009-07-05T12:07:00.002+04:30</published><updated>2009-07-05T12:21:10.669+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>Additional CallContext information in WCF</title><content type='html'>&lt;p&gt;Yeah, this might be a little old…but someone asked me how to send context information from client to the service with WCF.&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: In general, you should not rely on client provided information about security, authentication, licensing, etc. Use this code and sample as an idea on how to implement your custom security. If you’re trying to send sensitive information over the network, use at your own risk.&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;WCF client and services might be (and probably are) in two separate AppDomain or even on separate machines across the network, so how do we send extra information from the client to the service when the service call is made? Fortunately, WCF provides enough extensibility points for us to participate in the action and change the default behavior. It is pretty much easy to add context information to the message header on the client side, and the on the server side extract the custom data from the message header. Added data to the message header can be a DataContract / Serialized class. &lt;/p&gt;  &lt;pre class="code"&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;DataContract&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;MessageInfo&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color:maroon;"&gt;MessageInfo&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Guid &lt;/span&gt;&lt;span style="color:maroon;"&gt;sessionId&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;CultureInfo &lt;/span&gt;&lt;span style="color:maroon;"&gt;culture&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;requestDate&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;sessionId &lt;/span&gt;= &lt;span style="color:maroon;"&gt;sessionId&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;culture &lt;/span&gt;= &lt;span style="color:maroon;"&gt;culture&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;requestDate &lt;/span&gt;= &lt;span style="color:maroon;"&gt;requestDate&lt;/span&gt;;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [&lt;span style="color: rgb(166, 83, 0);"&gt;DataMember&lt;/span&gt;]&lt;br /&gt;   &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;RequestDate&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;       &lt;span style="color:navy;"&gt;get&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [&lt;span style="color: rgb(166, 83, 0);"&gt;DataMember&lt;/span&gt;]&lt;br /&gt;   &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Guid &lt;/span&gt;&lt;span style="color:maroon;"&gt;SessionId&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;       &lt;span style="color:navy;"&gt;get&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [&lt;span style="color: rgb(166, 83, 0);"&gt;DataMember&lt;/span&gt;]&lt;br /&gt;   &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;CultureInfo &lt;/span&gt;&lt;span style="color:maroon;"&gt;Culture&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;       &lt;span style="color:navy;"&gt;get&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;Client uses IClientMessageInspector to add context data before the message is handed over to WCF for the delivery.&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:gray;"&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &lt;/span&gt;&lt;span style="color:green;"&gt;Flows the client's MessageInfo to the service.&lt;br /&gt;&lt;/span&gt;&lt;span style="color:gray;"&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ClientMessageInfoInspector &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IClientMessageInspector&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;public object &lt;/span&gt;&lt;span style="color:maroon;"&gt;BeforeSendRequest&lt;/span&gt;(&lt;span style="color:navy;"&gt;ref &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Message &lt;/span&gt;&lt;span style="color:maroon;"&gt;request&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;IClientChannel &lt;/span&gt;&lt;span style="color:maroon;"&gt;channel&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:maroon;"&gt;request&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Headers&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;MessageHeader&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CreateHeader&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;MessageHeaderKeys&lt;/span&gt;.&lt;span style="color:maroon;"&gt;MessageInfoKey&lt;/span&gt;, &lt;span style="color:navy;"&gt;string&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Empty&lt;/span&gt;, &lt;span style="color:maroon;"&gt;GetInfo&lt;/span&gt;()));&lt;br /&gt;       &lt;span style="color:navy;"&gt;return null&lt;/span&gt;;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;AfterReceiveReply&lt;/span&gt;(&lt;span style="color:navy;"&gt;ref &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Message &lt;/span&gt;&lt;span style="color:maroon;"&gt;reply&lt;/span&gt;, &lt;span style="color:navy;"&gt;object &lt;/span&gt;&lt;span style="color:maroon;"&gt;correlationState&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;private &lt;/span&gt;&lt;span style="color:maroon;"&gt;MessageInfo GetInfo&lt;/span&gt;()&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;return new &lt;/span&gt;&lt;span style="color:maroon;"&gt;MessageInfo&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Guid&lt;/span&gt;.&lt;span style="color:maroon;"&gt;NewGuid&lt;/span&gt;(), &lt;span style="color: rgb(166, 83, 0);"&gt;Thread&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CurrentThread&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CurrentUICulture&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Now&lt;/span&gt;);&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;On the other hand, the service side uses IDispatchMessageInspector interface to extract the data from message header.&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:gray;"&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &lt;/span&gt;&lt;span style="color:green;"&gt;Reads the MessageInfo of client and updates the service.&lt;br /&gt;&lt;/span&gt;&lt;span style="color:gray;"&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ServiceMessageInfoInspector &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IDispatchMessageInspector&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;public object &lt;/span&gt;&lt;span style="color:maroon;"&gt;AfterReceiveRequest&lt;/span&gt;(&lt;span style="color:navy;"&gt;ref &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Message &lt;/span&gt;&lt;span style="color:maroon;"&gt;request&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;IClientChannel &lt;/span&gt;&lt;span style="color:maroon;"&gt;channel&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;InstanceContext &lt;/span&gt;&lt;span style="color:maroon;"&gt;instanceContext&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;int &lt;/span&gt;&lt;span style="color:maroon;"&gt;headerIndex &lt;/span&gt;= &lt;span style="color:maroon;"&gt;request&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Headers&lt;/span&gt;.&lt;span style="color:maroon;"&gt;FindHeader&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;MessageHeaderKeys&lt;/span&gt;.&lt;span style="color:maroon;"&gt;MessageInfoKey&lt;/span&gt;, &lt;span style="color:navy;"&gt;string&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Empty&lt;/span&gt;);&lt;br /&gt;       &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;headerIndex &lt;/span&gt;!= -&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;1&lt;/span&gt;)&lt;br /&gt;       {&lt;br /&gt;           &lt;span style="color:maroon;"&gt;MessageInfo ui &lt;/span&gt;= &lt;span style="color:maroon;"&gt;request&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Headers&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetHeader&lt;/span&gt;&amp;lt;&lt;span style="color:maroon;"&gt;MessageInfo&lt;/span&gt;&amp;gt;(&lt;span style="color:maroon;"&gt;headerIndex&lt;/span&gt;);&lt;br /&gt;          &lt;br /&gt;           &lt;span style="color: rgb(166, 83, 0);"&gt;Thread&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CurrentThread&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CurrentUICulture &lt;/span&gt;= &lt;span style="color:maroon;"&gt;ui&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Culture&lt;/span&gt;;&lt;br /&gt;           &lt;span style="color: rgb(166, 83, 0);"&gt;Thread&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CurrentThread&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CurrentCulture &lt;/span&gt;= &lt;span style="color:maroon;"&gt;ui&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Culture&lt;/span&gt;;&lt;/pre&gt;&lt;pre class="code"&gt;            &lt;span style="color: rgb(166, 83, 0);"&gt;Console&lt;/span&gt;.&lt;span style="color:maroon;"&gt;WriteLine&lt;/span&gt;("Request recieved on {0}", &lt;span style="color:maroon;"&gt;ui&lt;/span&gt;.&lt;span style="color:maroon;"&gt;RequestDate&lt;/span&gt;);&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       &lt;span style="color:navy;"&gt;return null&lt;/span&gt;;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;BeforeSendReply&lt;/span&gt;(&lt;span style="color:navy;"&gt;ref &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Message &lt;/span&gt;&lt;span style="color:maroon;"&gt;reply&lt;/span&gt;, &lt;span style="color:navy;"&gt;object &lt;/span&gt;&lt;span style="color:maroon;"&gt;correlationState&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;To wire things up, you also need to create a new behavior by implementing IEndpointBehavior and registering it with your endpoint, either by code, or configuration. Should you use a single behavior for both client and server, here’s how to do it:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;MessageInfoBehavior &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IEndpointBehavior&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Validate&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ServiceEndpoint &lt;/span&gt;&lt;span style="color:maroon;"&gt;endpoint&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;AddBindingParameters&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ServiceEndpoint &lt;/span&gt;&lt;span style="color:maroon;"&gt;endpoint&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;BindingParameterCollection &lt;/span&gt;&lt;span style="color:maroon;"&gt;bindingParameters&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;ApplyDispatchBehavior&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ServiceEndpoint &lt;/span&gt;&lt;span style="color:maroon;"&gt;endpoint&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;EndpointDispatcher &lt;/span&gt;&lt;span style="color:maroon;"&gt;endpointDispatcher&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color: rgb(166, 83, 0);"&gt;ServiceMessageInfoInspector &lt;/span&gt;&lt;span style="color:maroon;"&gt;inspector &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ServiceMessageInfoInspector&lt;/span&gt;();&lt;br /&gt;       &lt;span style="color:maroon;"&gt;endpointDispatcher&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DispatchRuntime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;MessageInspectors&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;(&lt;span style="color:maroon;"&gt;inspector&lt;/span&gt;);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;ApplyClientBehavior&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ServiceEndpoint &lt;/span&gt;&lt;span style="color:maroon;"&gt;endpoint&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;ClientRuntime &lt;/span&gt;&lt;span style="color:maroon;"&gt;clientRuntime&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:maroon;"&gt;ClientMessageInfoInspector inspector &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:maroon;"&gt;ClientMessageInfoInspector&lt;/span&gt;();&lt;br /&gt;       &lt;span style="color:maroon;"&gt;clientRuntime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;MessageInspectors&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;(&lt;span style="color:maroon;"&gt;inspector&lt;/span&gt;);&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;Hope this helps.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-6537315962989435480?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/6537315962989435480/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=6537315962989435480' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6537315962989435480'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6537315962989435480'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/07/additional-callcontext-information-in.html' title='Additional CallContext information in WCF'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-6216014571114460154</id><published>2009-06-23T10:45:00.002+04:30</published><updated>2009-06-23T12:53:30.670+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET MVC'/><title type='text'>File Download with ASP.NET MVC</title><content type='html'>&lt;p&gt;One of the things that might be less obvious when doing applications with ASP.NET MVC, is the ability to download files without exposing your files directly and possibly applying other mechanisms such as authentication or registering emails before allowing someone download your files. This is pretty much easy. The “Controller” class has a File method which returns a “FilePathResult”.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color:#a65300;"&gt;DownloadsController &lt;/span&gt;: &lt;span style="color:#a65300;"&gt;Controller&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;    &lt;span style="color:navy;"&gt;private readonly &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IProductService &lt;/span&gt;&lt;span style="color:maroon;"&gt;_productService&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color:maroon;"&gt;DownloadsController&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IProductService &lt;/span&gt;&lt;span style="color:maroon;"&gt;productService&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:maroon;"&gt;_productService &lt;/span&gt;= &lt;span style="color:maroon;"&gt;productService&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [&lt;span style="color:#a65300;"&gt;SessionPerRequest&lt;/span&gt;]&lt;br /&gt;    &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color:#a65300;"&gt;ActionResult &lt;/span&gt;&lt;span style="color:maroon;"&gt;Index&lt;/span&gt;()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;products &lt;/span&gt;= &lt;span style="color:maroon;"&gt;_productService&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetDownloadableProducts&lt;/span&gt;();&lt;br /&gt;        &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;model &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:#a65300;"&gt;DownloadViewModel&lt;/span&gt;(&lt;span style="color:maroon;"&gt;products&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;View&lt;/span&gt;(&lt;span style="color:maroon;"&gt;model&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [&lt;span style="color:#a65300;"&gt;SessionPerRequest&lt;/span&gt;]&lt;br /&gt;    &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color:#a65300;"&gt;ActionResult &lt;/span&gt;&lt;span style="color:maroon;"&gt;File&lt;/span&gt;(&lt;span style="color:navy;"&gt;int &lt;/span&gt;&lt;span style="color:maroon;"&gt;id&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;download &lt;/span&gt;= &lt;span style="color:maroon;"&gt;_productService&lt;/span&gt;.&lt;span style="color:maroon;"&gt;FindDownload&lt;/span&gt;(&lt;span style="color:maroon;"&gt;id&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;download &lt;/span&gt;!= &lt;span style="color:navy;"&gt;null&lt;/span&gt;)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;path &lt;/span&gt;= &lt;span style="color:maroon;"&gt;Server&lt;/span&gt;.&lt;span style="color:maroon;"&gt;MapPath&lt;/span&gt;(&lt;span style="color:maroon;"&gt;download&lt;/span&gt;.&lt;span style="color:maroon;"&gt;URL&lt;/span&gt;);&lt;br /&gt;            &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;file &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:#a65300;"&gt;FileInfo&lt;/span&gt;(&lt;span style="color:maroon;"&gt;path&lt;/span&gt;);&lt;br /&gt;            &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;file&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Exists&lt;/span&gt;)&lt;br /&gt;            {&lt;br /&gt;                &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;File&lt;/span&gt;(&lt;span style="color:maroon;"&gt;download&lt;/span&gt;.&lt;span style="color:maroon;"&gt;URL&lt;/span&gt;, "application/binary", &lt;span style="color:maroon;"&gt;download&lt;/span&gt;.&lt;span style="color:maroon;"&gt;FileName&lt;/span&gt;);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;RedirectToAction&lt;/span&gt;("Index");&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;table&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &lt;br /&gt;    &lt;/span&gt;&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color:navy;"&gt;foreach &lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;p &lt;/span&gt;&lt;span style="color:navy;"&gt;in &lt;/span&gt;&lt;span style="color:maroon;"&gt;Model&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Products&lt;/span&gt;) { &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;         &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;tr&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;td&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;             &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;strong&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Title &lt;/span&gt;&lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;strong&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;br &lt;/span&gt;&lt;span style="color:blue;"&gt;/&amp;gt;&lt;br /&gt;              &lt;br /&gt;             &lt;/span&gt;&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:navy;"&gt;foreach&lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;d &lt;/span&gt;&lt;span style="color:navy;"&gt;in &lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Downloads&lt;/span&gt;) { &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;               &lt;br /&gt;                &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;a &lt;/span&gt;&lt;span style="color:red;"&gt;href&lt;/span&gt;&lt;span style="color:blue;"&gt;="/Downloads/File/&lt;/span&gt;&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;= d.Id &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;"&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;                   &lt;/span&gt;Version: &lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:maroon;"&gt;d&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Version &lt;/span&gt;&lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;- &lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:maroon;"&gt;d&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DownloadSize &lt;/span&gt;&lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt; KB&lt;br /&gt;                &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;a&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;                &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;br &lt;/span&gt;&lt;span style="color:blue;"&gt;/&amp;gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;} &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;               &lt;br /&gt;           &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;td&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;tr&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; } &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;    &lt;br /&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;table&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-6216014571114460154?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/6216014571114460154/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=6216014571114460154' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6216014571114460154'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6216014571114460154'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/06/file-download-with-aspnet-mvc.html' title='File Download with ASP.NET MVC'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-2966809943747524719</id><published>2009-05-26T10:50:00.003+04:30</published><updated>2009-05-26T10:56:56.475+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='NHibernate'/><title type='text'>NHibernate JetDriver : Another Issue</title><content type='html'>&lt;p&gt;There is another issue with NHibernate and JetDriver I encountered yesterday. It seems that a select query containing “Order By”, will gets its “From” keyword cut off at some stage by the Jet Driver. It means simple object querying won’t work:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;Account&lt;/span&gt;&amp;gt; &lt;span style="color:maroon;"&gt;GetAllAccounts&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;UnitOfWork&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Start&lt;/span&gt;())&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;repository &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;NHRepository&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;Account&lt;/span&gt;&amp;gt;();&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;orders &lt;/span&gt;= &lt;span style="color:navy;"&gt;new&lt;/span&gt;[] { &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Order&lt;/span&gt;("Firstname", &lt;span style="color:navy;"&gt;true&lt;/span&gt;), &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Order&lt;/span&gt;("Lastname", &lt;span style="color:navy;"&gt;true&lt;/span&gt;) };&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;repository&lt;/span&gt;.&lt;span style="color:maroon;"&gt;FindAll&lt;/span&gt;(&lt;span style="color:maroon;"&gt;orders&lt;/span&gt;).&lt;span style="color:maroon;"&gt;ToList&lt;/span&gt;();&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;To fix it, let’s first pin point the problem by creating a failing test. It was a good thing I was already running everything from the trunk build, so I opened up the JetDriver project (from NHContrib) and luckily there is already a test project. Note that, since I didn’t want to actually change any object / method visibility, I used reflection to call the private methods that fails this test:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;TestFixture&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;JetOrderByFixture&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;  [&lt;span style="color: rgb(43, 145, 175);"&gt;Test&lt;/span&gt;]&lt;br /&gt;  &lt;span style="color:blue;"&gt;public void &lt;/span&gt;Select_Statement_With_OrderBy_Should_Run()&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color: rgb(43, 145, 175);"&gt;SqlString &lt;/span&gt;sql = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SqlString&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"SELECT * FROM ACCOUNTS ORDER BY Firstname ASC, Lastname ASC"&lt;/span&gt;);&lt;br /&gt;      &lt;span style="color: rgb(43, 145, 175);"&gt;JetDriver &lt;/span&gt;driver = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;JetDriver&lt;/span&gt;();&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: rgb(43, 145, 175);"&gt;SqlString &lt;/span&gt;processedSql = driver.GetType()&lt;br /&gt;                                     .GetMethod(&lt;span style="color: rgb(163, 21, 21);"&gt;"FinalizeJoins"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;BindingFlags&lt;/span&gt;.NonPublic | &lt;span style="color: rgb(43, 145, 175);"&gt;BindingFlags&lt;/span&gt;.Instance | &lt;span style="color: rgb(43, 145, 175);"&gt;BindingFlags&lt;/span&gt;.InvokeMethod)&lt;br /&gt;                                     .Invoke(driver, &lt;span style="color:blue;"&gt;new object&lt;/span&gt;[] { sql }) &lt;span style="color:blue;"&gt;as &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SqlString&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.That(processedSql, &lt;span style="color: rgb(43, 145, 175);"&gt;Is&lt;/span&gt;.Not.Null);&lt;br /&gt;      &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.That(processedSql.LastIndexOfCaseInsensitive(&lt;span style="color: rgb(163, 21, 21);"&gt;"FROM"&lt;/span&gt;), &lt;span style="color: rgb(43, 145, 175);"&gt;Is&lt;/span&gt;.GreaterThan(0));&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;The problem is the private FinalizeJoins method, so the test, to no surprise, fails. I have created a &lt;a href="http://nhjira.koah.net/browse/NHCD-33"&gt;task&lt;/a&gt; in NHibernate’s Jira and the patch to fix this is attached, if you need it.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-2966809943747524719?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/2966809943747524719/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=2966809943747524719' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2966809943747524719'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2966809943747524719'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/05/nhibernate-jetdriver-another-issue.html' title='NHibernate JetDriver : Another Issue'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-2772923857086647687</id><published>2009-05-21T13:31:00.001+04:30</published><updated>2009-05-21T13:31:20.029+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UnitTest'/><category scheme='http://www.blogger.com/atom/ns#' term='Testing'/><title type='text'>Unit Testing with TypeMock</title><content type='html'>&lt;p&gt;&lt;a href="http://www.typemock.com/"&gt;Unit Testing&lt;/a&gt; ASP.NET? &lt;a href="http://www.typemock.com/ASP.NET_unit_testing_page.php"&gt;ASP.NET unit testing&lt;/a&gt; has never been this easy. &lt;/p&gt;  &lt;p&gt;Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle - and for the launch will be giving out FREE licenses to bloggers and their readers. &lt;/p&gt;  &lt;p&gt;The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both &lt;a href="http://www.typemock.com/"&gt;Typemock Isolator&lt;/a&gt;, a &lt;a href="http://www.typemock.com/"&gt;unit test&lt;/a&gt; tool and &lt;a href="http://sm-art.biz/Ivonna.aspx"&gt;Ivonna&lt;/a&gt;, the Isolator add-on for &lt;a href="http://sm-art.biz/Ivonna.aspx"&gt;ASP.NET unit testing&lt;/a&gt;, for a bargain price. &lt;/p&gt;  &lt;p&gt;Typemock Isolator is a leading &lt;a href="http://www.typemock.com/"&gt;.NET unit testing&lt;/a&gt; tool (C# and VB.NET) for many ‘hard to test’ technologies such as &lt;a href="http://typemock.com/sharepointpage.php"&gt;SharePoint&lt;/a&gt;, &lt;a href="http://www.typemock.com/ASP.NET_unit_testing_page.php"&gt;ASP.NET&lt;/a&gt;, &lt;a href="http://www.typemock.com/ASP.NET_unit_testing_page.php"&gt;MVC&lt;/a&gt;, &lt;a href="http://www.typemock.com/wcfpage.php"&gt;WCF&lt;/a&gt;, WPF, &lt;a href="http://www.typemock.com/Silverlight_unit_testing_page.php"&gt;Silverlight&lt;/a&gt; and more. Note that for &lt;a href="http://www.typemock.com/Silverlight_unit_testing_page.php"&gt;unit testing Silverlight&lt;/a&gt; there is an open source Isolator add-on called &lt;a href="http://www.typemock.com/Silverlight_unit_testing_page.php"&gt;SilverUnit&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;The first 60 bloggers who will blog this text in their blog and &lt;a href="http://blog.typemock.com/2009/05/get-free-typemock-licenses-aspnet.html"&gt;tell us about it&lt;/a&gt;, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you'll get a license automatically (even if more than 60 submit) during the first week of this announcement. &lt;/p&gt;  &lt;p&gt;Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends. &lt;/p&gt;  &lt;p&gt;Go ahead, click the following link for &lt;a href="http://blog.typemock.com/2009/05/get-free-typemock-licenses-aspnet.html"&gt;more information&lt;/a&gt; on how to get your free license.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-2772923857086647687?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/2772923857086647687/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=2772923857086647687' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2772923857086647687'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2772923857086647687'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/05/unit-testing-with-typemock.html' title='Unit Testing with TypeMock'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-9051281506640760380</id><published>2009-05-20T16:20:00.003+04:30</published><updated>2009-05-21T12:35:18.587+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='NHibernate'/><title type='text'>NHibernate and DateTime</title><content type='html'>&lt;p&gt;I’ve been working on a WPF application which is maybe another post. The work is done and I have started localizing the applications. Localization was mostly chanding the FlowDirection and lables / texts for other languages, but when testing the application, I came across an intresting problem with Nhibernate. The application worked correctly in default (en-us) culture, but not in fa-ir, as NHibernate reported problems when running the working query. Here’s the problem:&lt;/p&gt;    &lt;pre class="code"&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;Test&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Can_Run_Query_In_Persian_Culture&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;using&lt;/span&gt;(&lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;CultureContext&lt;/span&gt;(&lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;PersianCultureInfo&lt;/span&gt;()))&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;UnitOfWork&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Start&lt;/span&gt;())&lt;br /&gt;      {&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;todayStart &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Today&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Year&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Today&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Month&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Today&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Day&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;CultureHelper&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DefaultCalendar&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;todayEnd &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Today&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Year&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Today&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Month&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Today&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Day&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;23&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;59&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;59&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;CultureHelper&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DefaultCalendar&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;queryable &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;UnitOfWork&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CurrentSession&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Linq&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;Purchase&lt;/span&gt;&amp;gt;();&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;query &lt;/span&gt;= &lt;span style="color:navy;"&gt;from &lt;/span&gt;&lt;span style="color:maroon;"&gt;sales &lt;/span&gt;&lt;span style="color:navy;"&gt;in &lt;/span&gt;&lt;span style="color:maroon;"&gt;queryable&lt;br /&gt;                      &lt;/span&gt;&lt;span style="color:navy;"&gt;where &lt;/span&gt;&lt;span style="color:maroon;"&gt;sales&lt;/span&gt;.&lt;span style="color:maroon;"&gt;PurchaseDate &lt;/span&gt;&amp;gt;= &lt;span style="color:maroon;"&gt;todayStart &lt;/span&gt;&amp;amp;&amp;amp; &lt;span style="color:maroon;"&gt;sales&lt;/span&gt;.&lt;span style="color:maroon;"&gt;PurchaseDate &lt;/span&gt;&amp;lt;= &lt;span style="color:maroon;"&gt;todayEnd&lt;br /&gt;                      &lt;/span&gt;&lt;span style="color:navy;"&gt;select &lt;/span&gt;&lt;span style="color:maroon;"&gt;sales&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;          &lt;span style="color: rgb(166, 83, 0);"&gt;Assert&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DoesNotThrow&lt;/span&gt;(() =&amp;gt; &lt;span style="color:maroon;"&gt;query&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ToList&lt;/span&gt;());&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;The test which is totally working on other cultures, fails miserably in fa-ir. The reason is somewhere in the criteria parameter being converted to string. Here’s the output of the query being run on the database:&lt;br /&gt;&lt;p&gt;[SELECT this_.PurchaseId as PurchaseId4_0_, this_.PaidValue as PaidValue4_0_, this_.PurchaseDate as Purchase3_4_0_, this_.Quantity as Quantity4_0_, this_.IsOwed as IsOwed4_0_, this_.Product_id as Product6_4_0_, this_.PurchaseRegisteredBy_id as Purchase7_4_0_, this_.Purchaser_id as Purchaser8_4_0_ from `Purchase` this_ WHERE (this_.PurchaseDate &amp;gt;= ? and this_.PurchaseDate &amp;lt;= ?) ]&lt;br /&gt;&lt;br /&gt;Positional parameters:  #0&amp;gt;1388/02/30 12:00:00 ق.ظ #1&amp;gt;1388/02/30 11:59:59 ب.ظ&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;It is obvious that date values are converted to String using current Culture of the running thread, while the conversion should use invariant culture instead:&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;WRONG:&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;dateValue &lt;/span&gt;= &lt;span style="color:maroon;"&gt;todayStart&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ToString&lt;/span&gt;();&lt;/pre&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;RIGHT:&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;dateValue &lt;/span&gt;= &lt;span style="color:maroon;"&gt;todayStart&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ToString&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;CultureInfo&lt;/span&gt;.&lt;span style="color:maroon;"&gt;InvariantCulture&lt;/span&gt;);&lt;/pre&gt;Any workaround for this? I haven’t found any yet. In fact I still do not exactly know if this is NH core problem or the Linq extensions and since I’m running from the trunk, I’m not sure if this is reproducible on released versions.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;Update : The problem was actually related to JET Driver of NHibernate Contrib project. A patch is created and sent to authorities.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-9051281506640760380?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/9051281506640760380/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=9051281506640760380' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/9051281506640760380'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/9051281506640760380'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/05/nhibernate-and-datetime.html' title='NHibernate and DateTime'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-1942645402685796229</id><published>2009-05-16T13:19:00.003+04:30</published><updated>2009-05-16T14:04:13.680+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='MVVM'/><category scheme='http://www.blogger.com/atom/ns#' term='WPF'/><category scheme='http://www.blogger.com/atom/ns#' term='NHibernate Validator'/><title type='text'>MVVM Validation With NHibernate Validator</title><content type='html'>&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Z5KTIfnfuNs/Sg5-Fzj07XI/AAAAAAAAAOk/0ca2oK3AUjs/s1600-h/MVVM-Validation%5B7%5D.png"&gt;&lt;img style="border: 0px none ; margin: 5px 20px 0px 10px; display: inline;" title="MVVM-Validation" alt="MVVM-Validation" src="http://lh4.ggpht.com/_Z5KTIfnfuNs/Sg5-LYic86I/AAAAAAAAAOo/YhoCqfl99Bk/MVVM-Validation_thumb%5B5%5D.png?imgmax=800" border="0" height="150" width="294" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Simplest way to do Validation in WPF is usually implementing IDataErrorInfo interface, and do the validation in the indexer’s getter. It turns out to be ugly and gets out of hand when your model gets a little larger. Implementing the IDataErrorInfo is as simple as this:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public string this&lt;/span&gt;[&lt;span style="color:navy;"&gt;string &lt;/span&gt;&lt;span style="color:maroon;"&gt;propertyName&lt;/span&gt;]&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;get&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:green;"&gt;//Validate property that is being set,&lt;br /&gt;      //and return an error message if there's&lt;br /&gt;      //any error.&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:navy;"&gt;return null&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Error&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;get &lt;/span&gt;{ &lt;span style="color:navy;"&gt;return string&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Empty&lt;/span&gt;; }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;I intend to use NHibernate Validator attributes to add non-intrusive validation to my ViewModel classes. So you’d just decorate properties of your ViewModel and let WPF and NHibernate Validator do the rest for you. Before we do that, let’s create a more elegant way to show the errors by restyling the TextBox control and add an Error Icon and tooltip to it in case of an error existing:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Style &lt;/span&gt;&lt;span style="color:red;"&gt;TargetType&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Type &lt;/span&gt;&lt;span style="color:red;"&gt;TextBox&lt;/span&gt;&lt;span style="color:blue;"&gt;}"&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Setter &lt;/span&gt;&lt;span style="color:red;"&gt;Property&lt;/span&gt;&lt;span style="color:blue;"&gt;="Validation.ErrorTemplate"&amp;gt;&lt;br /&gt;       &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Setter.Value&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ControlTemplate&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;               &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;DockPanel &lt;/span&gt;&lt;span style="color:red;"&gt;LastChildFill&lt;/span&gt;&lt;span style="color:blue;"&gt;="True"&amp;gt;&lt;br /&gt;                   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Image &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Name&lt;/span&gt;&lt;span style="color:blue;"&gt;="ErrorIcon" &lt;/span&gt;&lt;span style="color:red;"&gt;Source&lt;/span&gt;&lt;span style="color:blue;"&gt;="Images/FieldError.png" &lt;/span&gt;&lt;span style="color:red;"&gt;DockPanel.Dock&lt;/span&gt;&lt;span style="color:blue;"&gt;="Right" &lt;/span&gt;&lt;span style="color:red;"&gt;Width&lt;/span&gt;&lt;span style="color:blue;"&gt;="16" &lt;/span&gt;&lt;span style="color:red;"&gt;Height&lt;/span&gt;&lt;span style="color:blue;"&gt;="16" &lt;/span&gt;&lt;span style="color:red;"&gt;Margin&lt;/span&gt;&lt;span style="color:blue;"&gt;="4,0,0,0" /&amp;gt;&lt;br /&gt;                   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Border &lt;/span&gt;&lt;span style="color:red;"&gt;BorderBrush&lt;/span&gt;&lt;span style="color:blue;"&gt;="Red" &lt;/span&gt;&lt;span style="color:red;"&gt;BorderThickness&lt;/span&gt;&lt;span style="color:blue;"&gt;="1"&amp;gt;&lt;br /&gt;                       &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;AdornedElementPlaceholder &lt;/span&gt;&lt;span style="color:blue;"&gt;/&amp;gt;&lt;br /&gt;                   &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Border&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;               &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;DockPanel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;           &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ControlTemplate&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;       &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Setter.Value&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Setter&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Style.Triggers&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;       &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Trigger &lt;/span&gt;&lt;span style="color:red;"&gt;Property&lt;/span&gt;&lt;span style="color:blue;"&gt;="Validation.HasError" &lt;/span&gt;&lt;span style="color:red;"&gt;Value&lt;/span&gt;&lt;span style="color:blue;"&gt;="true"&amp;gt;&lt;br /&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Setter &lt;/span&gt;&lt;span style="color:red;"&gt;Property&lt;/span&gt;&lt;span style="color:blue;"&gt;="ToolTip" &lt;/span&gt;&lt;span style="color:red;"&gt;Value&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Binding &lt;/span&gt;&lt;span style="color:red;"&gt;RelativeSource&lt;/span&gt;&lt;span style="color:blue;"&gt;={&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;RelativeSource &lt;/span&gt;&lt;span style="color:red;"&gt;Self&lt;/span&gt;&lt;span style="color:blue;"&gt;}, &lt;/span&gt;&lt;span style="color:red;"&gt;Path&lt;/span&gt;&lt;span style="color:blue;"&gt;=(Validation.Errors)[&lt;/span&gt;0&lt;span style="color:blue;"&gt;].ErrorContent}"/&amp;gt;&lt;br /&gt;       &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Trigger&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Style.Triggers&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Style&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;Now that styles are all set, let’s configure NHibenrate Validator’s engine on application startup. Nothing fancy, just straightforeward configuration. We’ll use a shared validation engine:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;RegisterValidatorEngine&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;config &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;NHVConfigurationBase&lt;/span&gt;();&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:maroon;"&gt;config&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Properties&lt;/span&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ApplyToDDL&lt;/span&gt;] = "false";&lt;br /&gt;  &lt;span style="color:maroon;"&gt;config&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Properties&lt;/span&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;AutoregisterListeners&lt;/span&gt;] = "true";&lt;br /&gt;  &lt;span style="color:maroon;"&gt;config&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Properties&lt;/span&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ValidatorMode&lt;/span&gt;] = "UseAttribute";&lt;br /&gt;  &lt;span style="color:maroon;"&gt;config&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Properties&lt;/span&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SharedEngineClass&lt;/span&gt;] = &lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ValidatorEngine&lt;/span&gt;).&lt;span style="color:maroon;"&gt;FullName&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color:maroon;"&gt;config&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Mappings&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;(&lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;MappingConfiguration&lt;/span&gt;(&lt;span style="color:maroon;"&gt;DomainAssemblyName&lt;/span&gt;, &lt;span style="color:navy;"&gt;null&lt;/span&gt;));&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SharedEngineProvider &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;NHibernateSharedEngineProvider&lt;/span&gt;();&lt;br /&gt;  &lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SharedEngineProvider&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetEngine&lt;/span&gt;().&lt;span style="color:maroon;"&gt;Configure&lt;/span&gt;(&lt;span style="color:maroon;"&gt;config&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(166, 83, 0);"&gt;ValidatorInitializer&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Initialize&lt;/span&gt;(&lt;span style="color:maroon;"&gt;NHibernateConfig&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;And to make things reusable, why not create a base ViewModel:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public abstract class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ValidatableViewModel &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IDataErrorInfo&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;private readonly &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ValidatorEngine &lt;/span&gt;&lt;span style="color:maroon;"&gt;validation&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;protected &lt;/span&gt;&lt;span style="color:maroon;"&gt;ValidatableViewModel&lt;/span&gt;()&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:maroon;"&gt;validation &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SharedEngineProvider&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetEngine&lt;/span&gt;();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public string this&lt;/span&gt;[&lt;span style="color:navy;"&gt;string &lt;/span&gt;&lt;span style="color:maroon;"&gt;propertyName&lt;/span&gt;]&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;get&lt;br /&gt;      &lt;/span&gt;{&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;rules &lt;/span&gt;= &lt;span style="color:maroon;"&gt;GetInvalidRules&lt;/span&gt;(&lt;span style="color:maroon;"&gt;propertyName&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;rules &lt;/span&gt;!= &lt;span style="color:navy;"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; &lt;span style="color:maroon;"&gt;rules&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Count &lt;/span&gt;&amp;gt; &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;)&lt;br /&gt;          {&lt;br /&gt;              &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;rules&lt;/span&gt;[&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;].&lt;span style="color:maroon;"&gt;Message&lt;/span&gt;;&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;          &lt;span style="color:navy;"&gt;return null&lt;/span&gt;;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Error&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get &lt;/span&gt;{ &lt;span style="color:navy;"&gt;return string&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Empty&lt;/span&gt;; }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;InvalidValue&lt;/span&gt;&amp;gt; &lt;span style="color:maroon;"&gt;GetInvalidRules&lt;/span&gt;(&lt;span style="color:navy;"&gt;string &lt;/span&gt;&lt;span style="color:maroon;"&gt;propertyName&lt;/span&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;type &lt;/span&gt;= &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetType&lt;/span&gt;();&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;validation&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ValidatePropertyValue&lt;/span&gt;(&lt;span style="color:maroon;"&gt;type&lt;/span&gt;, &lt;span style="color:maroon;"&gt;propertyName&lt;/span&gt;, &lt;span style="color:maroon;"&gt;GetPropertyValue&lt;/span&gt;(&lt;span style="color:maroon;"&gt;type&lt;/span&gt;, &lt;span style="color:maroon;"&gt;propertyName&lt;/span&gt;));&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;InvalidValue&lt;/span&gt;&amp;gt; &lt;span style="color:maroon;"&gt;GetAllInvalidRules&lt;/span&gt;()&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;validation&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Validate&lt;/span&gt;(&lt;span style="color:navy;"&gt;this&lt;/span&gt;);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;private object &lt;/span&gt;&lt;span style="color:maroon;"&gt;GetPropertyValue&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Type &lt;/span&gt;&lt;span style="color:maroon;"&gt;objectType&lt;/span&gt;, &lt;span style="color:navy;"&gt;string &lt;/span&gt;&lt;span style="color:maroon;"&gt;properyName&lt;/span&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;objectType&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetProperty&lt;/span&gt;(&lt;span style="color:maroon;"&gt;properyName&lt;/span&gt;).&lt;span style="color:maroon;"&gt;GetValue&lt;/span&gt;(&lt;span style="color:navy;"&gt;this&lt;/span&gt;, &lt;span style="color:navy;"&gt;null&lt;/span&gt;);&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;The rest is just to inherit the ValidatableViewModel and add necessary attributes to our binded properties. A sample ViewModel containing a Save command, which is invocable only when there’s no error on the model and a couple of other business properties would look like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;NewAccountViewModel &lt;/span&gt;: &lt;span style="color: rgb(166, 83, 0);"&gt;ValidatableViewModel&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;INotifyPropertyChanged&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;private string &lt;/span&gt;&lt;span style="color:maroon;"&gt;_firstName&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color:navy;"&gt;private string &lt;/span&gt;&lt;span style="color:maroon;"&gt;_lastName&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color:navy;"&gt;private string &lt;/span&gt;&lt;span style="color:maroon;"&gt;_currentBalance&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  [&lt;span style="color: rgb(166, 83, 0);"&gt;NotNullNotEmpty&lt;/span&gt;]&lt;br /&gt;  &lt;span style="color:navy;"&gt;public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Firstname&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get &lt;/span&gt;{ &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;_firstName&lt;/span&gt;; }&lt;br /&gt;      &lt;span style="color:navy;"&gt;set&lt;br /&gt;      &lt;/span&gt;{&lt;br /&gt;          &lt;span style="color:maroon;"&gt;_firstName &lt;/span&gt;= &lt;span style="color:navy;"&gt;value&lt;/span&gt;;&lt;br /&gt;          &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Notify&lt;/span&gt;(&lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;PropertyChanged&lt;/span&gt;, &lt;span style="color:maroon;"&gt;o &lt;/span&gt;=&amp;gt; &lt;span style="color:maroon;"&gt;o&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Firstname&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  [&lt;span style="color: rgb(166, 83, 0);"&gt;NotNullNotEmpty&lt;/span&gt;]&lt;br /&gt;  &lt;span style="color:navy;"&gt;public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Lastname&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get &lt;/span&gt;{ &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;_lastName&lt;/span&gt;; }&lt;br /&gt;      &lt;span style="color:navy;"&gt;set&lt;br /&gt;      &lt;/span&gt;{&lt;br /&gt;          &lt;span style="color:maroon;"&gt;_lastName &lt;/span&gt;= &lt;span style="color:navy;"&gt;value&lt;/span&gt;;&lt;br /&gt;          &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Notify&lt;/span&gt;(&lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;PropertyChanged&lt;/span&gt;, &lt;span style="color:maroon;"&gt;o &lt;/span&gt;=&amp;gt; &lt;span style="color:maroon;"&gt;o&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Lastname&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  [&lt;span style="color: rgb(166, 83, 0);"&gt;IsNumeric&lt;/span&gt;]&lt;br /&gt;  [&lt;span style="color: rgb(166, 83, 0);"&gt;NotNullNotEmpty&lt;/span&gt;]&lt;br /&gt;  &lt;span style="color:navy;"&gt;public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;CurrentBalance&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get &lt;/span&gt;{ &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;_currentBalance&lt;/span&gt;; }&lt;br /&gt;      &lt;span style="color:navy;"&gt;set&lt;br /&gt;      &lt;/span&gt;{&lt;br /&gt;          &lt;span style="color:maroon;"&gt;_currentBalance &lt;/span&gt;= &lt;span style="color:navy;"&gt;value&lt;/span&gt;;&lt;br /&gt;          &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Notify&lt;/span&gt;(&lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;PropertyChanged&lt;/span&gt;, &lt;span style="color:maroon;"&gt;o &lt;/span&gt;=&amp;gt; &lt;span style="color:maroon;"&gt;o&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CurrentBalance&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public event &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyChangedEventHandler &lt;/span&gt;&lt;span style="color:maroon;"&gt;PropertyChanged &lt;/span&gt;= &lt;span style="color:navy;"&gt;delegate &lt;/span&gt;{ };&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;private &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ICommand &lt;/span&gt;&lt;span style="color:maroon;"&gt;_cmdSave&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ICommand &lt;/span&gt;&lt;span style="color:maroon;"&gt;SaveCommand&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get&lt;br /&gt;      &lt;/span&gt;{&lt;br /&gt;          &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;_cmdSave &lt;/span&gt;== &lt;span style="color:navy;"&gt;null&lt;/span&gt;)&lt;br /&gt;              &lt;span style="color:maroon;"&gt;_cmdSave &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;RelayCommand&lt;/span&gt;(&lt;span style="color:maroon;"&gt;Save&lt;/span&gt;, &lt;span style="color:maroon;"&gt;CanSave&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;          &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;_cmdSave&lt;/span&gt;;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Save&lt;/span&gt;()&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color: rgb(166, 83, 0);"&gt;MessageBox&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Show&lt;/span&gt;("Save");&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public bool &lt;/span&gt;&lt;span style="color:maroon;"&gt;CanSave&lt;/span&gt;()&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;GetAllInvalidRules&lt;/span&gt;().&lt;span style="color:maroon;"&gt;Count &lt;/span&gt;== &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;UserControl &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Class&lt;/span&gt;&lt;span style="color:blue;"&gt;="Jimba.UI.View.NewAccountView"&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:red;"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:red;"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StackPanel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;    &lt;br /&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StackPanel &lt;/span&gt;&lt;span style="color:red;"&gt;Orientation&lt;/span&gt;&lt;span style="color:blue;"&gt;="Horizontal"&amp;gt;&lt;br /&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Label &lt;/span&gt;&lt;span style="color:red;"&gt;Width&lt;/span&gt;&lt;span style="color:blue;"&gt;="100"&amp;gt;&lt;/span&gt;Firstname:&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Label&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;TextBox &lt;/span&gt;&lt;span style="color:red;"&gt;Text&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Binding &lt;/span&gt;&lt;span style="color:red;"&gt;Path&lt;/span&gt;&lt;span style="color:blue;"&gt;=Firstname, &lt;/span&gt;&lt;span style="color:red;"&gt;UpdateSourceTrigger&lt;/span&gt;&lt;span style="color:blue;"&gt;=PropertyChanged, &lt;/span&gt;&lt;span style="color:red;"&gt;ValidatesOnDataErrors&lt;/span&gt;&lt;span style="color:blue;"&gt;=True}" /&amp;gt;&lt;br /&gt;      &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StackPanel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StackPanel &lt;/span&gt;&lt;span style="color:red;"&gt;Orientation&lt;/span&gt;&lt;span style="color:blue;"&gt;="Horizontal"&amp;gt;&lt;br /&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Label &lt;/span&gt;&lt;span style="color:red;"&gt;Width&lt;/span&gt;&lt;span style="color:blue;"&gt;="100"&amp;gt;&lt;/span&gt;Lastname:&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Label&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;TextBox &lt;/span&gt;&lt;span style="color:red;"&gt;Text&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Binding &lt;/span&gt;&lt;span style="color:red;"&gt;Path&lt;/span&gt;&lt;span style="color:blue;"&gt;=Lastname, &lt;/span&gt;&lt;span style="color:red;"&gt;UpdateSourceTrigger&lt;/span&gt;&lt;span style="color:blue;"&gt;=PropertyChanged, &lt;/span&gt;&lt;span style="color:red;"&gt;ValidatesOnDataErrors&lt;/span&gt;&lt;span style="color:blue;"&gt;=True}" /&amp;gt;&lt;br /&gt;      &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StackPanel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StackPanel &lt;/span&gt;&lt;span style="color:red;"&gt;Orientation&lt;/span&gt;&lt;span style="color:blue;"&gt;="Horizontal"&amp;gt;&lt;br /&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Label &lt;/span&gt;&lt;span style="color:red;"&gt;Width&lt;/span&gt;&lt;span style="color:blue;"&gt;="100"&amp;gt;&lt;/span&gt;Current Balance:&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Label&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;TextBox &lt;/span&gt;&lt;span style="color:red;"&gt;Text&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Binding &lt;/span&gt;&lt;span style="color:red;"&gt;Path&lt;/span&gt;&lt;span style="color:blue;"&gt;=CurrentBalance, &lt;/span&gt;&lt;span style="color:red;"&gt;UpdateSourceTrigger&lt;/span&gt;&lt;span style="color:blue;"&gt;=PropertyChanged, &lt;/span&gt;&lt;span style="color:red;"&gt;ValidatesOnDataErrors&lt;/span&gt;&lt;span style="color:blue;"&gt;=True}" /&amp;gt;&lt;br /&gt;      &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StackPanel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;    &lt;br /&gt;  &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StackPanel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;UserControl&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-1942645402685796229?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/1942645402685796229/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=1942645402685796229' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1942645402685796229'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1942645402685796229'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/05/mvvm-validation-with-nhibernate.html' title='MVVM Validation With NHibernate Validator'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Z5KTIfnfuNs/Sg5-LYic86I/AAAAAAAAAOo/YhoCqfl99Bk/s72-c/MVVM-Validation_thumb%5B5%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-2318430110547628686</id><published>2009-05-03T14:42:00.004+04:30</published><updated>2009-05-03T15:06:19.856+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='NHibernate Validator'/><category scheme='http://www.blogger.com/atom/ns#' term='Json'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET MVC'/><category scheme='http://www.blogger.com/atom/ns#' term='jQuery'/><title type='text'>ASP.NET MVC Validation with Ajax and Json</title><content type='html'>&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Z5KTIfnfuNs/Sf1u-xLJUtI/AAAAAAAAAOc/4hC9Eb8hNCU/s1600-h/AjaxValidation%5B3%5D.png"&gt;&lt;img style="border: 0px none ; margin: 0px 15px 0px 0px; display: inline;" title="AjaxValidation" alt="AjaxValidation" src="http://lh4.ggpht.com/_Z5KTIfnfuNs/Sf1vCip88MI/AAAAAAAAAOg/3GfC-WrV1N0/AjaxValidation_thumb%5B1%5D.png?imgmax=800" width="244" align="left" border="0" height="223" /&gt;&lt;/a&gt;ASP.NET MVC has greate ajax integration with no doubt. Simply by using ajax forms you can use partial page rendering and controller callbacks. One thing that will break if you switch to Ajax / JsonResult is the built-in Validation result (ModelErrors) rendering of the MVC engine.&lt;br /&gt;&lt;br /&gt;It won’t work because of a simple fact: The built-in validation will take place when the page is being rendered on the server. When you use ajax and callbacks page will not re-render on the server-side, hence validation can not mark fields with error and add validation information to the already rendered page.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt; Let’s see how you can do this, without using a client-side validation framework.&lt;br /&gt;&lt;/p&gt;    &lt;h4&gt;&lt;span style="color: rgb(0, 128, 255);"&gt;Creating a Contact Form&lt;/span&gt;&lt;/h4&gt;  &lt;p&gt;First, let's create a form using Ajax.BeginForm and provide a javascript action to process the results. You can get rid of the ValidationMessage and ValidationSummary, since we don’t need it anymore:&lt;br /&gt;&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color:navy;"&gt;using &lt;/span&gt;(&lt;span style="color:maroon;"&gt;Ajax&lt;/span&gt;.&lt;span style="color:maroon;"&gt;BeginForm&lt;/span&gt;(&lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;AjaxOptions &lt;/span&gt;{ &lt;span style="color:maroon;"&gt;OnComplete &lt;/span&gt;= "ProcessResult" })) { &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;    &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;label &lt;/span&gt;&lt;span style="color:red;"&gt;for&lt;/span&gt;&lt;span style="color:blue;"&gt;="Name" &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;="contactLabel"&amp;gt;&lt;/span&gt;Your Name:&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;label&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:maroon;"&gt;Html&lt;/span&gt;.&lt;span style="color:maroon;"&gt;TextBox&lt;/span&gt;("SenderName", &lt;span style="color:maroon;"&gt;Model&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SenderName&lt;/span&gt;)&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;    &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;label &lt;/span&gt;&lt;span style="color:red;"&gt;for&lt;/span&gt;&lt;span style="color:blue;"&gt;="Email" &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;="contactLabel"&amp;gt;&lt;/span&gt;Your Email:&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;label&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:maroon;"&gt;Html&lt;/span&gt;.&lt;span style="color:maroon;"&gt;TextBox&lt;/span&gt;("Email", &lt;span style="color:maroon;"&gt;Model&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Email&lt;/span&gt;)&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;    &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;label &lt;/span&gt;&lt;span style="color:red;"&gt;for&lt;/span&gt;&lt;span style="color:blue;"&gt;="Subject" &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;="contactLabel"&amp;gt;&lt;/span&gt;Subject:&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;label&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:maroon;"&gt;Html&lt;/span&gt;.&lt;span style="color:maroon;"&gt;TextBox&lt;/span&gt;("Subject", &lt;span style="color:maroon;"&gt;Model&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Subject&lt;/span&gt;)&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;    &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;label &lt;/span&gt;&lt;span style="color:red;"&gt;for&lt;/span&gt;&lt;span style="color:blue;"&gt;="Message" &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;="contactLabel"&amp;gt;&lt;/span&gt;Message:&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;label&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:maroon;"&gt;Html&lt;/span&gt;.&lt;span style="color:maroon;"&gt;TextBox&lt;/span&gt;("Message", &lt;span style="color:maroon;"&gt;Model&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Message&lt;/span&gt;)&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;    &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;br &lt;/span&gt;&lt;span style="color:blue;"&gt;/&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="submit" &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;="Send" &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;="inputButton" /&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;br &lt;/span&gt;&lt;span style="color:blue;"&gt;/&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;div &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;="operationMessage"&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ul&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ul&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&amp;lt;%&lt;/span&gt; } &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;Notice that there is a div at the bottom which acts as an error placeholder and will be used to display validation information.&lt;br /&gt;&lt;br /&gt;To actually bind these fields to data, we need to create an &lt;a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object"&gt;POCO&lt;/a&gt; class acting as a backing entity. In this example, I’ve used a poco class and decorate it with &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fnhcontrib%2F&amp;amp;ei=inL9SdLwGovMjAfI8pGnAw&amp;amp;usg=AFQjCNFZNaEEiAB6LqwOrL2xCH1zao2IzQ"&gt;NHibernate Validator&lt;/a&gt;'s attributes to do the validation. See &lt;a href="http://blog.hightech.ir/2009/04/nhibernate-automatic-validation.html"&gt;here&lt;/a&gt; for more info on how to configure NHibernate Validator using ASP.NET MVC:&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ContactMessage &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IValidatable&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color:maroon;"&gt;ContactMessage&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:maroon;"&gt;MessageDate &lt;/span&gt;= &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Today&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;public virtual int &lt;/span&gt;&lt;span style="color:maroon;"&gt;Id&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;private set&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;NotNullNotEmpty&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public virtual string &lt;/span&gt;&lt;span style="color:maroon;"&gt;SenderName&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;NotNullNotEmpty&lt;/span&gt;]&lt;br /&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;Email&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public virtual string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Email&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;NotNullNotEmpty&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public virtual string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Subject&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;NotNullNotEmpty&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public virtual string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Message&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;public virtual &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;MessageDate&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;private set&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;h4&gt;&lt;span style="color: rgb(0, 128, 255);"&gt;Controller Action&lt;/span&gt;&lt;/h4&gt;&lt;p&gt;Two methods are needed to handle to Post / Get separately. Our Get method returns a new instance of our contact entity to the view, and Post method is where the operation results is returned to the client using &lt;a href="http://en.wikipedia.org/wiki/Json"&gt;Json&lt;/a&gt;. The OperationResultInfo class containing the elaborate error information is what returned as Json to the client:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;OperationResultInfo&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color:maroon;"&gt;OperationResultInfo&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:maroon;"&gt;Errors &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;ErrorReasonInfo&lt;/span&gt;&amp;gt;();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;public bool &lt;/span&gt;&lt;span style="color:maroon;"&gt;Successfull &lt;/span&gt;{ &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Message &lt;/span&gt;{ &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;ErrorReasonInfo&lt;/span&gt;&amp;gt; &lt;span style="color:maroon;"&gt;Errors &lt;/span&gt;{ &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ErrorReasonInfo&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;&lt;span style="color:navy;"&gt;public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;PropertyName &lt;/span&gt;{ &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;Error &lt;/span&gt;{ &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;; }&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="code"&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;AcceptVerbs&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;HttpVerbs&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Get&lt;/span&gt;)]&lt;br /&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ActionResult &lt;/span&gt;&lt;span style="color:maroon;"&gt;Contact&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;View&lt;/span&gt;(&lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ContactMessage&lt;/span&gt;());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;AcceptVerbs&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;HttpVerbs&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Post&lt;/span&gt;)]&lt;br /&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;AutoValidate&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ActionResult &lt;/span&gt;&lt;span style="color:maroon;"&gt;Contact&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ContactMessage &lt;/span&gt;&lt;span style="color:maroon;"&gt;msg&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;ModelState&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsValid&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;try&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;       &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;messengerService&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SendMail&lt;/span&gt;(&lt;span style="color:maroon;"&gt;msg&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;       &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;Json&lt;/span&gt;(&lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;OperationResultInfo&lt;br /&gt;                   &lt;/span&gt;{&lt;br /&gt;                       &lt;span style="color:maroon;"&gt;Successfull &lt;/span&gt;= &lt;span style="color:navy;"&gt;true&lt;/span&gt;,&lt;br /&gt;                       &lt;span style="color:maroon;"&gt;Message &lt;/span&gt;= "Thank you. Your message was sent successfully."&lt;br /&gt;                   });&lt;br /&gt;   }&lt;br /&gt;   &lt;span style="color:navy;"&gt;catch &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Exception &lt;/span&gt;&lt;span style="color:maroon;"&gt;ex&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:maroon;"&gt;ModelState&lt;/span&gt;.&lt;span style="color:maroon;"&gt;AddUnhandledError&lt;/span&gt;(&lt;span style="color:maroon;"&gt;ex&lt;/span&gt;);&lt;br /&gt;       &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;Json&lt;/span&gt;(&lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;OperationResultInfo&lt;br /&gt;                   &lt;/span&gt;{&lt;br /&gt;                       &lt;span style="color:maroon;"&gt;Successfull &lt;/span&gt;= &lt;span style="color:navy;"&gt;false&lt;/span&gt;,&lt;br /&gt;                       &lt;span style="color:maroon;"&gt;Message &lt;/span&gt;= "There was an error processing your request. Retry later."&lt;br /&gt;                   });&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;Json&lt;/span&gt;(&lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;OperationResultInfo&lt;br /&gt;           &lt;/span&gt;{&lt;br /&gt;               &lt;span style="color:maroon;"&gt;Successfull &lt;/span&gt;= &lt;span style="color:navy;"&gt;false&lt;/span&gt;,&lt;br /&gt;               &lt;span style="color:maroon;"&gt;Message &lt;/span&gt;= "Could not send your information.",&lt;br /&gt;               &lt;span style="color:maroon;"&gt;Errors &lt;/span&gt;= &lt;span style="color:maroon;"&gt;ModelState&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetAllErrors&lt;/span&gt;()&lt;br /&gt;           });&lt;br /&gt;}&lt;br /&gt;&lt;span style="color:navy;"&gt;&lt;br /&gt;public static class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ModelStateExtensions&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;&lt;span style="color:navy;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;ErrorReasonInfo&lt;/span&gt;&amp;gt; &lt;span style="color:maroon;"&gt;GetAllErrors&lt;/span&gt;(&lt;span style="color:navy;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ModelStateDictionary &lt;/span&gt;&lt;span style="color:maroon;"&gt;modelState&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;errors &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;ErrorReasonInfo&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;foreach &lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;state &lt;/span&gt;&lt;span style="color:navy;"&gt;in &lt;/span&gt;&lt;span style="color:maroon;"&gt;modelState&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;state&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Value&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Errors&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Count &lt;/span&gt;&amp;gt; &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;)&lt;br /&gt;       {&lt;br /&gt;           &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;err &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ErrorReasonInfo &lt;/span&gt;{&lt;span style="color:maroon;"&gt;PropertyName &lt;/span&gt;= &lt;span style="color:maroon;"&gt;state&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Key&lt;/span&gt;};&lt;br /&gt;           &lt;span style="color:maroon;"&gt;state&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Value&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Errors&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ForEach&lt;/span&gt;(&lt;span style="color:maroon;"&gt;x &lt;/span&gt;=&amp;gt; &lt;span style="color:maroon;"&gt;err&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Error &lt;/span&gt;+= &lt;span style="color:maroon;"&gt;x&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ErrorMessage&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;           &lt;span style="color:maroon;"&gt;errors&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;(&lt;span style="color:maroon;"&gt;err&lt;/span&gt;);&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;errors&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;strong&gt;&lt;h4&gt;&lt;span style="color: rgb(0, 128, 255);"&gt;Displaying The Json Result&lt;/span&gt;&lt;/h4&gt;&lt;/strong&gt;The Json result returned to the client contains all the fields having errors and the related error message. A typical result would look like this:&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;pre class="code"&gt;{&lt;br /&gt;"Successfull":false,&lt;br /&gt;"Message":"Could not send your information:",&lt;br /&gt;"Errors":[&lt;br /&gt;{"PropertyName":"SenderName",&lt;br /&gt;"Error":"may not be null or empty"},&lt;br /&gt;{"PropertyName":"Email",&lt;br /&gt;"Error":"may not be null or empty"},&lt;br /&gt;{"PropertyName":"Subject",&lt;br /&gt;"Error":"may not be null or empty"},&lt;br /&gt;{"PropertyName":"Message",&lt;br /&gt;"Error":"may not be null or empty"}]&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;How do we format this data on the client and display it in a user-friendly fashion on our page? Let’s process the Json object using javascript and add the result to the error place holder using &lt;a href="http://en.wikipedia.org/wiki/Jquery"&gt;jQuery&lt;/a&gt;:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="text/javascript"&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:navy;"&gt;function &lt;/span&gt;&lt;span style="color:maroon;"&gt;ProcessResult&lt;/span&gt;(&lt;span style="color:maroon;"&gt;content&lt;/span&gt;) {&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;json &lt;/span&gt;= &lt;span style="color:maroon;"&gt;content&lt;/span&gt;.&lt;span style="color:maroon;"&gt;get_response&lt;/span&gt;().&lt;span style="color:maroon;"&gt;get_object&lt;/span&gt;();&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;result &lt;/span&gt;= &lt;span style="color:maroon;"&gt;eval&lt;/span&gt;(&lt;span style="color:maroon;"&gt;json&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:maroon;"&gt;$&lt;/span&gt;("#operationMessage &amp;gt; span").&lt;span style="color:maroon;"&gt;empty&lt;/span&gt;();&lt;br /&gt;   &lt;span style="color:maroon;"&gt;$&lt;/span&gt;("#operationMessage &amp;gt; ul").&lt;span style="color:maroon;"&gt;empty&lt;/span&gt;();&lt;br /&gt;   &lt;span style="color:maroon;"&gt;$&lt;/span&gt;(':input').&lt;span style="color:maroon;"&gt;removeClass&lt;/span&gt;('input-validation-error');&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;result&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Successfull&lt;/span&gt;) {&lt;br /&gt;       &lt;span style="color:maroon;"&gt;$&lt;/span&gt;('#operationMessage').&lt;span style="color:maroon;"&gt;append&lt;/span&gt;('&amp;lt;span&amp;gt;' + &lt;span style="color:maroon;"&gt;result&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Message &lt;/span&gt;+ '&amp;lt;/span&amp;gt;')&lt;br /&gt;                             .&lt;span style="color:maroon;"&gt;removeClass&lt;/span&gt;('error')&lt;br /&gt;                             .&lt;span style="color:maroon;"&gt;addClass&lt;/span&gt;('success');&lt;br /&gt;   }&lt;br /&gt;   &lt;span style="color:navy;"&gt;else &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;       &lt;span style="color:maroon;"&gt;$&lt;/span&gt;('#operationMessage').&lt;span style="color:maroon;"&gt;append&lt;/span&gt;('&amp;lt;span&amp;gt;&amp;lt;br&amp;gt;' + &lt;span style="color:maroon;"&gt;result&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Message &lt;/span&gt;+ '&amp;lt;/span&amp;gt;')&lt;br /&gt;                             .&lt;span style="color:maroon;"&gt;removeClass&lt;/span&gt;('success')&lt;br /&gt;                             .&lt;span style="color:maroon;"&gt;addClass&lt;/span&gt;('error');&lt;br /&gt;&lt;br /&gt;       &lt;span style="color:navy;"&gt;for &lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;err &lt;/span&gt;&lt;span style="color:navy;"&gt;in &lt;/span&gt;&lt;span style="color:maroon;"&gt;result&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Errors&lt;/span&gt;) {&lt;br /&gt;           &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;propertyName &lt;/span&gt;= &lt;span style="color:maroon;"&gt;result&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Errors&lt;/span&gt;[&lt;span style="color:maroon;"&gt;err&lt;/span&gt;].&lt;span style="color:maroon;"&gt;PropertyName&lt;/span&gt;;&lt;br /&gt;           &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;errorMessage &lt;/span&gt;= &lt;span style="color:maroon;"&gt;result&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Errors&lt;/span&gt;[&lt;span style="color:maroon;"&gt;err&lt;/span&gt;].&lt;span style="color:maroon;"&gt;Error&lt;/span&gt;;&lt;br /&gt;           &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;message &lt;/span&gt;= &lt;span style="color:maroon;"&gt;propertyName &lt;/span&gt;+ ' ' + &lt;span style="color:maroon;"&gt;errorMessage&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;           &lt;span style="color:maroon;"&gt;$&lt;/span&gt;('#' + &lt;span style="color:maroon;"&gt;propertyName&lt;/span&gt;).&lt;span style="color:maroon;"&gt;addClass&lt;/span&gt;('input-validation-error');&lt;br /&gt;           &lt;span style="color:maroon;"&gt;$&lt;/span&gt;('#operationMessage &amp;gt; ul').&lt;span style="color:maroon;"&gt;append&lt;/span&gt;('&amp;lt;li&amp;gt;# ' + &lt;span style="color:maroon;"&gt;message &lt;/span&gt;+ '&amp;lt;/li&amp;gt;');&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Pretty neat, ha? Hope this helps.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-2318430110547628686?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/2318430110547628686/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=2318430110547628686' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2318430110547628686'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2318430110547628686'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/05/aspnet-validation-with-ajax-and-json.html' title='ASP.NET MVC Validation with Ajax and Json'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Z5KTIfnfuNs/Sf1vCip88MI/AAAAAAAAAOg/3GfC-WrV1N0/s72-c/AjaxValidation_thumb%5B1%5D.png?imgmax=800' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-2575865364094499222</id><published>2009-04-29T12:26:00.002+04:30</published><updated>2009-04-29T12:26:53.776+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><title type='text'>Google Analytics in Silverlight</title><content type='html'>&lt;p&gt;I’ve been trying to setup &lt;a href="http://www.google.com/analytics"&gt;Google Analytics&lt;/a&gt; Event Tracking in a Silverlight application for a while and despite all the how-tos and artilces on the web, I just couldn’t get it running. I kept doing everything proposed, but when the application was run on IE, a javascript error was occurred. Firefox didn’t even bother reporting the JS error. So, if you happen to have the same problem, here’s how you should do it to get it working.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fcode.google.com%2Fapis%2Fanalytics%2Fdocs%2Ftracking%2FeventTrackerGuide.html&amp;amp;ei=6QH4SfjUNMKe-Ab4-tCcDw&amp;amp;usg=AFQjCNEV2mCQxPIlu5QwrpFZWTcCY3sLlw"&gt;Event Tracking&lt;/a&gt; is when you want to track users accessing various parts of you &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FRich_Internet_application&amp;amp;ei=nQL4SdLPEs3N-QalgKG0Dw&amp;amp;usg=AFQjCNEE-7lvg62HZI8_wHWYfMsU4llEJQ"&gt;RIA&lt;/a&gt; application, which works differently comparing to html based websites. The basic setup is almost the same: include ga.js javascript on your page hosting the Silverlight application. Include this snippet before &amp;lt;/body&amp;gt; tag:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="text/javascript"&amp;gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;gaJsHost &lt;/span&gt;= (("https:" == &lt;span style="color:maroon;"&gt;document&lt;/span&gt;.&lt;span style="color:maroon;"&gt;location&lt;/span&gt;.&lt;span style="color:maroon;"&gt;protocol&lt;/span&gt;) ? "https://ssl." : "http://www.");&lt;br /&gt;   &lt;span style="color:maroon;"&gt;document&lt;/span&gt;.&lt;span style="color:maroon;"&gt;write&lt;/span&gt;(&lt;span style="color:maroon;"&gt;unescape&lt;/span&gt;("%3Cscript src='" + &lt;span style="color:maroon;"&gt;gaJsHost &lt;/span&gt;+ "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));&lt;br /&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;Next step would be to create a helper javascript function which we’ll call in our silverlight application. The script would help us track events on our silverlight application. Add this just below the above script:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="text/javascript"&amp;gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:navy;"&gt;function &lt;/span&gt;&lt;span style="color:maroon;"&gt;trackPage&lt;/span&gt;(&lt;span style="color:maroon;"&gt;category&lt;/span&gt;, &lt;span style="color:maroon;"&gt;action&lt;/span&gt;, &lt;span style="color:maroon;"&gt;label&lt;/span&gt;) {&lt;br /&gt;       &lt;span style="color:navy;"&gt;try &lt;/span&gt;{&lt;br /&gt;           &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;pageTracker &lt;/span&gt;= &lt;span style="color:maroon;"&gt;_gat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;_getTracker&lt;/span&gt;("xx-xxxxxxxx-x");&lt;br /&gt;           &lt;span style="color:maroon;"&gt;pageTracker&lt;/span&gt;.&lt;span style="color:maroon;"&gt;_trackPageview&lt;/span&gt;();&lt;br /&gt;           &lt;span style="color:maroon;"&gt;pageTracker&lt;/span&gt;.&lt;span style="color:maroon;"&gt;_trackEvent&lt;/span&gt;(&lt;span style="color:maroon;"&gt;category&lt;/span&gt;, &lt;span style="color:maroon;"&gt;action&lt;/span&gt;, &lt;span style="color:maroon;"&gt;label&lt;/span&gt;);&lt;br /&gt;       }&lt;br /&gt;       &lt;span style="color:navy;"&gt;catch &lt;/span&gt;(&lt;span style="color:maroon;"&gt;err&lt;/span&gt;) {&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;strong&gt;&lt;em&gt;&lt;a href="http://lh6.ggpht.com/_Z5KTIfnfuNs/SfgIEOyJy1I/AAAAAAAAAOU/FEsEXTHmd5U/s1600-h/EventTracking%5B3%5D.png"&gt;&lt;img style="border: 0px none ; margin: 0px 20px 0px 0px; display: inline;" title="EventTracking" alt="EventTracking" src="http://lh5.ggpht.com/_Z5KTIfnfuNs/SfgIE5i5stI/AAAAAAAAAOY/ozsPNtgSCMs/EventTracking_thumb%5B1%5D.png?imgmax=800" width="244" align="left" border="0" height="139" /&gt;&lt;/a&gt;It is essential that calls to_trackPageview is made prior to calling  _trackEvent. &lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt; Notice how the function is wrapped in a try-catch block so if there is any issues with ga.js loading, we silently ignore the tracking. Strangely, when there was any javascript error in IE 8, some parts of my silverlight application didn’t work properly. This helped getting around it.&lt;/p&gt;Usually, you’d call an existing javascript in your silverlight app by calling it this way:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;HtmlPage&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Window&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Invoke&lt;/span&gt;("functionName", &lt;span style="color:navy;"&gt;new&lt;/span&gt;[] { &lt;span style="color:maroon;"&gt;args &lt;/span&gt;});&lt;/pre&gt;But to make things easier, let’s create an extension method to help us calling the tracking function:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public static class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ViewExtension&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;public static void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Track&lt;/span&gt;(&lt;span style="color:navy;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;UserControl &lt;/span&gt;&lt;span style="color:maroon;"&gt;view&lt;/span&gt;, &lt;span style="color:navy;"&gt;string &lt;/span&gt;&lt;span style="color:maroon;"&gt;action&lt;/span&gt;, &lt;span style="color:navy;"&gt;string &lt;/span&gt;&lt;span style="color:maroon;"&gt;page&lt;/span&gt;, &lt;span style="color:navy;"&gt;string &lt;/span&gt;&lt;span style="color:maroon;"&gt;label&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color: rgb(166, 83, 0);"&gt;HtmlPage&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Window&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Invoke&lt;/span&gt;("trackPage", &lt;span style="color:navy;"&gt;new string&lt;/span&gt;[] { &lt;span style="color:maroon;"&gt;action&lt;/span&gt;, &lt;span style="color:maroon;"&gt;page&lt;/span&gt;, &lt;span style="color:maroon;"&gt;label &lt;/span&gt;});&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;Just need to start tracking now!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-2575865364094499222?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/2575865364094499222/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=2575865364094499222' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2575865364094499222'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2575865364094499222'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/04/google-analytics-in-silverlight.html' title='Google Analytics in Silverlight'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_Z5KTIfnfuNs/SfgIE5i5stI/AAAAAAAAAOY/ozsPNtgSCMs/s72-c/EventTracking_thumb%5B1%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-6268611079883229192</id><published>2009-04-28T13:15:00.004+04:30</published><updated>2009-04-29T11:06:55.311+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='NHibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET MVC'/><title type='text'>NHibernate Automatic Validation</title><content type='html'>&lt;p&gt;NHibernate is for sure one of the prodigy childs of the .NET open-source movement, yet there are some other great libraries and frameworks. One of them is NHibernate Validator which belongs to &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fnhcontrib%2F&amp;amp;ei=STX3SfLON8e4-QbnmfW2Dw&amp;amp;usg=AFQjCNFZNaEEiAB6LqwOrL2xCH1zao2IzQ"&gt;NHibernate Contrib&lt;/a&gt; project. What it does is to validate your domain entities that supposedly are being persisted using NHibernate, but it provides so much flexibility that you can validate almost every POCO class using attributes.&lt;/p&gt;&lt;p&gt;I’m using it in an ASP.NET MVC project, so first let’s see how to configure and make it work here.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: I’m using the trunk build of the Castle Project.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;First thing you need to do is to initialize Validator’s engine. Since I’m using it on a web application, let’s initialize and use a shared engine. I’m using the attribute based validation, but you can write the validation logic in your .hbm mapping files.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;MvcApplication &lt;/span&gt;: &lt;span style="color:maroon;"&gt;System&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Web&lt;/span&gt;.&lt;span style="color: rgb(166, 83, 0);"&gt;HttpApplication&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;IMvcApplication&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;protected void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Application_Start&lt;/span&gt;()&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:maroon;"&gt;RegisterRoutes&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;RouteTable&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Routes&lt;/span&gt;);&lt;br /&gt;      &lt;span style="color:maroon;"&gt;CreateDependencyInjectionContainer&lt;/span&gt;();&lt;br /&gt;      &lt;span style="color:maroon;"&gt;RegisterControllerFactory&lt;/span&gt;();&lt;br /&gt;      &lt;span style="color:maroon;"&gt;RegisterSessionFactory&lt;/span&gt;();&lt;br /&gt;      &lt;span style="color:maroon;"&gt;RegisterValidatorEngine&lt;/span&gt;();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;private void &lt;/span&gt;&lt;span style="color:maroon;"&gt;RegisterValidatorEngine&lt;/span&gt;()&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;config &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;NHVConfigurationBase&lt;/span&gt;();&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:maroon;"&gt;config&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Properties&lt;/span&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ApplyToDDL&lt;/span&gt;] = "false";&lt;br /&gt;      &lt;span style="color:maroon;"&gt;config&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Properties&lt;/span&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;AutoregisterListeners&lt;/span&gt;] = "true";&lt;br /&gt;      &lt;span style="color:maroon;"&gt;config&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Properties&lt;/span&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ValidatorMode&lt;/span&gt;] = "UseAttribute";&lt;br /&gt;      &lt;span style="color:maroon;"&gt;config&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Properties&lt;/span&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SharedEngineClass&lt;/span&gt;] = &lt;span style="color:navy;"&gt;typeof &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ValidatorEngine&lt;/span&gt;).&lt;span style="color:maroon;"&gt;FullName&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color:maroon;"&gt;config&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Mappings&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;(&lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;MappingConfiguration&lt;/span&gt;("MyApp.Domain", &lt;span style="color:navy;"&gt;null&lt;/span&gt;));&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SharedEngineProvider &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;NHibernateSharedEngineProvider&lt;/span&gt;();&lt;br /&gt;      &lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SharedEngineProvider&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetEngine&lt;/span&gt;().&lt;span style="color:maroon;"&gt;Configure&lt;/span&gt;(&lt;span style="color:maroon;"&gt;config&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: rgb(166, 83, 0);"&gt;ValidatorInitializer&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Initialize&lt;/span&gt;(&lt;span style="color:maroon;"&gt;NHibernateConfig&lt;/span&gt;);&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;and that’s it. You can validate entities passed to your controller’s action by creating a extension method which simplifies things and automatically adds all the errors to ModelState:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public static class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ControllerExtensions&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;  &lt;span style="color:gray;"&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;  /// &lt;/span&gt;&lt;span style="color:green;"&gt;Validates an entity&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:gray;"&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;  /// &amp;lt;param name="controller"&amp;gt;&amp;lt;/param&amp;gt;&lt;br /&gt;  /// &amp;lt;param name="entity"&amp;gt;&amp;lt;/param&amp;gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:navy;"&gt;public static void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Validate&lt;/span&gt;(&lt;span style="color:navy;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Controller &lt;/span&gt;&lt;span style="color:maroon;"&gt;controller&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;IValidatable &lt;/span&gt;&lt;span style="color:maroon;"&gt;entity&lt;/span&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;engine &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;Environment&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SharedEngineProvider&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetEngine&lt;/span&gt;();&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;errors &lt;/span&gt;= &lt;span style="color:maroon;"&gt;engine&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Validate&lt;/span&gt;(&lt;span style="color:maroon;"&gt;entity&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:navy;"&gt;foreach &lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;error &lt;/span&gt;&lt;span style="color:navy;"&gt;in &lt;/span&gt;&lt;span style="color:maroon;"&gt;errors&lt;/span&gt;)&lt;br /&gt;      {&lt;br /&gt;          &lt;span style="color:maroon;"&gt;controller&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ModelState&lt;/span&gt;.&lt;span style="color:maroon;"&gt;AddModelError&lt;/span&gt;(&lt;span style="color:maroon;"&gt;error&lt;/span&gt;.&lt;span style="color:maroon;"&gt;PropertyName&lt;/span&gt;, &lt;span style="color:maroon;"&gt;error&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Message&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;Note that IValidatable is just a marker interface. To call this method and do the actual validation, you need to call the validate method:&lt;br /&gt;&lt;pre class="code"&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;AcceptVerbs&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;HttpVerbs&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Post&lt;/span&gt;)]&lt;br /&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ActionResult &lt;/span&gt;&lt;span style="color:maroon;"&gt;Contact&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ContactMessage &lt;/span&gt;&lt;span style="color:maroon;"&gt;msg&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;try&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Validate&lt;/span&gt;(&lt;span style="color:maroon;"&gt;msg&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;ModelState&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsValid&lt;/span&gt;)&lt;br /&gt;      {&lt;br /&gt;          &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;messengerService&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SendMail&lt;/span&gt;(&lt;span style="color:maroon;"&gt;msg&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;RedirectToAction&lt;/span&gt;("Index");&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;  &lt;span style="color:navy;"&gt;catch &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Exception &lt;/span&gt;&lt;span style="color:maroon;"&gt;ex&lt;/span&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:maroon;"&gt;ModelState&lt;/span&gt;.&lt;span style="color:maroon;"&gt;AddUnhandledError&lt;/span&gt;(&lt;span style="color:maroon;"&gt;ex&lt;/span&gt;);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;View&lt;/span&gt;(&lt;span style="color:navy;"&gt;msg&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;Easy, right? but it can even get easier! Let’s create an ActionFilter to automagically validate our parameters when the action is invoked:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:gray;"&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &lt;/span&gt;&lt;span style="color:green;"&gt;Automatically validates all the action&lt;br /&gt;&lt;/span&gt;&lt;span style="color:gray;"&gt;/// &lt;/span&gt;&lt;span style="color:green;"&gt;parameters of type IValidatable.&lt;br /&gt;&lt;/span&gt;&lt;span style="color:gray;"&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;AutoValidate &lt;/span&gt;: &lt;span style="color: rgb(166, 83, 0);"&gt;ActionFilterAttribute&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;public override void &lt;/span&gt;&lt;span style="color:maroon;"&gt;OnActionExecuting&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ActionExecutingContext &lt;/span&gt;&lt;span style="color:maroon;"&gt;filterContext&lt;/span&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;controller &lt;/span&gt;= &lt;span style="color:maroon;"&gt;filterContext&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Controller &lt;/span&gt;&lt;span style="color:navy;"&gt;as &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Controller&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color:navy;"&gt;if&lt;/span&gt;(&lt;span style="color:maroon;"&gt;controller &lt;/span&gt;== &lt;span style="color:navy;"&gt;null&lt;/span&gt;)&lt;br /&gt;          &lt;span style="color:navy;"&gt;return&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:navy;"&gt;foreach &lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;entity &lt;/span&gt;&lt;span style="color:navy;"&gt;in &lt;/span&gt;&lt;span style="color:maroon;"&gt;GetEntitiesFromParameters&lt;/span&gt;(&lt;span style="color:maroon;"&gt;filterContext&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ActionParameters&lt;/span&gt;))&lt;br /&gt;      {&lt;br /&gt;          &lt;span style="color:maroon;"&gt;controller&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Validate&lt;/span&gt;(&lt;span style="color:maroon;"&gt;entity&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;private static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IValidatable&lt;/span&gt;&amp;gt; &lt;span style="color:maroon;"&gt;GetEntitiesFromParameters&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color:navy;"&gt;string&lt;/span&gt;, &lt;span style="color:navy;"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; &lt;span style="color:maroon;"&gt;dictionary&lt;/span&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;validatableParameters &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IValidatable&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:navy;"&gt;foreach &lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;keyValue &lt;/span&gt;&lt;span style="color:navy;"&gt;in &lt;/span&gt;&lt;span style="color:maroon;"&gt;dictionary&lt;/span&gt;)&lt;br /&gt;      {&lt;br /&gt;          &lt;span style="color:navy;"&gt;if&lt;/span&gt;(&lt;span style="color:maroon;"&gt;keyValue&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Value &lt;/span&gt;&lt;span style="color:navy;"&gt;is &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IValidatable&lt;/span&gt;)&lt;br /&gt;          {&lt;br /&gt;              &lt;span style="color:maroon;"&gt;validatableParameters&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;((&lt;span style="color: rgb(43, 145, 175);"&gt;IValidatable&lt;/span&gt;)&lt;span style="color:maroon;"&gt;keyValue&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Value&lt;/span&gt;);&lt;br /&gt;          }&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;validatableParameters&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;and your action method looks as simple as this:&lt;br /&gt;&lt;pre class="code"&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;AcceptVerbs&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;HttpVerbs&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Post&lt;/span&gt;)]&lt;br /&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;AutoValidate&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ActionResult &lt;/span&gt;&lt;span style="color:maroon;"&gt;Contact&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ContactMessage &lt;/span&gt;&lt;span style="color:maroon;"&gt;msg&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;if&lt;/span&gt;(&lt;span style="color:maroon;"&gt;ModelState&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsValid&lt;/span&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;try&lt;br /&gt;      &lt;/span&gt;{&lt;br /&gt;          &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;messengerService&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SendMail&lt;/span&gt;(&lt;span style="color:maroon;"&gt;msg&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;RedirectToAction&lt;/span&gt;("Index");&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:navy;"&gt;catch &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Exception &lt;/span&gt;&lt;span style="color:maroon;"&gt;ex&lt;/span&gt;)&lt;br /&gt;      {&lt;br /&gt;          &lt;span style="color:maroon;"&gt;ModelState&lt;/span&gt;.&lt;span style="color:maroon;"&gt;AddUnhandledError&lt;/span&gt;(&lt;span style="color:maroon;"&gt;ex&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;View&lt;/span&gt;(&lt;span style="color:navy;"&gt;msg&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;Have fun validating!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-6268611079883229192?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/6268611079883229192/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=6268611079883229192' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6268611079883229192'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6268611079883229192'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/04/nhibernate-automatic-validation.html' title='NHibernate Automatic Validation'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-1652479209163059441</id><published>2009-04-28T11:33:00.002+04:30</published><updated>2009-04-28T11:46:15.275+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Code Analysis'/><title type='text'>Code Review No. N, or Do chimps write code better than us?</title><content type='html'>&lt;p&gt;Well, this is not actually my first code review, but this is supposed to be a copycat of &lt;a href="http://ayende.com/Blog/archive/2007/11/13/Code-review-WTF-number-N.aspx"&gt;“Code Review WTF, Number N”&lt;/a&gt; series by &lt;a href="http://ayende.com/"&gt;Ayende Rahien&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I was inspecting an open-source code base which is a medical business application. So why am I reviewing the code base? I had a request to add persian calendar support to this application and I was trying to figure out how much time does this take and if it is possible.&lt;/p&gt;  &lt;p&gt;There are very serious issues here, I even don’t know where to start! For a open-source project at its 6th version, I was hoping things to be pretty much solid. Apparantly with all the bells and whistles on developer’s website, it is not.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Using a strange naming convension for your project like xCodeBase, xODR is BAD.&lt;/li&gt;    &lt;li&gt;Having A LOT of commented code in your code base is BAD.&lt;/li&gt;    &lt;li&gt;Exposing class fields as public is BAD. use properties.&lt;/li&gt;    &lt;li&gt;Using IFs to check for database type EVERYWHERE is VERY BAD.&lt;/li&gt;    &lt;li&gt;Obfuscated class and method names are BAD. (as in Lan class which does the string translation job with public methods named g, F and C)!!&lt;/li&gt;    &lt;li&gt;Wrapping remote method names in an ENUM. What are you thinking?!&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;After going through some of the classes, I think I’m going crazy! Okay, let’s see how’s the business logic being handled. That’s the major part of the application, right?&lt;/p&gt;  &lt;p&gt;Data is accessed by DataCore class in server AND client side (depending if remoting is used), which retrieves the data in an UnTyped dataset. To make things easier(?) there’s one single method to invoke remote BusinessLayer implementation methods:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;DataSet &lt;/span&gt;&lt;span style="color:maroon;"&gt;GetDsByMethod&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;MethodNameDS &lt;/span&gt;&lt;span style="color:maroon;"&gt;methodName&lt;/span&gt;, &lt;span style="color:navy;"&gt;object&lt;/span&gt;[] &lt;span style="color:maroon;"&gt;parameters&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;switch &lt;/span&gt;(&lt;span style="color:maroon;"&gt;methodName&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;default&lt;/span&gt;:&lt;br /&gt;           &lt;span style="color:navy;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ApplicationException&lt;/span&gt;("MethodName not found");&lt;br /&gt;       &lt;span style="color:navy;"&gt;case &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;MethodNameDS&lt;/span&gt;.&lt;span style="color:maroon;"&gt;AccountModule_GetAll&lt;/span&gt;:&lt;br /&gt;           &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;AccountModules&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetAll&lt;/span&gt;((&lt;span style="color:navy;"&gt;int&lt;/span&gt;)&lt;span style="color:maroon;"&gt;parameters&lt;/span&gt;[&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;], (&lt;span style="color:navy;"&gt;bool&lt;/span&gt;)&lt;span style="color:maroon;"&gt;parameters&lt;/span&gt;[&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;1&lt;/span&gt;], (&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;)&lt;span style="color:maroon;"&gt;parameters&lt;/span&gt;[&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;2&lt;/span&gt;], (&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;)&lt;span style="color:maroon;"&gt;parameters&lt;/span&gt;[&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;3&lt;/span&gt;], (&lt;span style="color:navy;"&gt;bool&lt;/span&gt;)&lt;span style="color:maroon;"&gt;parameters&lt;/span&gt;[&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;4&lt;/span&gt;]);&lt;br /&gt;       &lt;span style="color:navy;"&gt;case &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;MethodNameDS&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Appointment_GetApptEdit&lt;/span&gt;:&lt;br /&gt;           &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;Appointments&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetApptEdit&lt;/span&gt;((&lt;span style="color:navy;"&gt;int&lt;/span&gt;)&lt;span style="color:maroon;"&gt;parameters&lt;/span&gt;[&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;]);&lt;br /&gt;       &lt;span style="color:navy;"&gt;case &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;MethodNameDS&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Cache_Refresh&lt;/span&gt;:&lt;br /&gt;           &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;Cache&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Refresh&lt;/span&gt;((&lt;span style="color:navy;"&gt;string&lt;/span&gt;)&lt;span style="color:maroon;"&gt;parameters&lt;/span&gt;[&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;0&lt;/span&gt;]);&lt;br /&gt;       &lt;span style="color:navy;"&gt;case &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;MethodNameDS&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CovCats_RefreshCache&lt;/span&gt;:&lt;br /&gt;           &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;CovCats&lt;/span&gt;.&lt;span style="color:maroon;"&gt;RefreshCache&lt;/span&gt;();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;public enum &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;MethodNameDS&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:maroon;"&gt;AccountModule_GetAll&lt;/span&gt;,&lt;br /&gt;   &lt;span style="color:maroon;"&gt;Appointment_GetApptEdit&lt;/span&gt;,&lt;br /&gt;   &lt;span style="color:maroon;"&gt;Cache_Refresh&lt;/span&gt;,&lt;br /&gt;   &lt;span style="color:maroon;"&gt;CovCats_RefreshCache&lt;/span&gt;,&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;  Dude! WTF is that?! and then there’s a similar method to work with DataTables!&lt;br /&gt;&lt;br /&gt;On the server where client requests are processed, data is being serialized / deserialzed by a “WorkerClass” class. The code to perform the client’s request is what makes me wonder!&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;WorkerClass&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;private &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;NetworkStream &lt;/span&gt;&lt;span style="color:maroon;"&gt;netStream&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:green;"&gt;// The constructor obtains the state information.&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color:maroon;"&gt;WorkerClass&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;NetworkStream &lt;/span&gt;&lt;span style="color:maroon;"&gt;stream&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:maroon;"&gt;netStream &lt;/span&gt;= &lt;span style="color:maroon;"&gt;stream&lt;/span&gt;;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;DoWork&lt;/span&gt;()&lt;br /&gt;   {&lt;br /&gt;   &lt;span style="color:navy;"&gt;while&lt;/span&gt;(&lt;span style="color:navy;"&gt;true&lt;/span&gt;) {&lt;span style="color:green;"&gt;//Each loop gets and returns one message pair.&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:navy;"&gt;byte&lt;/span&gt;[] &lt;span style="color:maroon;"&gt;data &lt;/span&gt;=  &lt;span style="color:navy;"&gt;null&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color:green;"&gt;// Retrieve data from client&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:navy;"&gt;try &lt;/span&gt;{&lt;br /&gt;           &lt;span style="color:maroon;"&gt;data &lt;/span&gt;= &lt;span style="color:maroon;"&gt;RemotingClient&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ReadDataFromStream&lt;/span&gt;(&lt;span style="color:maroon;"&gt;netStream&lt;/span&gt;);&lt;br /&gt;       }&lt;br /&gt;       &lt;span style="color:navy;"&gt;catch &lt;/span&gt;{&lt;span style="color:green;"&gt;//if connection was closed by client.&lt;br /&gt;           &lt;/span&gt;&lt;span style="color:navy;"&gt;break&lt;/span&gt;;&lt;br /&gt;       }&lt;br /&gt;       &lt;span style="color:maroon;"&gt;DataTransferObject dto&lt;/span&gt;=&lt;span style="color:maroon;"&gt;DataTransferObject&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Deserialize&lt;/span&gt;(&lt;span style="color:maroon;"&gt;data&lt;/span&gt;);&lt;br /&gt;       &lt;span style="color:green;"&gt;//Process and send response to client--------------------------------------&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:maroon;"&gt;XmlSerializer serializer&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color:navy;"&gt;using &lt;/span&gt;(&lt;span style="color:maroon;"&gt;MemoryStream memStream &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:maroon;"&gt;MemoryStream&lt;/span&gt;()) {&lt;br /&gt;           &lt;span style="color:navy;"&gt;try &lt;/span&gt;{&lt;br /&gt;               &lt;span style="color: rgb(166, 83, 0);"&gt;Type &lt;/span&gt;&lt;span style="color:maroon;"&gt;type &lt;/span&gt;= &lt;span style="color:maroon;"&gt;dto&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetType&lt;/span&gt;();&lt;br /&gt;               &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;type &lt;/span&gt;== &lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:maroon;"&gt;DtoGetDS&lt;/span&gt;)) {&lt;br /&gt;                   &lt;span style="color: rgb(166, 83, 0);"&gt;DataSet &lt;/span&gt;&lt;span style="color:maroon;"&gt;ds &lt;/span&gt;= &lt;span style="color:maroon;"&gt;DataCore&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetDsByMethod&lt;/span&gt;(((&lt;span style="color:maroon;"&gt;DtoGetDS&lt;/span&gt;)&lt;span style="color:maroon;"&gt;dto&lt;/span&gt;).&lt;span style="color:maroon;"&gt;MethodNameDS&lt;/span&gt;, ((&lt;span style="color:maroon;"&gt;DtoGetDS&lt;/span&gt;)&lt;span style="color:maroon;"&gt;dto&lt;/span&gt;).&lt;span style="color:maroon;"&gt;Parameters&lt;/span&gt;);&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;serializer &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:maroon;"&gt;XmlSerializer&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;DataSet&lt;/span&gt;));&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;serializer&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Serialize&lt;/span&gt;(&lt;span style="color:maroon;"&gt;memStream&lt;/span&gt;, &lt;span style="color:maroon;"&gt;ds&lt;/span&gt;);&lt;br /&gt;               }&lt;br /&gt;               &lt;span style="color:navy;"&gt;else if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;type &lt;/span&gt;== &lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:maroon;"&gt;DtoGetTable&lt;/span&gt;)) {&lt;br /&gt;                   &lt;span style="color: rgb(166, 83, 0);"&gt;DataTable &lt;/span&gt;&lt;span style="color:maroon;"&gt;tb &lt;/span&gt;= &lt;span style="color:maroon;"&gt;DataCore&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetTableByMethod&lt;/span&gt;(((&lt;span style="color:maroon;"&gt;DtoGetTable&lt;/span&gt;)&lt;span style="color:maroon;"&gt;dto&lt;/span&gt;).&lt;span style="color:maroon;"&gt;MethodNameTable&lt;/span&gt;, ((&lt;span style="color:maroon;"&gt;DtoGetTable&lt;/span&gt;)&lt;span style="color:maroon;"&gt;dto&lt;/span&gt;).&lt;span style="color:maroon;"&gt;Parameters&lt;/span&gt;);&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;serializer &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:maroon;"&gt;XmlSerializer&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;DataTable&lt;/span&gt;));&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;serializer&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Serialize&lt;/span&gt;(&lt;span style="color:maroon;"&gt;memStream&lt;/span&gt;, &lt;span style="color:maroon;"&gt;tb&lt;/span&gt;);&lt;br /&gt;               }&lt;br /&gt;               &lt;span style="color:navy;"&gt;else if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;type&lt;/span&gt;.&lt;span style="color:maroon;"&gt;BaseType &lt;/span&gt;== &lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:maroon;"&gt;DtoCommandBase&lt;/span&gt;)) {&lt;br /&gt;                   &lt;span style="color:navy;"&gt;int &lt;/span&gt;&lt;span style="color:maroon;"&gt;result &lt;/span&gt;= &lt;span style="color:maroon;"&gt;BusinessLayer&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ProcessCommand&lt;/span&gt;((&lt;span style="color:maroon;"&gt;DtoCommandBase&lt;/span&gt;)&lt;span style="color:maroon;"&gt;dto&lt;/span&gt;);&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;DtoServerAck ack &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:maroon;"&gt;DtoServerAck&lt;/span&gt;();&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;ack&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IDorRows &lt;/span&gt;= &lt;span style="color:maroon;"&gt;result&lt;/span&gt;;&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;serializer &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:maroon;"&gt;XmlSerializer&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:maroon;"&gt;DtoServerAck&lt;/span&gt;));&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;serializer&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Serialize&lt;/span&gt;(&lt;span style="color:maroon;"&gt;memStream&lt;/span&gt;, &lt;span style="color:maroon;"&gt;ack&lt;/span&gt;);&lt;br /&gt;               }&lt;br /&gt;               &lt;span style="color:navy;"&gt;else if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;type&lt;/span&gt;.&lt;span style="color:maroon;"&gt;BaseType &lt;/span&gt;== &lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:maroon;"&gt;DtoQueryBase&lt;/span&gt;)) {&lt;br /&gt;                   &lt;span style="color: rgb(166, 83, 0);"&gt;DataSet &lt;/span&gt;&lt;span style="color:maroon;"&gt;ds &lt;/span&gt;= &lt;span style="color:maroon;"&gt;BusinessLayer&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ProcessQuery&lt;/span&gt;((&lt;span style="color:maroon;"&gt;DtoQueryBase&lt;/span&gt;)&lt;span style="color:maroon;"&gt;dto&lt;/span&gt;);&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;serializer &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:maroon;"&gt;XmlSerializer&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;DataSet&lt;/span&gt;));&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;serializer&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Serialize&lt;/span&gt;(&lt;span style="color:maroon;"&gt;memStream&lt;/span&gt;, &lt;span style="color:maroon;"&gt;ds&lt;/span&gt;);&lt;br /&gt;               }&lt;br /&gt;               &lt;span style="color:navy;"&gt;else if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;type&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsGenericType &lt;/span&gt;&amp;amp;&amp;amp; &lt;span style="color:maroon;"&gt;type&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetGenericTypeDefinition&lt;/span&gt;() == &lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:maroon;"&gt;FactoryTransferObject&lt;/span&gt;&amp;lt;&amp;gt;)) {&lt;br /&gt;                   &lt;span style="color:green;"&gt;// Pass the DTO to the FactoryServer&amp;lt;T&amp;gt;&lt;br /&gt;                   &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Type &lt;/span&gt;&lt;span style="color:maroon;"&gt;factoryServerType &lt;/span&gt;= &lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:maroon;"&gt;FactoryServer&lt;/span&gt;&amp;lt;&amp;gt;);&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;factoryServerType &lt;/span&gt;= &lt;span style="color:maroon;"&gt;factoryServerType&lt;/span&gt;.&lt;span style="color:maroon;"&gt;MakeGenericType&lt;/span&gt;(&lt;span style="color:maroon;"&gt;type&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetGenericArguments&lt;/span&gt;());&lt;br /&gt;&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;MethodInfo processCommandMethod &lt;/span&gt;= &lt;span style="color:maroon;"&gt;factoryServerType&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetMethod&lt;/span&gt;("ProcessCommand", &lt;span style="color:maroon;"&gt;BindingFlags&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Public &lt;/span&gt;| &lt;span style="color:maroon;"&gt;BindingFlags&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Static&lt;/span&gt;);&lt;br /&gt;                   &lt;span style="color:maroon;"&gt;processCommandMethod&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Invoke&lt;/span&gt;(&lt;span style="color:navy;"&gt;null&lt;/span&gt;, &lt;span style="color:navy;"&gt;new object&lt;/span&gt;[] { &lt;span style="color:maroon;"&gt;memStream&lt;/span&gt;, &lt;span style="color:maroon;"&gt;dto &lt;/span&gt;});&lt;br /&gt;               }&lt;br /&gt;               &lt;span style="color:navy;"&gt;else &lt;/span&gt;{&lt;br /&gt;                   &lt;span style="color:navy;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;NotSupportedException&lt;/span&gt;(&lt;span style="color:navy;"&gt;string&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Format&lt;/span&gt;(&lt;span style="color:maroon;"&gt;Resources&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DtoNotSupportedException&lt;/span&gt;, &lt;span style="color:maroon;"&gt;type&lt;/span&gt;.&lt;span style="color:maroon;"&gt;FullName&lt;/span&gt;));&lt;br /&gt;               }&lt;br /&gt;           }&lt;br /&gt;           &lt;span style="color:navy;"&gt;catch &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Exception &lt;/span&gt;&lt;span style="color:maroon;"&gt;e&lt;/span&gt;) {&lt;br /&gt;               &lt;span style="color:maroon;"&gt;DtoException exception &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:maroon;"&gt;DtoException&lt;/span&gt;();&lt;br /&gt;               &lt;span style="color:maroon;"&gt;exception&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Message &lt;/span&gt;= &lt;span style="color:maroon;"&gt;e&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Message&lt;/span&gt;;&lt;br /&gt;               &lt;span style="color:maroon;"&gt;serializer &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:maroon;"&gt;XmlSerializer&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:maroon;"&gt;DtoException&lt;/span&gt;));&lt;br /&gt;               &lt;span style="color:maroon;"&gt;serializer&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Serialize&lt;/span&gt;(&lt;span style="color:maroon;"&gt;memStream&lt;/span&gt;, &lt;span style="color:maroon;"&gt;exception&lt;/span&gt;);&lt;br /&gt;           }&lt;br /&gt;           &lt;span style="color:maroon;"&gt;data &lt;/span&gt;= &lt;span style="color:maroon;"&gt;memStream&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ToArray&lt;/span&gt;();&lt;br /&gt;           &lt;span style="color:maroon;"&gt;RemotingClient&lt;/span&gt;.&lt;span style="color:maroon;"&gt;WriteDataToStream&lt;/span&gt;(&lt;span style="color:maroon;"&gt;netStream&lt;/span&gt;, &lt;span style="color:maroon;"&gt;data&lt;/span&gt;);&lt;br /&gt;       }&lt;br /&gt;   }&lt;span style="color:green;"&gt;//wait for the next message&lt;br /&gt;   //connection was lost.  Client probably closed program&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:maroon;"&gt;netStream&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Close&lt;/span&gt;();&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;The code is so full of anti-patterns that I wonder how it even works. But wait…I just saw a Unit Test project! Something good, finally, eh? But nah….It is a WinForm application and the test code looks like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;private void &lt;/span&gt;&lt;span style="color:maroon;"&gt;FormUnitTests_Load&lt;/span&gt;(&lt;span style="color:navy;"&gt;object &lt;/span&gt;&lt;span style="color:maroon;"&gt;sender&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;EventArgs &lt;/span&gt;&lt;span style="color:maroon;"&gt;e&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:maroon;"&gt;BenefitComputeRenewDate&lt;/span&gt;();&lt;br /&gt;   &lt;span style="color:maroon;"&gt;ToothFormatRanges&lt;/span&gt;();&lt;br /&gt;   &lt;span style="color:green;"&gt;//LabDueDate();&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:maroon;"&gt;textResults&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Text &lt;/span&gt;+= "Done.";&lt;br /&gt;   &lt;span style="color:maroon;"&gt;textResults&lt;/span&gt;.&lt;span style="color:maroon;"&gt;SelectionStart &lt;/span&gt;= &lt;span style="color:maroon;"&gt;textResults&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Text&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Length&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;private void &lt;/span&gt;&lt;span style="color:maroon;"&gt;BenefitComputeRenewDate&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;asofDate &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;2006&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;3&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;19&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color:navy;"&gt;bool &lt;/span&gt;&lt;span style="color:maroon;"&gt;isCalendarYear &lt;/span&gt;= &lt;span style="color:navy;"&gt;true&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;insStartDate &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;2003&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;3&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;1&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;result &lt;/span&gt;= &lt;span style="color:maroon;"&gt;BenefitLogic&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ComputeRenewDate&lt;/span&gt;(&lt;span style="color:maroon;"&gt;asofDate&lt;/span&gt;, &lt;span style="color:maroon;"&gt;isCalendarYear&lt;/span&gt;, &lt;span style="color:maroon;"&gt;insStartDate&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;result &lt;/span&gt;!= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;2006&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;1&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;1&lt;/span&gt;))&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:maroon;"&gt;textResults&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Text &lt;/span&gt;+= "BenefitComputeRenewDate 1 failed.\r\n";&lt;br /&gt;   }&lt;br /&gt;   &lt;span style="color:maroon;"&gt;isCalendarYear &lt;/span&gt;= &lt;span style="color:navy;"&gt;false&lt;/span&gt;; &lt;span style="color:green;"&gt;//for the remaining tests&lt;br /&gt;   //earlier in same month&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:maroon;"&gt;result &lt;/span&gt;= &lt;span style="color:maroon;"&gt;BenefitLogic&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ComputeRenewDate&lt;/span&gt;(&lt;span style="color:maroon;"&gt;asofDate&lt;/span&gt;, &lt;span style="color:maroon;"&gt;isCalendarYear&lt;/span&gt;, &lt;span style="color:maroon;"&gt;insStartDate&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;result &lt;/span&gt;!= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;2006&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;3&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;1&lt;/span&gt;))&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:maroon;"&gt;textResults&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Text &lt;/span&gt;+= "BenefitComputeRenewDate 2 failed.\r\n";&lt;br /&gt;   }&lt;br /&gt;   &lt;span style="color:green;"&gt;//earlier month in year&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:maroon;"&gt;asofDate &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;2006&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;5&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;1&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color:maroon;"&gt;result &lt;/span&gt;= &lt;span style="color:maroon;"&gt;BenefitLogic&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ComputeRenewDate&lt;/span&gt;(&lt;span style="color:maroon;"&gt;asofDate&lt;/span&gt;, &lt;span style="color:maroon;"&gt;isCalendarYear&lt;/span&gt;, &lt;span style="color:maroon;"&gt;insStartDate&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;result &lt;/span&gt;!= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;2006&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;3&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;1&lt;/span&gt;))&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:maroon;"&gt;textResults&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Text &lt;/span&gt;+= "BenefitComputeRenewDate 3 failed.\r\n";&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;I think that’s enough proof for one day. You probably have heard the saying: “Even a chimp can write code”, but sometimes it’s like: “Even chimp writes code better than that”.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-1652479209163059441?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/1652479209163059441/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=1652479209163059441' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1652479209163059441'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1652479209163059441'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/04/code-review-no-n-or-do-chimps-write.html' title='Code Review No. N, or Do chimps write code better than us?'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-7878290965172646944</id><published>2009-04-26T15:57:00.002+04:30</published><updated>2009-04-26T16:46:55.931+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='FarsiLibrary'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET MVC'/><category scheme='http://www.blogger.com/atom/ns#' term='Calendars'/><title type='text'>Dynamically Generated Images with ASP.NET MVC</title><content type='html'>&lt;p&gt;For a site I’m working on using ASP.NET MVC, I intended to place a Date badge beside the blog and news posts I’m writing. Since in ASP.NET MVC there’s no notion of custom controls (well, at least not like in WebForms) you’ll have to do this manually, but as it turned out it was pretty easy to do.&lt;/p&gt;  &lt;p&gt;What I needed to do was to convert a Date instance, e.g. 04.23.2009 to a user friendly calendar icon like this one:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Z5KTIfnfuNs/SfRPHgU55rI/AAAAAAAAAN0/Vh0wfwsvIow/s1600-h/CalendarIcon3.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="CalendarIcon" alt="CalendarIcon" src="http://lh5.ggpht.com/_Z5KTIfnfuNs/SfRPJsWuV5I/AAAAAAAAAN4/LFz8ONgu5ko/CalendarIcon_thumb1.png?imgmax=800" width="46" border="0" height="51" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I’ve seen blogs and site using different icon for each day of the month or a similar trick to do this, but why not actually render it using Graphics API and a picture?&lt;/p&gt;  &lt;p&gt;The first step was to decide what should be returned in you Controller’s action method, as the action result? Since there’s nothing that returns an Image as the result, let’s create one:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ImageResult &lt;/span&gt;: &lt;span style="color: rgb(166, 83, 0);"&gt;ActionResult&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Image &lt;/span&gt;&lt;span style="color:maroon;"&gt;Image&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat &lt;/span&gt;&lt;span style="color:maroon;"&gt;ImageFormat&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;private static &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;, &lt;span style="color:navy;"&gt;string&lt;/span&gt;&amp;gt; &lt;span style="color:maroon;"&gt;FormatMap&lt;br /&gt;  &lt;/span&gt;{&lt;br /&gt;      &lt;span style="color:navy;"&gt;get&lt;/span&gt;; &lt;span style="color:navy;"&gt;set&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;static &lt;/span&gt;&lt;span style="color:maroon;"&gt;ImageResult&lt;/span&gt;()&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:maroon;"&gt;CreateContentTypeMap&lt;/span&gt;();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;public override void &lt;/span&gt;&lt;span style="color:maroon;"&gt;ExecuteResult&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ControllerContext &lt;/span&gt;&lt;span style="color:maroon;"&gt;context&lt;/span&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;Image &lt;/span&gt;== &lt;span style="color:navy;"&gt;null&lt;/span&gt;) &lt;span style="color:navy;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ArgumentNullException&lt;/span&gt;("Image");&lt;br /&gt;      &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;ImageFormat &lt;/span&gt;== &lt;span style="color:navy;"&gt;null&lt;/span&gt;) &lt;span style="color:navy;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ArgumentNullException&lt;/span&gt;("ImageFormat");&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:maroon;"&gt;context&lt;/span&gt;.&lt;span style="color:maroon;"&gt;HttpContext&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Response&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Clear&lt;/span&gt;();&lt;br /&gt;      &lt;span style="color:maroon;"&gt;context&lt;/span&gt;.&lt;span style="color:maroon;"&gt;HttpContext&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Response&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ContentType &lt;/span&gt;= &lt;span style="color:maroon;"&gt;FormatMap&lt;/span&gt;[&lt;span style="color:maroon;"&gt;ImageFormat&lt;/span&gt;];&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:maroon;"&gt;Image&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Save&lt;/span&gt;(&lt;span style="color:maroon;"&gt;context&lt;/span&gt;.&lt;span style="color:maroon;"&gt;HttpContext&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Response&lt;/span&gt;.&lt;span style="color:maroon;"&gt;OutputStream&lt;/span&gt;, &lt;span style="color:maroon;"&gt;ImageFormat&lt;/span&gt;);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;private static void &lt;/span&gt;&lt;span style="color:maroon;"&gt;CreateContentTypeMap&lt;/span&gt;()&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:maroon;"&gt;FormatMap &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;, &lt;span style="color:navy;"&gt;string&lt;/span&gt;&amp;gt;&lt;br /&gt;      {&lt;br /&gt;          { &lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Bmp&lt;/span&gt;,  "image/bmp"                },&lt;br /&gt;          { &lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Gif&lt;/span&gt;,  "image/gif"                },&lt;br /&gt;          { &lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Icon&lt;/span&gt;, "image/vnd.microsoft.icon" },&lt;br /&gt;          { &lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Jpeg&lt;/span&gt;, "image/Jpeg"               },&lt;br /&gt;          { &lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Png&lt;/span&gt;,  "image/png"                },&lt;br /&gt;          { &lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Tiff&lt;/span&gt;, "image/tiff"               },&lt;br /&gt;          { &lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Wmf&lt;/span&gt;,  "image/wmf"                }&lt;br /&gt;      };&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;pretty easy, ha? You just need to specify the image and the format and it is rendered to the HttpContext as an image. The controller action to render the text would something like this:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ActionResult &lt;/span&gt;&lt;span style="color:maroon;"&gt;GetCalendarBadge&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;displayDate&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color: rgb(166, 83, 0);"&gt;Graphics &lt;/span&gt;&lt;span style="color:maroon;"&gt;graphics &lt;/span&gt;= ?;&lt;br /&gt;  &lt;span style="color: rgb(166, 83, 0);"&gt;Bitmap &lt;/span&gt;&lt;span style="color:maroon;"&gt;bmp &lt;/span&gt;= ?;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:green;"&gt;//Draw using graphics&lt;br /&gt;&lt;br /&gt;  //Direct the output to the bitmap&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:navy;"&gt;return new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ImageResult &lt;/span&gt;{ &lt;span style="color:maroon;"&gt;Image &lt;/span&gt;= &lt;span style="color:maroon;"&gt;bmp&lt;/span&gt;, &lt;span style="color:maroon;"&gt;ImageFormat &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Png &lt;/span&gt;};&lt;br /&gt;}&lt;/pre&gt;and on the view:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color:navy;"&gt;foreach&lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;item &lt;/span&gt;&lt;span style="color:navy;"&gt;in this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Model&lt;/span&gt;.&lt;span style="color:maroon;"&gt;News&lt;/span&gt;) &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;     &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:maroon;"&gt;Html&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Image&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;NewsController&lt;/span&gt;&amp;gt;(&lt;span style="color:maroon;"&gt;o &lt;/span&gt;=&amp;gt; &lt;span style="color:maroon;"&gt;o&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetCalendarBadge&lt;/span&gt;(&lt;span style="color:maroon;"&gt;item&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DisplayDate&lt;/span&gt;), &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;100&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;100&lt;/span&gt;) &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&amp;lt;%&lt;/span&gt; } &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://lh6.ggpht.com/_Z5KTIfnfuNs/SfRPL2NGqZI/AAAAAAAAAN8/SLmInKZkrKA/s1600-h/Calendar%5B4%5D.png"&gt;&lt;img style="border: 0px none ; margin: 0px 15px 0px 0px; display: inline;" title="Calendar" alt="Calendar" src="http://lh6.ggpht.com/_Z5KTIfnfuNs/SfRPN0r2QLI/AAAAAAAAAOA/eN7vVPK8XTY/Calendar_thumb%5B2%5D.png?imgmax=800" width="90" align="left" border="0" height="83" /&gt;&lt;/a&gt;Now, to make things easier, let’s use an existing bitmap as our canvas and just draw the values on it. To do this, add an existing image to your project and load it. The rest is just GDI API that renders the date values according to user’s Cultural setting.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ActionResult &lt;/span&gt;&lt;span style="color:maroon;"&gt;GetCalendarBadge&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;displayDate&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;bmp &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;Images&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Calendar&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;g &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;Graphics&lt;/span&gt;.&lt;span style="color:maroon;"&gt;FromImage&lt;/span&gt;(&lt;span style="color:maroon;"&gt;bmp&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;using &lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;genericFormat &lt;/span&gt;= &lt;span style="color:maroon;"&gt;GetStringFormat&lt;/span&gt;())&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;yearRect &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Rectangle&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;24&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;13&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;40&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;15&lt;/span&gt;);&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;dayOfMonthRect &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Rectangle&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;10&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;29&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;70&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;44&lt;/span&gt;);&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;dayNameRect &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Rectangle&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;10&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;30&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;70&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;10&lt;/span&gt;);&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;monthNameRect &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Rectangle&lt;/span&gt;(&lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;10&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;61&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;70&lt;/span&gt;, &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;10&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:navy;"&gt;using&lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;headerFont &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Font&lt;/span&gt;("Tahoma", &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;7.5f&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;FontStyle&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Regular&lt;/span&gt;))&lt;br /&gt;      &lt;span style="color:navy;"&gt;using&lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;footerFont &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Font&lt;/span&gt;("Tahoma", &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;7.5f&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;FontStyle&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Regular&lt;/span&gt;))&lt;br /&gt;      &lt;span style="color:navy;"&gt;using &lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;dayFont &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Font&lt;/span&gt;("Tahoma", &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;14&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;FontStyle&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Bold&lt;/span&gt;))&lt;br /&gt;      {&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;day &lt;/span&gt;= &lt;span style="color:maroon;"&gt;CurrentCulture&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Calendar&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetDayOfMonth&lt;/span&gt;(&lt;span style="color:maroon;"&gt;displayDate&lt;/span&gt;).&lt;span style="color:maroon;"&gt;ToString&lt;/span&gt;();&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;month &lt;/span&gt;= &lt;span style="color:maroon;"&gt;CurrentCulture&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Calendar&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetMonth&lt;/span&gt;(&lt;span style="color:maroon;"&gt;displayDate&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;year &lt;/span&gt;= &lt;span style="color:maroon;"&gt;CurrentCulture&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Calendar&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetYear&lt;/span&gt;(&lt;span style="color:maroon;"&gt;displayDate&lt;/span&gt;).&lt;span style="color:maroon;"&gt;ToString&lt;/span&gt;();&lt;br /&gt;        &lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;weekDay &lt;/span&gt;= &lt;span style="color:maroon;"&gt;CurrentCulture&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Calendar&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetDayOfWeek&lt;/span&gt;(&lt;span style="color:maroon;"&gt;displayDate&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;dayName &lt;/span&gt;= &lt;span style="color:maroon;"&gt;CurrentCulture&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DateTimeFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetDayName&lt;/span&gt;(&lt;span style="color:maroon;"&gt;weekDay&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;monthName &lt;/span&gt;= &lt;span style="color:maroon;"&gt;CurrentCulture&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DateTimeFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetAbbreviatedMonthName&lt;/span&gt;(&lt;span style="color:maroon;"&gt;month&lt;/span&gt;);&lt;br /&gt;        &lt;br /&gt;          &lt;span style="color:maroon;"&gt;g&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DrawString&lt;/span&gt;(&lt;span style="color:maroon;"&gt;year&lt;/span&gt;, &lt;span style="color:maroon;"&gt;headerFont&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;Brushes&lt;/span&gt;.&lt;span style="color:maroon;"&gt;White&lt;/span&gt;, &lt;span style="color:maroon;"&gt;yearRect&lt;/span&gt;, &lt;span style="color:maroon;"&gt;genericFormat&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:maroon;"&gt;g&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DrawString&lt;/span&gt;(&lt;span style="color:maroon;"&gt;day&lt;/span&gt;, &lt;span style="color:maroon;"&gt;dayFont&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;Brushes&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Black&lt;/span&gt;, &lt;span style="color:maroon;"&gt;dayOfMonthRect&lt;/span&gt;, &lt;span style="color:maroon;"&gt;genericFormat&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:maroon;"&gt;g&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DrawString&lt;/span&gt;(&lt;span style="color:maroon;"&gt;dayName&lt;/span&gt;, &lt;span style="color:maroon;"&gt;footerFont&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;Brushes&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Black&lt;/span&gt;, &lt;span style="color:maroon;"&gt;dayNameRect&lt;/span&gt;, &lt;span style="color:maroon;"&gt;genericFormat&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:maroon;"&gt;g&lt;/span&gt;.&lt;span style="color:maroon;"&gt;DrawString&lt;/span&gt;(&lt;span style="color:maroon;"&gt;monthName&lt;/span&gt;, &lt;span style="color:maroon;"&gt;footerFont&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;Brushes&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Black&lt;/span&gt;, &lt;span style="color:maroon;"&gt;monthNameRect&lt;/span&gt;, &lt;span style="color:maroon;"&gt;genericFormat&lt;/span&gt;);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;return new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ImageResult &lt;/span&gt;{ &lt;span style="color:maroon;"&gt;Image &lt;/span&gt;= &lt;span style="color:maroon;"&gt;bmp&lt;/span&gt;, &lt;span style="color:maroon;"&gt;ImageFormat &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;ImageFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Png &lt;/span&gt;};&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://lh6.ggpht.com/_Z5KTIfnfuNs/SfRPQPntsmI/AAAAAAAAAOE/gz3ScPNikS0/s1600-h/CalendarBadge%5B14%5D.png"&gt;&lt;img style="border: 0px none ; display: inline; margin-left: 0px; margin-right: 0px;" title="CalendarBadge" alt="CalendarBadge" src="http://lh4.ggpht.com/_Z5KTIfnfuNs/SfRPUGljIaI/AAAAAAAAAOI/ytgRZXKpubI/CalendarBadge_thumb%5B12%5D.png?imgmax=800" width="436" border="0" height="244" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Note that CurrentCulture property returns the running user’s CultureInfo which will help “translating” the date value correctly for different Cultures. What we achieved is a nice calendar badge with render date values and it is not even constrained to our Canvas size. Hope this helps.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-7878290965172646944?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/7878290965172646944/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=7878290965172646944' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7878290965172646944'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7878290965172646944'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/04/dynamically-generated-images-with.html' title='Dynamically Generated Images with ASP.NET MVC'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_Z5KTIfnfuNs/SfRPJsWuV5I/AAAAAAAAAN4/LFz8ONgu5ko/s72-c/CalendarIcon_thumb1.png?imgmax=800' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-3858265667493731655</id><published>2009-04-26T15:30:00.001+04:30</published><updated>2009-04-26T15:43:31.007+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET MVC'/><category scheme='http://www.blogger.com/atom/ns#' term='Windsor'/><title type='text'>Using Windsor IoC in ASP.NET MVC</title><content type='html'>&lt;p&gt;With the power of an IoC engine, you can inject your repositories, services, etc. when building applications using ASP.NET MVC framework. Since one of the best IoC engines out there IMHO is Castle Windsor using it has become natural more than ever, but as soon as I did, strange problems occurred when testing very simple scenarios. Two major pains were:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Model Binders&lt;/strong&gt;     &lt;br /&gt;ModelBinders automatically bind form values to your object properties. When using Windsor, I noticed that almost all the parameter values are null when passed to the controller action.     &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;Parameter Values&lt;/strong&gt;     &lt;br /&gt;When a controller action was called with a parameter for the first time, all subsequent calls somehow cached the parameter values. Type and Value of the parameter has no effect, and even disabling caching didn’t work.    &lt;br /&gt;    &lt;br /&gt;I’ve been pulling my hair for a couple of days (yeah, I kinda went bald because of this) before I figure this out, and when I thinking about it, the reason of this behavior is clearly obvious! &lt;/p&gt;  &lt;h4&gt;&lt;em&gt;Windsor uses Singleton lifetime for registered services. All you need to do when registering controllers, is to use Transient lifetime!&lt;/em&gt;&lt;/h4&gt;  &lt;pre class="code"&gt;&lt;span style="color: maroon"&gt;Container&lt;/span&gt;.&lt;span style="color: maroon"&gt;Register&lt;/span&gt;(&lt;span style="color: #a65300"&gt;AllTypes&lt;/span&gt;.&lt;span style="color: maroon"&gt;FromAssemblyContaining&lt;/span&gt;&amp;lt;&lt;span style="color: #a65300"&gt;MvcApplication&lt;/span&gt;&amp;gt;()&lt;br /&gt;                           .&lt;span style="color: maroon"&gt;Where&lt;/span&gt;(&lt;span style="color: maroon"&gt;o &lt;/span&gt;=&amp;gt; &lt;span style="color: maroon"&gt;o&lt;/span&gt;.&lt;span style="color: maroon"&gt;Namespace &lt;/span&gt;== &lt;span style="color: navy"&gt;typeof&lt;/span&gt;(&lt;span style="color: #a65300"&gt;HomeController&lt;/span&gt;).&lt;span style="color: maroon"&gt;Namespace&lt;/span&gt;)&lt;br /&gt;                           .&lt;span style="color: maroon"&gt;Configure&lt;/span&gt;(&lt;span style="color: maroon"&gt;o &lt;/span&gt;=&amp;gt; &lt;span style="color: maroon"&gt;o&lt;/span&gt;.&lt;span style="color: maroon"&gt;LifeStyle&lt;/span&gt;.&lt;span style="color: maroon"&gt;Is&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;LifestyleType&lt;/span&gt;.&lt;span style="color: maroon"&gt;Transient&lt;/span&gt;)));&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Problem solved, Case closed! &lt;br /&gt;&lt;br /&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-3858265667493731655?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/3858265667493731655/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=3858265667493731655' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/3858265667493731655'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/3858265667493731655'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/04/using-windsor-ioc-in-aspnet-mvc.html' title='Using Windsor IoC in ASP.NET MVC'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-1139563326852900807</id><published>2009-04-23T11:17:00.001+04:30</published><updated>2009-04-23T11:17:27.169+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET MVC'/><title type='text'>ASP.NET MVC : Best way to go?</title><content type='html'>&lt;p&gt;With total control over html rendering in ASP.NET MVC there are a lot of things you can do, and this actually is correct the other way around, meaning you have to do a lot of things yourself: No fancy ASP.NET Custom Controls. It somehow reminds me of the old days, when you had to do everything to display a list of data. Back then, everything seemed manual. You had to emit html, control page state, etc. but with the rise of ASP.NET everything was amazingly done for you behind the scene. You no longer had to waste your time on the basics and you could focus on the problem at hand. Great, right?&lt;/p&gt;  &lt;p&gt;Now with ASP.NET MVC, you might think we’re back where we started, but I wouldn’t go that far. While ASP.NET MVC offers control, maintainability and simplicity it also provides you &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSeparation_of_concerns&amp;amp;ei=5QzwSev2IpnNjAfB94HWDA&amp;amp;usg=AFQjCNFjPCAoeZtZUsEK1uTqlMlgN2QTOg"&gt;Separation of Concerns&lt;/a&gt; which [ is one of the things that] was lacking in Classic ASP.&lt;/p&gt;  &lt;p&gt;The question is, should you use it, instead of well established ASP.NET WebForms? That depends. Are you working on a &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FGreenfield_project&amp;amp;ei=-g3wSdmlNMOQjAfhxMm0DA&amp;amp;usg=AFQjCNHI5BpCIpAX_d_EVYfs4d8nReJ_LA"&gt;greenfield&lt;/a&gt; project? Are you eager to learn the new stuff? Are you looking for more control and power on what the actual html outcome is? If you answer yes to these questions then you definitely need to use ASP.NET MVC for your project.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-1139563326852900807?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/1139563326852900807/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=1139563326852900807' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1139563326852900807'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1139563326852900807'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/04/aspnet-mvc-best-way-to-go.html' title='ASP.NET MVC : Best way to go?'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-9112482127803738956</id><published>2009-04-22T12:50:00.002+04:30</published><updated>2009-04-22T12:57:51.272+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Testing'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET MVC'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'>ASP.NET MVC and Testing FilterActions</title><content type='html'>&lt;p&gt;I’m preparing my website, which benefits NHibernate and ASP.NET MVC so I finally got a chance to actually do something with this nice pair. Since the pattern of SessionPerRequest and TransactionPerRequest is useful, I intended to automagically create a new session upon activating my Controller’s action, where necessary, since this will make your session management code  separate from your controller’s code and you no longer need to worry about it. This part is very easy to do thanks to ActionFilterAttributes in ASP.NET MVC:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;TransactionPerRequest &lt;/span&gt;: &lt;span style="color: rgb(166, 83, 0);"&gt;ActionFilterAttribute&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;public override void &lt;/span&gt;&lt;span style="color:maroon;"&gt;OnActionExecuting&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ActionExecutingContext &lt;/span&gt;&lt;span style="color:maroon;"&gt;filterContext&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color: rgb(166, 83, 0);"&gt;NHibernateSessionHolder&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Current&lt;/span&gt;.&lt;span style="color:maroon;"&gt;BeginTransaction&lt;/span&gt;();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;public override void &lt;/span&gt;&lt;span style="color:maroon;"&gt;OnResultExecuted&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;ResultExecutedContext &lt;/span&gt;&lt;span style="color:maroon;"&gt;filterContext&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;if&lt;/span&gt;(&lt;span style="color:maroon;"&gt;filterContext&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Exception &lt;/span&gt;== &lt;span style="color:navy;"&gt;null&lt;/span&gt;)&lt;br /&gt;       {&lt;br /&gt;           &lt;span style="color:maroon;"&gt;Commit&lt;/span&gt;();&lt;br /&gt;       }&lt;br /&gt;       &lt;span style="color:navy;"&gt;else&lt;br /&gt;       &lt;/span&gt;{&lt;br /&gt;           &lt;span style="color:maroon;"&gt;Rollback&lt;/span&gt;();&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;protected virtual void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Commit&lt;/span&gt;()&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;NHibernateSessionHolder&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Current&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Transaction&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsActive&lt;/span&gt;)&lt;br /&gt;       {&lt;br /&gt;           &lt;span style="color: rgb(166, 83, 0);"&gt;NHibernateSessionHolder&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Current&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Transaction&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Commit&lt;/span&gt;();&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;protected virtual void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Rollback&lt;/span&gt;()&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;NHibernateSessionHolder&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Current&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Transaction&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsActive&lt;/span&gt;)&lt;br /&gt;       {&lt;br /&gt;           &lt;span style="color: rgb(166, 83, 0);"&gt;NHibernateSessionHolder&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Current&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Transaction&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Rollback&lt;/span&gt;();&lt;br /&gt;       }           &lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;NHibernateSessionHolder is actually a static class and is initialized in Global.asax when application is started. Is stores the session in current HttpContext object.&lt;/p&gt;&lt;p&gt;So, how do we test this, you might ask? This leads to another question that how do you generally test ActionFilterAttributes? There are two ways to achieve this.&lt;br /&gt;&lt;br /&gt;If you can test the filter attribute’s behavior in your controller, the easiest way would be to directly call the action on the controller. You need to create the route values and use the ControllerActionInvoker to call the action of your controller. Also mocking HttpContext is preferred for obvious reasons. My test case would look like this:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;TestMethod&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Transaction_Is_Automatically_Opened_Using_Transaction_Per_Request&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;httpContext &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;HttpContextBase&lt;/span&gt;&amp;gt;().&lt;span style="color:maroon;"&gt;Object&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;controller &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;AccountController&lt;/span&gt;();&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;controllerContext &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ControllerContext&lt;/span&gt;(&lt;span style="color:maroon;"&gt;httpContext&lt;/span&gt;, &lt;span style="color:maroon;"&gt;controller&lt;/span&gt;.&lt;span style="color:maroon;"&gt;GetCreateActionRouteData&lt;/span&gt;(), &lt;span style="color:maroon;"&gt;controller&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;controllerInvoker &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ControllerActionInvoker&lt;/span&gt;();&lt;br /&gt;  &lt;br /&gt;   &lt;span style="color:maroon;"&gt;controllerInvoker&lt;/span&gt;.&lt;span style="color:maroon;"&gt;InvokeAction&lt;/span&gt;(&lt;span style="color:maroon;"&gt;controllerContext&lt;/span&gt;, "Create");&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(166, 83, 0);"&gt;Assert&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsTrue&lt;/span&gt;(&lt;span style="color:maroon;"&gt;controller&lt;/span&gt;.&lt;span style="color:maroon;"&gt;TransactinWasCreated&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;But sometimes, you need to test the internal behavior of your filter attribute and it would not be possible doing so in your Controller classes. Let’s say, you want to check if the OnResultExecuted method handles exception properly by rolling back the transaction. To do it this way, you need to create or mock ActionExecutingContext and ResultExecutedContext:&lt;br /&gt;&lt;pre class="code"&gt;[&lt;span style="color: rgb(166, 83, 0);"&gt;TestMethod&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Transaction_Is_Automatically_Rolled_Back_If_Exception_Thrown&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;httpContext &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;HttpContextBase&lt;/span&gt;&amp;gt;().&lt;span style="color:maroon;"&gt;Object&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;actionDescriptor &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;ActionDescriptor&lt;/span&gt;&amp;gt;().&lt;span style="color:maroon;"&gt;Object&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;actionResult &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;ActionResult&lt;/span&gt;&amp;gt;().&lt;span style="color:maroon;"&gt;Object&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;controller &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;AccountController&lt;/span&gt;();&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;controllerContext &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ControllerContext&lt;/span&gt;(&lt;span style="color:maroon;"&gt;httpContext&lt;/span&gt;, &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;RouteData&lt;/span&gt;(), &lt;span style="color:maroon;"&gt;controller&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;filterContext &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ActionExecutingContext&lt;/span&gt;(&lt;span style="color:maroon;"&gt;controllerContext&lt;/span&gt;, &lt;span style="color:maroon;"&gt;actionDescriptor&lt;/span&gt;, &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;RouteValueDictionary&lt;/span&gt;());&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;resultContext &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ResultExecutedContext&lt;/span&gt;(&lt;span style="color:maroon;"&gt;controllerContext&lt;/span&gt;, &lt;span style="color:maroon;"&gt;actionResult&lt;/span&gt;, &lt;span style="color:navy;"&gt;false&lt;/span&gt;, &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Exception&lt;/span&gt;("an exception is thrown"));&lt;br /&gt;   &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;transactionAttrib &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;TestableTransactionAttribute&lt;/span&gt;();&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:maroon;"&gt;transactionAttrib&lt;/span&gt;.&lt;span style="color:maroon;"&gt;OnActionExecuting&lt;/span&gt;(&lt;span style="color:maroon;"&gt;filterContext&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color:maroon;"&gt;transactionAttrib&lt;/span&gt;.&lt;span style="color:maroon;"&gt;OnResultExecuted&lt;/span&gt;(&lt;span style="color:maroon;"&gt;resultContext&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(166, 83, 0);"&gt;Assert&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsFalse&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;NHibernateSessionHolder&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Current&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Transaction&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsActive&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color: rgb(166, 83, 0);"&gt;Assert&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsFalse&lt;/span&gt;(&lt;span style="color:maroon;"&gt;transactionAttrib&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsCommitted&lt;/span&gt;);&lt;br /&gt;   &lt;span style="color: rgb(166, 83, 0);"&gt;Assert&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsTrue&lt;/span&gt;(&lt;span style="color:maroon;"&gt;transactionAttrib&lt;/span&gt;.&lt;span style="color:maroon;"&gt;IsRolledback&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-9112482127803738956?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/9112482127803738956/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=9112482127803738956' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/9112482127803738956'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/9112482127803738956'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/04/aspnet-mvc-and-testing-filteractions.html' title='ASP.NET MVC and Testing FilterActions'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-2776776824505042287</id><published>2009-04-13T17:31:00.005+04:30</published><updated>2009-04-13T17:38:13.632+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Security'/><category scheme='http://www.blogger.com/atom/ns#' term='Cracking'/><title type='text'>Key-Gen for .NET Apps!</title><content type='html'>&lt;p&gt;You all know that .NET generated application convert the high-level codes from source language (e.g. C#) and converts them to IL. Basically, if you could convert the IL code back to the high-level language, you’d have the original source code of the application, and to some extent, you can do this, but this is the story for another post. Today, I’d want to show you how your public licensing API would provide a very easy way to crack open your own application. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: I take no responsibility for how you use the piece of information. By reading these instructions  you accept the sole responsibility of any illegal use. The names and information provided here are changed to save the innocent.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Suppose you’ve put a lot of energy and time and written your state-of-the-art application and you’ve released it to the market. After a while some junior software developer tries to inspect your assembly to see how you’ve managed to do a special tricks or two. (Mind you, that’s not what I’d suggest you do, dear reader, because you might end-up facing copyright infringement lawsuits). Now when he’s inspecting the API, he encounters your licensing API, and even worst, those API are public:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;RSALicenseCodec &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IEncoder&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;IDecoder&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;}&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;License&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;&lt;span style="color:navy;"&gt;   public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Guid &lt;/span&gt;&lt;span style="color:maroon;"&gt;LicenseId &lt;/span&gt;= &lt;span style="color: rgb(43, 145, 175);"&gt;Guid&lt;/span&gt;.&lt;span style="color:maroon;"&gt;NewGuid&lt;/span&gt;();&lt;br /&gt;&lt;span style="color:navy;"&gt;   public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;EndTime&lt;/span&gt;;&lt;span style="color:navy;"&gt;   public string &lt;/span&gt;&lt;span style="color:maroon;"&gt;LicensedTo&lt;/span&gt;;&lt;br /&gt;&lt;span style="color:navy;"&gt;   public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;PurchaseDate&lt;/span&gt;;&lt;br /&gt;&lt;span style="color:navy;"&gt;   public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;LicenseType &lt;/span&gt;&lt;span style="color:maroon;"&gt;Type&lt;/span&gt;;&lt;br /&gt;&lt;span style="color:navy;"&gt;   public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime &lt;/span&gt;&lt;span style="color:maroon;"&gt;StartTime&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;public static string &lt;/span&gt;&lt;span style="color:maroon;"&gt;LicenseToKey&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IEncoder &lt;/span&gt;&lt;span style="color:maroon;"&gt;encoder&lt;/span&gt;, &lt;span style="color: rgb(166, 83, 0);"&gt;License &lt;/span&gt;&lt;span style="color:maroon;"&gt;license&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:navy;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;License &lt;/span&gt;&lt;span style="color:maroon;"&gt;KeyToLicense&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IDecoder &lt;/span&gt;&lt;span style="color:maroon;"&gt;decoder&lt;/span&gt;, &lt;span style="color:navy;"&gt;string &lt;/span&gt;&lt;span style="color:maroon;"&gt;key&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: Actual implementation was cut off!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Z5KTIfnfuNs/SeM3oQHECXI/AAAAAAAAANo/1VHcQuUrtKA/s1600-h/Licensing-API%5B4%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="Licensing-API" alt="Licensing-API" src="http://lh6.ggpht.com/_Z5KTIfnfuNs/SeM3pRERlsI/AAAAAAAAANs/ZD6epIBV_3M/Licensing-API_thumb%5B2%5D.png?imgmax=800" width="305" border="0" height="109" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Now with all these public API, only thing between a novice developer with bad intensions and a perfect key-gen to for application, is the copyright infringement lawsuit! Do you think that alone is enough?&lt;/p&gt;&lt;p&gt;I don’t want to give you the idea that by only making these API private you’re safe, no. There are a lot of things you should do before you’re even close to being safe with hackers and crackers, but in my opinion taking all the care would no save you either. Almost nothing can stop a motivated cracker.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-2776776824505042287?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/2776776824505042287/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=2776776824505042287' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2776776824505042287'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2776776824505042287'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/04/key-gen-for-net-apps.html' title='Key-Gen for .NET Apps!'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_Z5KTIfnfuNs/SeM3pRERlsI/AAAAAAAAANs/ZD6epIBV_3M/s72-c/Licensing-API_thumb%5B2%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-6504799661695001720</id><published>2009-04-09T09:31:00.001+04:30</published><updated>2009-04-09T09:31:29.555+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Resharper'/><title type='text'>Resharper 4.5 Released</title><content type='html'>&lt;p&gt;Great guys at &lt;a href="http://www.jetbrains.com/"&gt;JetBrains&lt;/a&gt; have release next version of their productivity tool, Resharper. It seems this release fixes performance and memory usage issues and performance is tuned for big projects. &lt;a href="http://www.jetbrains.com/resharper/features/newfeatures.html"&gt;According&lt;/a&gt; to JetBrains, following features are significantly faster now:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Renaming Symbols&lt;/li&gt;    &lt;li&gt;Finding Usages&lt;/li&gt;    &lt;li&gt;Creating Symbol from Usage&lt;/li&gt;    &lt;li&gt;Analyzing large XAML files&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I’ve been using their nightly builds for a while and there are a couple of nice new features added to version 4.5. Things like enforcing a naming convention, native support for MSTest unit testing, and copying XML comment from existing (base class / interfaces) sources are some of them.&amp;#160; To give it a try, download a trial copy &lt;a href="http://www.jetbrains.com/resharper/download/index.html"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-6504799661695001720?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/6504799661695001720/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=6504799661695001720' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6504799661695001720'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6504799661695001720'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/04/resharper-45-released.html' title='Resharper 4.5 Released'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-2069416705337892587</id><published>2009-04-05T15:37:00.001+04:30</published><updated>2009-04-05T15:37:23.327+04:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Windows 7'/><title type='text'>Windows Seven and Sidebar Gadgets</title><content type='html'>&lt;p&gt;I’ve been using Windows 7 for a while now and my test experience is nearly great, with some exceptions like for occasional NVidia driver crashing the kernel and a few restarts because of it. The other day, I found something new : my sidebar gadget was missing completely! After searching for it and googling, it turns out disable UAC altogether (setting to “Never Notify”) will make sidebar completely disappear and as soon as you bring it up only one notch (setting it to “do not dim my desktop”) will automagically bring it back after a restart.&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;&lt;a href="http://lh6.ggpht.com/_Z5KTIfnfuNs/SdiQzXmgqJI/AAAAAAAAANg/Or96dnrQDKA/s1600-h/UAC%5B5%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="UAC" border="0" alt="UAC" src="http://lh4.ggpht.com/_Z5KTIfnfuNs/SdiQ5WkuPQI/AAAAAAAAANk/VBJLsMEQDRM/UAC_thumb%5B3%5D.png?imgmax=800" width="458" height="299" /&gt;&lt;/a&gt;     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;Luckily for me, guys on &lt;a href="http://www.mydigitallife.info"&gt;My Digital Life&lt;/a&gt; forum have a hack to get this going :&lt;/p&gt;  &lt;p&gt;1- Close all gadgets.&lt;/p&gt;  &lt;p&gt;2- Take the ownership of the Gadget folder and grant permission to Administrators (folder is located on “WindowsDrive:\Program Files\Windows Sidebar)&lt;/p&gt;  &lt;p&gt;3- There are a couple of file here, create a backup copy.&lt;/p&gt;  &lt;p&gt;4- Download the patched files &lt;a href="http://www.ziddu.com/download/3042059/Win7-Sidebar-Fix.zip.html"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;5- Overwrite the existing files with the ones in the zip file.&lt;/p&gt;  &lt;p&gt;6- Re-add all your gadgets to your desktop.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;u&gt;&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;u&gt;Note that this is a patch. It worked on my machine but use it at your own risk.&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-2069416705337892587?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/2069416705337892587/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=2069416705337892587' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2069416705337892587'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2069416705337892587'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/04/windows-seven-and-sidebar-gadgets.html' title='Windows Seven and Sidebar Gadgets'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Z5KTIfnfuNs/SdiQ5WkuPQI/AAAAAAAAANk/VBJLsMEQDRM/s72-c/UAC_thumb%5B3%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-7476878467697993939</id><published>2009-03-16T16:19:00.001+03:30</published><updated>2009-03-16T16:19:14.366+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='FarsiLibrary'/><title type='text'>FarsiLibrary 2.1 Released</title><content type='html'>&lt;p&gt;Finally the 2.1 version of FarsiLibrary is released. Here are the changes made into this version:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;- &lt;font color="#008000"&gt;&lt;strong&gt;Fixed&lt;/strong&gt;&lt;/font&gt; : Rendering of controls in non-official themes or when a 3rd party windows skinning (e.g. WindowsBlinds) is installed.    &lt;br /&gt;- &lt;font color="#008000"&gt;&lt;strong&gt;Fixed&lt;/strong&gt;&lt;/font&gt; : Problem when setting SelectedDateTime property of FXDatePicker control to null value.    &lt;br /&gt;- &lt;font color="#008000"&gt;&lt;strong&gt;Fixed&lt;/strong&gt;&lt;/font&gt; : Painting of FADatePicker in readonly mode when Readonly property is set fixed.    &lt;br /&gt;- &lt;font color="#008000"&gt;&lt;strong&gt;Fixed&lt;/strong&gt;&lt;/font&gt; : Problem of setting ForeColor and BackColor in FADatePicker    &lt;br /&gt;- &lt;font color="#008000"&gt;&lt;strong&gt;Fixed&lt;/strong&gt;&lt;/font&gt; : Certain usage of WinForms control resulted wrong display of Week Of Day in header part of the FAMonthView.    &lt;br /&gt;- &lt;font color="#008000"&gt;&lt;strong&gt;Fixed&lt;/strong&gt;&lt;/font&gt; : Certain usage of FAMonthView resulted in wrong displaying of date when culture is invariant.    &lt;br /&gt;    &lt;br /&gt;- &lt;font color="#0080ff"&gt;&lt;strong&gt;Added&lt;/strong&gt;&lt;/font&gt; : Methods SetTodayDate and SetEmptyDate added to FXDatePicker and FXMonthView which lets you call the code that represents Today and Empty buttons clicks respectively.    &lt;br /&gt;- &lt;font color="#0080ff"&gt;&lt;strong&gt;Added&lt;/strong&gt;&lt;/font&gt; : PersianCultureInfo which is a FA-IR Culture with correct PersianCalendar and DateTimeFormatInfo.    &lt;br /&gt;- &lt;font color="#0080ff"&gt;&lt;strong&gt;Added&lt;/strong&gt;&lt;/font&gt; : PersianDateTimeFormatInfo that represents datetime formatting information for FA-IR culture.    &lt;br /&gt;- &lt;font color="#0080ff"&gt;&lt;strong&gt;Added&lt;/strong&gt;&lt;/font&gt; : DateTimeExtensions to help convert between PersianDate and DateTime instances via extension methods.    &lt;br /&gt;- &lt;font color="#0080ff"&gt;&lt;strong&gt;Added&lt;/strong&gt;&lt;/font&gt; : XmlnsDefinition is added to WPF namespaces. You can reference the controls assembly with &lt;a href="http://schemas.hightech.ir/wpf/2008/FarsiLibrary"&gt;http://schemas.hightech.ir/wpf/2008/FarsiLibrary&lt;/a&gt; namespace.    &lt;br /&gt;- &lt;font color="#0080ff"&gt;&lt;strong&gt;Added&lt;/strong&gt;&lt;/font&gt; : Methods to add / remove validation errors on BaseControl.    &lt;br /&gt;- &lt;font color="#0080ff"&gt;&lt;strong&gt;Added&lt;/strong&gt;&lt;/font&gt; : PersianDateValueConverter which can be used in WPF applications to convert strings representing DateTime to their PersianDate equivalant.    &lt;br /&gt;- &lt;font color="#0080ff"&gt;&lt;strong&gt;Added&lt;/strong&gt;&lt;/font&gt; : WPF Demo to show usage of custom date converters.    &lt;br /&gt;- &lt;font color="#0080ff"&gt;&lt;strong&gt;Added&lt;/strong&gt;&lt;/font&gt; : Ability to show and hide Today and Empty buttons on FAMonthView.     &lt;br /&gt;- &lt;font color="#0080ff"&gt;&lt;strong&gt;Added&lt;/strong&gt;&lt;/font&gt; : VisualStudio designer for WPF and actions for WinForm controls is added.    &lt;br /&gt;    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : Localization of WPF controls are using mechanism like WinForm controls (using StringIDs). Redundant .resx files are removed.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : Merged WinForm and WPF controls into one solution.     &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : ValueValidatingEventArgs now passes HasErrors property of the control when raising event.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : PersianWeekDayNames and PersianMonthNames have became internal. Use PersianDateTimeFormatInfo class instead.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : When parsing a string representation of PersianDate e.g. 1382/08/23 time part was initialized from system time, but now initialized to 00:00 to be consistent with DateTime behavior.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : PersianDateConverter is changed access modifier to Internal. You should not use this class, and instead either cast instances of DateTime / PersianDate or use newly provided extension methods.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : Strings representing DateTime is now parsed using InvariantCulture when parsed to DateTime instance.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : Static Parse and TryParse method accepting DateTime instance is removed. Use constructor overload instead.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : &amp;quot;Readonly&amp;quot; property in FAContainerComboBox is made obsolete. Use IsReadonly property.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : Border color of control in Office2000 and WindowsXP was near white color. Now uses SystemColors.ControlDarkDark value.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : Created a new base class for FAMonthView, which will be base of other upcoming controls.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : CurrentMonthName property from FAMonthView is made obsolete. You can use the GetMonthName method on BaseCulturedControl class instead.    &lt;br /&gt;- &lt;font color="#ff8000"&gt;&lt;strong&gt;Modified&lt;/strong&gt;&lt;/font&gt; : Arrows of the FAMonthView will gray-out if the control is in disabled state. Change of selected date is not available if the control is disabled.&lt;/p&gt;  &lt;p&gt;Notice that “Modified” entries might be breaking your existing code base, but you are encouraged to use this latest version as there were some rather important bugs fixed and some useful features is added.&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;Like always, get the files from my web sky-drive &lt;a href="http://cid-4962b6ceabc2cbd7.skydrive.live.com/browse.aspx/BlogFiles/Farsi%20Library"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-7476878467697993939?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/7476878467697993939/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=7476878467697993939' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7476878467697993939'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7476878467697993939'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/03/farsilibrary-21-released.html' title='FarsiLibrary 2.1 Released'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-6837572658101227458</id><published>2009-03-09T10:55:00.002+03:30</published><updated>2009-03-09T15:45:21.483+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Testing'/><category scheme='http://www.blogger.com/atom/ns#' term='General'/><title type='text'>Unit Testing : Fact or Fantasy?</title><content type='html'>&lt;p&gt;I proposed a &lt;a href="http://www.codeproject.com/script/Surveys/Results.aspx?srvid=666"&gt;poll&lt;/a&gt; to &lt;a href="http://www.codeproject.com/"&gt;CodeProject&lt;/a&gt; web site a while ago to see how community is embracing the TDD and agile. There are a lot of talks in .NET community regarding writing tests (Unit Tests, Acceptance Tests, etc.) and even Microsoft is taking this seriously by providing new frameworks and tools to support the idea. I could argue that ASP.NET MVC, supporting TDD in VisualStudio 2010 and lots of other things coming from the “&lt;a href="http://blogs.msdn.com/somasegar/"&gt;Bosses&lt;/a&gt;” is a sign of this, don’t you agree? But how community in general and specifically developers are embracing this? That’s what I intended to find out.&lt;/p&gt;&lt;p&gt;Here’s the result:&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Z5KTIfnfuNs/SbTEd__JbjI/AAAAAAAAANI/DNIbmblTQKo/s1600-h/Chart%5B11%5D.png"&gt;&lt;img style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: block; FLOAT: none; MARGIN-LEFT: auto; BORDER-TOP: 0px; MARGIN-RIGHT: auto; BORDER-RIGHT: 0px" title="Chart" border="0" alt="Chart" src="http://lh6.ggpht.com/_Z5KTIfnfuNs/SbTEgbJQ9CI/AAAAAAAAANM/ITsmdl8YLb0/Chart_thumb%5B7%5D.png?imgmax=800" width="736" height="260" /&gt;&lt;/a&gt; &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;Interesting, right? You can see that 18% not know what unit testing is! and this makes almost half of 1000 voted people (exactly 42%) not testing their code! What do you make of it? I’d say it is a disaster! How could the application developers have a night’s sleep if they have absolutely ZERO testing code? I’m sure they say unit testing is not for us, we just write business applications. Well, writing a business application with 50,000 – 70,000 LoC should have SOME bugs in it, right? How do you make sure there’s no bug at least in your core functionality!?&lt;/p&gt;&lt;p&gt;Thanks Chris for shedding some lights on this. What about you, dear reader? Do you find unit testing important? Do you test the code you’ve written?&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-6837572658101227458?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/6837572658101227458/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=6837572658101227458' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6837572658101227458'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6837572658101227458'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/03/unit-testing-fact-or-fantasy.html' title='Unit Testing : Fact or Fantasy?'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_Z5KTIfnfuNs/SbTEgbJQ9CI/AAAAAAAAANM/ITsmdl8YLb0/s72-c/Chart_thumb%5B7%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-8947709544653402346</id><published>2009-03-05T10:35:00.000+03:30</published><updated>2009-03-05T10:35:00.811+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='WinForms'/><title type='text'>Design-Time Properties</title><content type='html'>&lt;p&gt;Ever wanted to have a Designer-Only properties, whose only purpose is to do something in Visual Studio? I guess you already almost know how to do this. Simply create a property with Getters / Setters and assign an Editor to it, but there are a few things you can do to enhance it.&lt;/p&gt;&lt;p&gt;Let’s say we want to create a property that will be shown on PropertyEditor and assign it an editor so that when the editor is activated we get an About dialog about our control / component. Fancy enough?&lt;/p&gt;&lt;p&gt;so let’s create our basic property. After thinking about it, we don’t need a setter at all and since this is a design time property the return value and return type are also unimportant. So we’re down to this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public object &lt;/span&gt;&lt;span style="color:maroon;"&gt;About&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;    &lt;span style="color:navy;"&gt;get &lt;/span&gt;{ &lt;span style="color:navy;"&gt;return null&lt;/span&gt;; }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Assigning an property Editor is relatively easy. Create a new class and inherit from UITypeEditor override a couple of methods and you’re all set:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color:#a65300;"&gt;EditorBrowsable&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;EditorBrowsableState&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Never&lt;/span&gt;)]&lt;br /&gt;&lt;span style="color:navy;"&gt;internal sealed class &lt;/span&gt;&lt;span style="color:#a65300;"&gt;AboutDialogEditor &lt;/span&gt;: &lt;span style="color:#a65300;"&gt;UITypeEditor&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;    &lt;span style="color:navy;"&gt;public override &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;UITypeEditorEditStyle &lt;/span&gt;&lt;span style="color:maroon;"&gt;GetEditStyle&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ITypeDescriptorContext &lt;/span&gt;&lt;span style="color:maroon;"&gt;context&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;UITypeEditorEditStyle&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Modal&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:navy;"&gt;public override object &lt;/span&gt;&lt;span style="color:maroon;"&gt;EditValue&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ITypeDescriptorContext &lt;/span&gt;&lt;span style="color:maroon;"&gt;context&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;IServiceProvider &lt;/span&gt;&lt;span style="color:maroon;"&gt;provider&lt;/span&gt;, &lt;span style="color:navy;"&gt;object &lt;/span&gt;&lt;span style="color:maroon;"&gt;value&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#a65300;"&gt;About &lt;/span&gt;&lt;span style="color:maroon;"&gt;about &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color:#a65300;"&gt;About&lt;/span&gt;();&lt;br /&gt;        &lt;span style="color:maroon;"&gt;about&lt;/span&gt;.&lt;span style="color:maroon;"&gt;ShowDialog&lt;/span&gt;();&lt;br /&gt;        &lt;span style="color:maroon;"&gt;about&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Dispose&lt;/span&gt;();&lt;br /&gt;&lt;br /&gt;        &lt;span style="color:navy;"&gt;return null&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;What we’re doing here is first to specify our editor should be opened in Modal mode, and second when the value is requested for that property (Editor is invoked), we just display our about dialog. So, by assigning this editor to our property, it should work:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color:#a65300;"&gt;Editor&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#a65300;"&gt;AboutDialogEditor&lt;/span&gt;), &lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#a65300;"&gt;UITypeEditor&lt;/span&gt;))]&lt;br /&gt;&lt;span style="color:navy;"&gt;public object &lt;/span&gt;&lt;span style="color:maroon;"&gt;About&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;    &lt;span style="color:navy;"&gt;get &lt;/span&gt;{ &lt;span style="color:navy;"&gt;return null&lt;/span&gt;; }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Z5KTIfnfuNs/Sa92bYDIlkI/AAAAAAAAAM4/3WuW3LCCbmk/s1600-h/AboutDialog-FirstTake%5B4%5D.png"&gt;&lt;img style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title="AboutDialog-FirstTake" border="0" alt="AboutDialog-FirstTake" src="http://lh5.ggpht.com/_Z5KTIfnfuNs/Sa92e0E2VlI/AAAAAAAAAM8/B2jIV6vv2kw/AboutDialog-FirstTake_thumb%5B2%5D.png?imgmax=800" width="640" height="251" /&gt;&lt;/a&gt; &lt;/p&gt;&lt;p&gt;There are some other things to do. First, our property is displayed “somewhere” in the PropertyEditor. That’s not what we want for an About dialog, is it? We need the property to appear always first and we need to somehow distinct it from other normal properties. What we can do about it is to use ParenthesizePropertyNameAttribute which simply signifies what our property should be treated specially, just like the standard Name property which is displayed above the others and is in parentheses. Also, to explicitly declare that our property is only a design-time property we’ll add a DesignOnlyAttribute:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color:#a65300;"&gt;DesignOnly&lt;/span&gt;(&lt;span style="color:navy;"&gt;true&lt;/span&gt;)]&lt;br /&gt;[&lt;span style="color:#a65300;"&gt;ParenthesizePropertyName&lt;/span&gt;(&lt;span style="color:navy;"&gt;true&lt;/span&gt;)]&lt;br /&gt;[&lt;span style="color:#a65300;"&gt;Editor&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#a65300;"&gt;AboutDialogEditor&lt;/span&gt;), &lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#a65300;"&gt;UITypeEditor&lt;/span&gt;))]&lt;br /&gt;&lt;span style="color:navy;"&gt;public object &lt;/span&gt;&lt;span style="color:maroon;"&gt;About&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;    &lt;span style="color:navy;"&gt;get &lt;/span&gt;{ &lt;span style="color:navy;"&gt;return null&lt;/span&gt;; }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The only thing left here is that we don’t want developers working with our controls see this property, because of the fact that it is a design-time property. To do this we add a EditorBrowsableAttribute and make our property Hidden in the texteditor of Visual Studio. So here’s how our final property looks like:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color:#a65300;"&gt;DesignOnly&lt;/span&gt;(&lt;span style="color:navy;"&gt;true&lt;/span&gt;)]&lt;br /&gt;[&lt;span style="color:#a65300;"&gt;EditorBrowsable&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;EditorBrowsableState&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Never&lt;/span&gt;)]&lt;br /&gt;[&lt;span style="color:#a65300;"&gt;DesignerSerializationVisibility&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;DesignerSerializationVisibility&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Hidden&lt;/span&gt;)]&lt;br /&gt;[&lt;span style="color:#a65300;"&gt;ParenthesizePropertyName&lt;/span&gt;(&lt;span style="color:navy;"&gt;true&lt;/span&gt;)]&lt;br /&gt;[&lt;span style="color:#a65300;"&gt;Editor&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#a65300;"&gt;AboutDialogEditor&lt;/span&gt;), &lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#a65300;"&gt;UITypeEditor&lt;/span&gt;))]&lt;br /&gt;&lt;span style="color:navy;"&gt;public object &lt;/span&gt;&lt;span style="color:maroon;"&gt;About&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;    &lt;span style="color:navy;"&gt;get &lt;/span&gt;{ &lt;span style="color:navy;"&gt;return null&lt;/span&gt;; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Z5KTIfnfuNs/Sa92gGqF5SI/AAAAAAAAANA/2Qok8Bu9EnM/s1600-h/AboutDialog-SecondTake%5B4%5D.png"&gt;&lt;img style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title="AboutDialog-SecondTake" border="0" alt="AboutDialog-SecondTake" src="http://lh5.ggpht.com/_Z5KTIfnfuNs/Sa92hlLjDEI/AAAAAAAAANE/afda9oGcCGs/AboutDialog-SecondTake_thumb%5B2%5D.png?imgmax=800" width="375" height="191" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-8947709544653402346?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/8947709544653402346/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=8947709544653402346' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/8947709544653402346'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/8947709544653402346'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/03/design-time-properties.html' title='Design-Time Properties'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_Z5KTIfnfuNs/Sa92e0E2VlI/AAAAAAAAAM8/B2jIV6vv2kw/s72-c/AboutDialog-FirstTake_thumb%5B2%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-1771244682538079907</id><published>2009-02-15T14:25:00.001+03:30</published><updated>2009-02-15T14:25:52.732+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='General'/><title type='text'>Gmail on IE8</title><content type='html'>&lt;p&gt;Looks like Gmail guys don’t like my new browser. Sometimes (notice, just sometimes) things get broken in IE8 beta when using Gmail resulting strange layout problems. Even in turning compatibility mode to on has no effect on this. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_Z5KTIfnfuNs/SZf0mq5jvpI/AAAAAAAAAMY/avec-d-u6tE/s1600-h/Gmail-Compose%5B8%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Gmail-Compose" border="0" alt="Gmail-Compose" src="http://lh5.ggpht.com/_Z5KTIfnfuNs/SZf0pFyR-XI/AAAAAAAAAMc/5EFQqcK36WI/Gmail-Compose_thumb%5B6%5D.png?imgmax=800" width="412" height="167" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_Z5KTIfnfuNs/SZf0rXNTdQI/AAAAAAAAAMg/HRvhT_Dedwo/s1600-h/Gmail-Inbox%5B5%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Gmail-Inbox" border="0" alt="Gmail-Inbox" src="http://lh3.ggpht.com/_Z5KTIfnfuNs/SZf0tmM1wMI/AAAAAAAAAMk/tPI1xVMbagM/Gmail-Inbox_thumb%5B3%5D.png?imgmax=800" width="410" height="166" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-1771244682538079907?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/1771244682538079907/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=1771244682538079907' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1771244682538079907'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1771244682538079907'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/02/gmail-on-ie8.html' title='Gmail on IE8'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_Z5KTIfnfuNs/SZf0pFyR-XI/AAAAAAAAAMc/5EFQqcK36WI/s72-c/Gmail-Compose_thumb%5B6%5D.png?imgmax=800' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-5572244319865496160</id><published>2009-02-14T15:01:00.002+03:30</published><updated>2009-02-14T15:48:36.028+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='VS.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='WinForms'/><title type='text'>VS.NET Designer Errors</title><content type='html'>&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_Z5KTIfnfuNs/SZariGi7oII/AAAAAAAAAMI/yovDr_7SnQA/s1600-h/Unknown-Designer-Exceptions%5B6%5D.png"&gt;&lt;img style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; MARGIN: 5px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title="Unknown-Designer-Exceptions" border="0" alt="Unknown-Designer-Exceptions" align="right" src="http://lh4.ggpht.com/_Z5KTIfnfuNs/SZarjgq75cI/AAAAAAAAAMM/iQbV6HMsOhE/Unknown-Designer-Exceptions_thumb%5B4%5D.png?imgmax=800" width="488" height="181" /&gt;&lt;/a&gt; Maintaining old applications has always been something painful for me. Old application, for me, means Winform applications using DataSets for binding operations. When converting and opening an old application, the least thing you want on your hands is to open a form and see Designer screen of death. This is due to the fact that in the WinForm world when you open a form, all the extra codes in the constructor and Load events will get executed, and this where things can go hairdo. Think about a pieced of code in constructor of a form doing something innocent in nature like saving / loading data from registry, connecting to a service. Almost always VS.NET shows a cryptic message. How would you know which code / form / usercontrol is causing this?&lt;br /&gt;&lt;/p&gt;&lt;h4&gt;&lt;span style="color:#0080ff;"&gt;&lt;strong&gt;Debugging the Designer&lt;/strong&gt;&lt;/span&gt;&lt;/h4&gt;&lt;p&gt;There are just a few steps to catch the buggy piece of code ruining your day. Open up your visual studio solution containing your application’s code and compile. Locate the form that is breaking the designer. Open another instance of VS.NET and Goto “Tools” –&amp;gt; "Attach To Process” and locate the process named “Devend.exe” which is Visual Studio’s process name. There are two processes named “Devenv”. Make sure you select the one with loaded solution. To catch all the thrown exceptions go to “Debug” –&amp;gt; “Exceptions” and check the column “Thrown” in front of “Common Language Runtime Exceptions”. This will tell the VS.NET to catch all the exception thrown by CLR. At this point you’re good to go. Switch to your VS.NET containing the solution and open the Form / Usercontrol. The exception will be caught so you can have a very good idea what the problem is.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh3.ggpht.com/_Z5KTIfnfuNs/SZarnCiMxrI/AAAAAAAAAMQ/tPAp_WKnw58/s1600-h/VSDebugger%5B7%5D.png"&gt;&lt;img style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: block; FLOAT: none; MARGIN-LEFT: auto; BORDER-TOP: 0px; MARGIN-RIGHT: auto; BORDER-RIGHT: 0px" title="VSDebugger" border="0" alt="VSDebugger" src="http://lh4.ggpht.com/_Z5KTIfnfuNs/SZarruWDzSI/AAAAAAAAAMU/3jV-YgDh2Zc/VSDebugger_thumb%5B3%5D.png?imgmax=800" width="644" height="430" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;It is very easy to avoid this. &lt;/p&gt;&lt;ul&gt;&lt;li&gt;Make sure the codes written in Form and Usercontrol constructors / OnLoaded / Loaded event (except for InitializeComponents in constructors) will not get executed.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Use the DesignMode property&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;If you’re extending functionalities of a custom control you need to be aware of this problem. The constrcutors of custom controls will be called in InitializeComponent anyway, so if there’s something wrong with the code in there, you’ll end up with this problem.&lt;br /&gt;&lt;br /&gt;There's also an issue with the DesignMode property in UserControls. The design mode only works when the user control is Sited so you may be in design mode and get a False value when checking DesignMode property. The trick is to use a property on System.ComponentModel.LicenseManager which is more reliable.&lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-5572244319865496160?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/5572244319865496160/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=5572244319865496160' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/5572244319865496160'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/5572244319865496160'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/02/vs-designer-errors-rsod-again.html' title='VS.NET Designer Errors'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_Z5KTIfnfuNs/SZarjgq75cI/AAAAAAAAAMM/iQbV6HMsOhE/s72-c/Unknown-Designer-Exceptions_thumb%5B4%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-8183648265405487841</id><published>2009-02-14T13:06:00.000+03:30</published><updated>2009-02-14T15:20:33.788+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='General'/><title type='text'>MIX 10K</title><content type='html'>&lt;p&gt;There’s been a new competition around for WPFers. You need to submit an application created with WPF technology (being a WPF app or Silverlight) which should not exceed 10 KB in size. Tricky, eh? What kind of an app can be 10 K? Check out the &lt;a href="http://2009.visitmix.com/MIXtify/TenKGallery.aspx"&gt;gallery&lt;/a&gt; to see for yourself. You can vote on your favorite app until 16th of February.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-8183648265405487841?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/8183648265405487841/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=8183648265405487841' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/8183648265405487841'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/8183648265405487841'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/02/mix-10k.html' title='MIX 10K'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-4588782118621570582</id><published>2009-02-06T20:19:00.002+03:30</published><updated>2009-02-06T20:22:39.937+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Windsor'/><title type='text'>Windsor and Automatic Properties</title><content type='html'>&lt;p&gt;When maintaining an old application that uses Windsor as DI framework, a strange NullReferenceException throwed when trying to resolve an instance of a specific class. Our project has been recently upgraded to VS 2008 so it was natural to use C# 3.0 specific features. One thing I used in the failing class was Automated Properties. The implementation looked like this :&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;public class &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;GridRepositoryFactory &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;: &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(43, 145, 175);"&gt;IGridRepositoryFactory&lt;br /&gt;&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;{&lt;br /&gt;   &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;public &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;GridRepositoryFactory&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(43, 145, 175);"&gt;IDynamicFormDataService &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;dataService&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;this&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;.&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;Service &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;= &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;dataService&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;public &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(43, 145, 175);"&gt;IDynamicFormDataService &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;Service&lt;br /&gt;   &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;{&lt;br /&gt;       &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;get&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;;&lt;br /&gt;       &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;private set&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;as the stacktrace showed, Castle Windsor version 1.0.3.4333 does not work correctly with inject automated properties. I had to use properties with backing field to solve this, but updating to a more recent version may also get it fixed.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-4588782118621570582?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/4588782118621570582/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=4588782118621570582' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/4588782118621570582'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/4588782118621570582'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/02/windsor-and-automatic-properties.html' title='Windsor and Automatic Properties'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-3158068460022587886</id><published>2009-02-03T15:14:00.002+03:30</published><updated>2009-02-03T15:16:24.907+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><title type='text'>Error Messages in Silverlight</title><content type='html'>&lt;p&gt;Can you guess what’s wrong with this Xaml code, on the first look?&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Application &lt;/span&gt;&lt;span style="color:red;"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;br /&gt;             &lt;/span&gt;&lt;span style="color:red;"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;br /&gt;             &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Class&lt;/span&gt;&lt;span style="color:blue;"&gt;="Test.UI.App"&lt;br /&gt;             &amp;gt;&lt;br /&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Application.Resources&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Color &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Key&lt;/span&gt;&lt;span style="color:blue;"&gt;="HeaderRectangleStrokeColor"&amp;gt;&lt;/span&gt;#FF777676&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Color&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Color &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Key&lt;/span&gt;&lt;span style="color:blue;"&gt;="AppInvertTitleForegroundColor"&amp;gt;&lt;/span&gt;#16FFFFFF&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Color&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Color &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Key&lt;/span&gt;&lt;span style="color:blue;"&gt;="AppTitleForegroundColor"&amp;gt;&lt;/span&gt;FFFFF8F8&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Color&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Color &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Key&lt;/span&gt;&lt;span style="color:blue;"&gt;="PageBackground"&amp;gt;&lt;/span&gt;#FF000000&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Color&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;SolidColorBrush &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Key&lt;/span&gt;&lt;span style="color:blue;"&gt;="HeaderRectangleStrokeBrush" &lt;/span&gt;&lt;span style="color:red;"&gt;Color&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color:#a31515;"&gt;StaticResource &lt;/span&gt;&lt;span style="color:red;"&gt;HeaderRectangleStrokeColor&lt;/span&gt;&lt;span style="color:blue;"&gt;}" /&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;SolidColorBrush &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Key&lt;/span&gt;&lt;span style="color:blue;"&gt;="AppInvertTitleForegroundBrush" &lt;/span&gt;&lt;span style="color:red;"&gt;Color&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color:#a31515;"&gt;StaticResource &lt;/span&gt;&lt;span style="color:red;"&gt;AppInvertTitleForegroundColor&lt;/span&gt;&lt;span style="color:blue;"&gt;}" /&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;SolidColorBrush &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Key&lt;/span&gt;&lt;span style="color:blue;"&gt;="AppTitleForegroundBrush" &lt;/span&gt;&lt;span style="color:red;"&gt;Color&lt;/span&gt;&lt;span style="color:blue;"&gt;="{&lt;/span&gt;&lt;span style="color:#a31515;"&gt;StaticResource &lt;/span&gt;&lt;span style="color:red;"&gt;AppTitleForegroundColor&lt;/span&gt;&lt;span style="color:blue;"&gt;}" /&amp;gt;&lt;br /&gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;LinearGradientBrush &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Key&lt;/span&gt;&lt;span style="color:blue;"&gt;="HeaderRectangleBrush" &lt;/span&gt;&lt;span style="color:red;"&gt;EndPoint&lt;/span&gt;&lt;span style="color:blue;"&gt;="0.5,1.011" &lt;/span&gt;&lt;span style="color:red;"&gt;StartPoint&lt;/span&gt;&lt;span style="color:blue;"&gt;="0.5,-0.011"&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;GradientStop &lt;/span&gt;&lt;span style="color:red;"&gt;Color&lt;/span&gt;&lt;span style="color:blue;"&gt;="#FF343333" &lt;/span&gt;&lt;span style="color:red;"&gt;Offset&lt;/span&gt;&lt;span style="color:blue;"&gt;="0"/&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;GradientStop &lt;/span&gt;&lt;span style="color:red;"&gt;Color&lt;/span&gt;&lt;span style="color:blue;"&gt;="#FF3E3E3E" &lt;/span&gt;&lt;span style="color:red;"&gt;Offset&lt;/span&gt;&lt;span style="color:blue;"&gt;="1"/&amp;gt;&lt;br /&gt;        &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;LinearGradientBrush&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;         &lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Application.Resources&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Application&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Application compiles just fine. Even Resharper can not detect a problem in it. But when you running it a hard error happens that fails the Silverlight plug-in! What’s worse, is that the error message is cryptic as can be :&lt;br /&gt;&lt;a href="http://lh5.ggpht.com/_Z5KTIfnfuNs/SYgt_OTEdVI/AAAAAAAAALo/kKBShsXmDiM/s1600-h/Silverlight-TestPage-Error%5B5%5D.png"&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Silverlight-TestPage-Error" border="0" alt="Silverlight-TestPage-Error" src="http://lh6.ggpht.com/_Z5KTIfnfuNs/SYguCRN4tfI/AAAAAAAAALs/8t9wbwYgKMw/Silverlight-TestPage-Error_thumb%5B3%5D.png?imgmax=800" width="831" height="480" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;My Application resource had more Colors and Brushes than the example here, so it was much harder to find the needle in haystack. Luckily I found out what the problem was. The Color value for one of the resource has missing # mark. Shouldn’t there be a more explicit error message? &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-3158068460022587886?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/3158068460022587886/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=3158068460022587886' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/3158068460022587886'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/3158068460022587886'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/02/silverlight.html' title='Error Messages in Silverlight'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_Z5KTIfnfuNs/SYguCRN4tfI/AAAAAAAAALs/8t9wbwYgKMw/s72-c/Silverlight-TestPage-Error_thumb%5B3%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-8438538781679899009</id><published>2009-01-30T12:22:00.003+03:30</published><updated>2009-01-30T12:45:54.946+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Code Analysis'/><title type='text'>Efficient Code</title><content type='html'>&lt;p&gt;Writing code may look easy to some and hard to others, but some programmers always tend to write messy, smelling, ugly-looking, inefficient code. Here’s one :&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;protected override &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;AppointmentForm &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;CreateAppointmentForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;SchedulerControl &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;control&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;Appointment &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;apt&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;bool &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;openRecurrenceForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;)&lt;br /&gt;{&lt;br /&gt;  &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:green;" &gt;//return new CustomAppointmentForm(control, apt, openRecurrenceForm);&lt;br /&gt;  &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;if &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;Thread&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;.&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;CurrentThread&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;.&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;CurrentUICulture&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;.&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;Name &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;== &lt;/span&gt;&lt;span style="background: rgb(255, 255, 230) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;"fa-IR"&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;if &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;openRecurrenceForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;)&lt;br /&gt;          &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;return new &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;CustomAppointmentForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;control&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;apt&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;,&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;openRecurrenceForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;);&lt;br /&gt;      &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;else&lt;br /&gt;      return new &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;CustomAppointmentForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;control&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;apt&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;if &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;openRecurrenceForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;)&lt;br /&gt;      &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;return new &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;AppointmentForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;control&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;apt&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;,&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;openRecurrenceForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;);&lt;br /&gt;  &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;else&lt;br /&gt;      return new &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;AppointmentForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;control&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;apt&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;What’s wrong with it you might say? Other than the logic implemented wrong (for specific culture insteam of all RTL cultures), “A lot”, I’d answer.&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Commenting out code instead of deleting it will leave a lot of ‘Ghost’ codes in your source. If you have a source-control in place why do you need to keep old code as commented out? it is always available in your source control, isn’t it??&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Embedding culture-specific code in not a good idea. What we want to achieve here can be easily achieved using factory patterns.&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Control flow is very important for other programmers to understand what you wanted to achieve. Writing nested if / else and multiple returns in a method tend to make this harder.&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Having braces in C style programmings is more a matter of style, but always be consistent.&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Efficiently use constructor / method overloads. Know what are the default values for overloaded parameters. &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The above code could be refactored into this :&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;protected override &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;AppointmentForm &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;CreateAppointmentForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;SchedulerControl &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;control&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;Appointment &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;apt&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;bool &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;openRecurrenceForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;)&lt;br /&gt;{&lt;br /&gt;  &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;AppointmentForm &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;form &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;= &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;null&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;if &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;CultureHelper&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;.&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;IsCultureRightToLeft&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;form &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;= &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;new &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;CustomAppointmentForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;control&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;apt&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;openRecurrenceForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;);&lt;br /&gt;  }&lt;br /&gt;  &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;else&lt;br /&gt;  &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;{&lt;br /&gt;      &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;form &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;= &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;new &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;AppointmentForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;control&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;apt&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;openRecurrenceForm&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;return &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;form&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-8438538781679899009?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/8438538781679899009/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=8438538781679899009' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/8438538781679899009'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/8438538781679899009'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/01/efficient-code.html' title='Efficient Code'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-1311417813552171869</id><published>2009-01-28T15:45:00.005+03:30</published><updated>2009-01-28T16:10:45.031+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='REST'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>Silverlight + RESTful POX</title><content type='html'>I’m working on a small project for a Swedish company. They need their application be available via web, so it’s time to put that Silverlight knowledge at work, so I decided to use Silverlight for UI technology and have a REST service layer to provide the required data. After all Silverlight and REST services should have no problem. Silverlight experience was great. You have LINQ at your disposal to process the REST results and although the set of available commands in Silverlight is less than WPF, but this makes it is easier at the same time. There was a couple of gatchas along the way, so I’m writing this as a note-to-self.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#0080ff;"&gt;Service Contract vs. Object Model&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;The domain model is very simple and consist of a couple of entities, so what REST Services expose as data contracts are more or less the same as what I use in application layer. Silverlight’s class library is not the same as a normal class library so I can not reuse the domain model available in my Silverlight library. This will lead to two set of object models. One for the domain model and the other for the service layer as data contracts. This separation or concern is a good idea, but for this small application it is overkill, but there’s nothing I can do with it. With the help of LINQ2XML the best choice for exposing the contract model is POX (Plain old XML) so result of our services would be in XML parse by LINQ and converted to our domain model.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#0080ff;"&gt;Unit Testing&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;Also because of different class library outputs, you can not use unit testing frameworks the way you usually do. This means you can not use xUnit, nUnit, etc. Hopefully there’s a test harness to allow both UI and API level testing when developing Silverlight and it works just like MSTest does. Download the binary files &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=EA93DD89-3AF2-4ACB-9CF4-BFE01B3F02D4&amp;amp;displaylang=en"&gt;here&lt;/a&gt; and read &lt;a href="http://www.jeff.wilcox.name/2008/03/silverlight2-unit-testing/"&gt;this&lt;/a&gt; to know how to add templates to your VS.NET to easily create a test application.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#0080ff;"&gt;RESTful POX&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;To achieve this the service contract is decorated with REST attributes, That is a WebGetAttribute if you’re exposing the operation via GET verb or a WebInvokeAttribute if you’re exposing it via POST.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Note : The thing was that exposing &lt;strong&gt;IList&amp;lt;T&amp;gt;&lt;/strong&gt; as a returned result of a service resulted a non functioning service with the following message : “Request Error : The server encountered an error processing the request. See server logs for more details.”. It turned out exposing the &lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt; worked. The reason behind this is probably the same thing as exposing an object’s interface via normal (Soap) WCF service which also doesn’t work due to interfaces not being serializable.&lt;br /&gt;&lt;br /&gt;&lt;/em&gt;So here’s the service contract :&lt;br /&gt;&lt;pre class="code"&gt;[&lt;span style="color:#a65300;"&gt;ServiceContract&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:navy;"&gt;public interface &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IDataServices&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;    [&lt;span style="color:#a65300;"&gt;OperationContract&lt;/span&gt;]&lt;br /&gt;    [&lt;span style="color:#a65300;"&gt;WebGet&lt;/span&gt;(&lt;span style="color:maroon;"&gt;ResponseFormat &lt;/span&gt;= &lt;span style="color:#2b91af;"&gt;WebMessageFormat&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Xml&lt;/span&gt;, &lt;span style="color:maroon;"&gt;BodyStyle &lt;/span&gt;= &lt;span style="color:#2b91af;"&gt;WebMessageBodyStyle&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Bare&lt;/span&gt;, &lt;span style="color:maroon;"&gt;UriTemplate &lt;/span&gt;= "/Advertisers")]&lt;br /&gt;    &lt;span style="color:#a65300;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#a65300;"&gt;Advertiser&lt;/span&gt;&amp;gt; &lt;span style="color:maroon;"&gt;GetAdvertisers&lt;/span&gt;();&lt;br /&gt;}&lt;/pre&gt;and the implementation is self explanatory. What you get is a service exposed at a URL like &lt;a title="http://localhost:9999/Services/DataServices.svc/Advertisers" href="http://localhost/Services/DataServices.svc/Advertisers"&gt;http://localhost/Services/DataServices.svc/Advertisers&lt;/a&gt;. In order to get rid of the nasty .svc extension you either need to use IIS URL Rewriting Module which is only available on IIS 7 or use IIS Wildcard Mapping plus a HttpModule to make this happen. Read &lt;a href="http://www.west-wind.com/weblog/posts/570695.aspx"&gt;here&lt;/a&gt; for complete information.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Note : The .svc extension does not matter from operational perspective. It is computer applications that connect to these URLs after all, but from purists point of view it does matter.&lt;br /&gt;&lt;br /&gt;&lt;/em&gt;The final part is the configuration which is standard WCF configuration like identifying service and contract, exposing EndPoints, etc. :&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;serviceHostingEnvironment &lt;/span&gt;&lt;span style="color:red;"&gt;aspNetCompatibilityEnabled&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;true&lt;/span&gt;" &lt;span style="color:blue;"&gt;/&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;behaviors&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;     &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;endpointBehaviors&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;       &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;behavior &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;WebBehavior&lt;/span&gt;"&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;         &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;webHttp&lt;/span&gt;&lt;span style="color:blue;"&gt;/&amp;gt;&lt;br /&gt;       &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;behavior&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;     &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;endpointBehaviors&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;behaviors&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;services&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;     &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;service &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Reklam.UI.Web.Services.DataServices&lt;/span&gt;"&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;       &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;endpoint &lt;/span&gt;&lt;span style="color:red;"&gt;behaviorConfiguration&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;WebBehavior&lt;/span&gt;"&lt;br /&gt;                 &lt;span style="color:red;"&gt;binding&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;webHttpBinding&lt;/span&gt;"&lt;br /&gt;                 &lt;span style="color:red;"&gt;contract&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Reklam.UI.Web.Services.IDataServices&lt;/span&gt;" &lt;span style="color:blue;"&gt;/&amp;gt;&lt;br /&gt;     &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;service&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;services&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-1311417813552171869?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/1311417813552171869/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=1311417813552171869' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1311417813552171869'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1311417813552171869'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/01/silverlight-restful-pox.html' title='Silverlight + RESTful POX'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-935259870654605962</id><published>2009-01-28T10:47:00.001+03:30</published><updated>2009-02-14T13:00:16.239+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Windows 7'/><category scheme='http://www.blogger.com/atom/ns#' term='General'/><title type='text'>Using Windows Seven Beta</title><content type='html'>&lt;p&gt;It’s been a few weeks since I’ve installed Windows 7 on my work development box. During the installation everything went fine and all the applications I had on my Windows Vista installed and worked fine under new windows 7, except for Skype for which I had to download and install their version 4.0 Beta. As a Vista user, I found myself immediately familiar with the environment but there are a lot of goodies here and there which made me say “Nice” loud and clear. Performance gain is totally noticeable compared to Vista and memory usage is as low as it can be. Fresh installation of Windows 7 occupies just 700 MB of memory which is obviously way lower than Vista.&lt;/p&gt;  &lt;p&gt;I had very few problems using it. The disastrous one was two occasional system crashes when I was designing WPF apps in Visual Studio 2008. The design screen of my WPF UI was frozen and with just a mouse click system had a hard crash. The other issue is using Virtual PC. When windows overlap the VPC window, it goes black and I have to move the window a little bit to see the content (I’m using VPN in Windowed mode, not fullscreen). This too is very annoying because Windows are overlapping each other all the time and its giving me a hard time. &lt;/p&gt;  &lt;p&gt;I know this is beta stuff and things may be broken and not working, but I see that guys at Windows 7 team have done a great job.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Update : If you have a Nvidia graphic card, upgrading the driver via Windows Update will solve the VPC going black.&lt;/em&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-935259870654605962?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/935259870654605962/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=935259870654605962' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/935259870654605962'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/935259870654605962'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/01/using-windows-seven-beta.html' title='Using Windows Seven Beta'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-2806333132303858222</id><published>2009-01-08T19:09:00.002+03:30</published><updated>2009-01-08T19:42:57.533+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='WinForms'/><title type='text'>Paint API on Vista</title><content type='html'>&lt;p&gt;I was testing bunch of custom controls using Managed paint API on Windows Vista. Basically, managed and unmanaged paint API use native resources of the OS to paint controls, so if you run your controls on Windows XP you’ll get XP look and feel and if you run it on Vista, you get new vistaish look and feel, right? All control parts I’ve tested seems to work, but there is a catch! If you draw a disabled TextBox, it looks strange in Windows Vista, but looks right in XP :&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;VisualStyleRenderer &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;renderer &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;= &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:navy;" &gt;new &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;VisualStyleRenderer&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;VisualStyleElement&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;.&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;Button&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;.&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(166, 83, 0);"&gt;PushButton&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;.&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;Disabled&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;);&lt;br /&gt;&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;renderer&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;.&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;DrawBackground&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;(&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;e&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;.&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;Graphics&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;, &lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;color:maroon;" &gt;ClientRectangle&lt;/span&gt;&lt;span style="background: rgb(248, 248, 248) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;);&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;While this code looks fine in XP, it produces a flat blue textbox on Vista :&lt;br /&gt;&lt;p&gt;&lt;a href="http://www.hightech.ir/BlogPics/ManagedPaintAPIonVista_105B6/VistaPaintAPINormal.png"&gt;&lt;img title="VistaPaintAPI-Normal" style="border: 0px none ; display: inline;" alt="VistaPaintAPI-Normal" src="http://www.hightech.ir/BlogPics/ManagedPaintAPIonVista_105B6/VistaPaintAPINormal_thumb.png" width="256" border="0" height="87" /&gt;&lt;/a&gt; &lt;a href="http://www.hightech.ir/BlogPics/ManagedPaintAPIonVista_105B6/VistaPaintAPIReadonly.png"&gt;&lt;img title="VistaPaintAPI-Readonly" style="border: 0px none ; display: inline;" alt="VistaPaintAPI-Readonly" src="http://www.hightech.ir/BlogPics/ManagedPaintAPIonVista_105B6/VistaPaintAPIReadonly_thumb.png" width="255" border="0" height="80" /&gt;&lt;/a&gt; &lt;/p&gt;As far as I could tell, this is an issue with textbox only and other controls like buttons, etc render correctly in both OSes.&lt;br /&gt;&lt;p&gt;I wonder why?! From end-user’s perspective this doesnot look like disabled piece of UI and even worse it doesn’t look like a native disabled texbox on Vista. I still don’t know about any workarounds and I doubt it if native calls to UxTheme API would solve this.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-2806333132303858222?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/2806333132303858222/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=2806333132303858222' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2806333132303858222'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2806333132303858222'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/01/paint-api-on-vista.html' title='Paint API on Vista'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-2079288859251236901</id><published>2009-01-01T14:51:00.001+03:30</published><updated>2009-01-01T14:51:48.978+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='WinForms'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>OutOfIdeasException!</title><content type='html'>&lt;p&gt;Today I got a funny error message! Showing a simpel Form I got an OutOfMemoryException! Further investigation showed that an actual NullReferenceException is the cause of this. The question is, why a NullReferenceException would convert to an OutOfMemoryException!!!&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.hightech.ir/BlogPics/OutOfIdeasException_D04C/OutOfMemoryError.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="OutOfMemory-Error" border="0" alt="OutOfMemory-Error" src="http://www.hightech.ir/BlogPics/OutOfIdeasException_D04C/OutOfMemoryError_thumb.png" width="671" height="347" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.hightech.ir/BlogPics/OutOfIdeasException_D04C/NullReferenceError.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="NullReference-Error" border="0" alt="NullReference-Error" src="http://www.hightech.ir/BlogPics/OutOfIdeasException_D04C/NullReferenceError_thumb.png" width="666" height="314" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-2079288859251236901?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/2079288859251236901/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=2079288859251236901' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2079288859251236901'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2079288859251236901'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2009/01/outofideasexception.html' title='OutOfIdeasException!'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-7185771948185614940</id><published>2008-12-31T10:50:00.003+03:30</published><updated>2008-12-31T10:53:23.829+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Rhino Tools'/><category scheme='http://www.blogger.com/atom/ns#' term='NHibernate'/><title type='text'>ATM with NHibernate</title><content type='html'>&lt;p&gt;It is cool what you can achieve using anonymous methods and lambdas. Usually to persist an entity to datastore, you would do something like this with NHiberante: &lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;SaveOrder&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;using &lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;session &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;IoC&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Resolve&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ISessionFactory&lt;/span&gt;&amp;gt;().&lt;span style="color:maroon;"&gt;OpenSession&lt;/span&gt;())&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;transaction &lt;/span&gt;= &lt;span style="color:maroon;"&gt;session&lt;/span&gt;.&lt;span style="color:maroon;"&gt;BeginTransaction&lt;/span&gt;();&lt;br /&gt;&lt;br /&gt;      &lt;span style="color:navy;"&gt;try&lt;br /&gt;      &lt;/span&gt;{&lt;br /&gt;          &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;o &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Order&lt;br /&gt;          &lt;/span&gt;{&lt;br /&gt;              &lt;span style="color:maroon;"&gt;ProductName &lt;/span&gt;= &lt;span style="background: rgb(255, 255, 230) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;"Ice Creams"&lt;/span&gt;,&lt;br /&gt;              &lt;span style="color:maroon;"&gt;Description &lt;/span&gt;= &lt;span style="background: rgb(255, 255, 230) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;"Lots of strawberry ice creams"&lt;br /&gt;&lt;/span&gt;            };&lt;br /&gt;&lt;br /&gt;          &lt;span style="color:maroon;"&gt;session&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Save&lt;/span&gt;(&lt;span style="color:maroon;"&gt;o&lt;/span&gt;);&lt;br /&gt;          &lt;span style="color:maroon;"&gt;session&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Flush&lt;/span&gt;();&lt;br /&gt;          &lt;span style="color:maroon;"&gt;transaction&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Commit&lt;/span&gt;();&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:navy;"&gt;catch &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Exception&lt;/span&gt;)&lt;br /&gt;      {&lt;br /&gt;          &lt;span style="color:maroon;"&gt;transaction&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Rollback&lt;/span&gt;();&lt;br /&gt;          &lt;span style="color:navy;"&gt;throw&lt;/span&gt;;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;but now you can abstract away the whole transaction creation and commit / rollback thing. Transactions will be rolledback automagically in case an exception occurs :&lt;br /&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;SaveOrder&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color: rgb(166, 83, 0);"&gt;With&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Transaction&lt;/span&gt;(() =&amp;gt;&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;o &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Order&lt;br /&gt;      &lt;/span&gt;{&lt;br /&gt;          &lt;span style="color:maroon;"&gt;ProductName &lt;/span&gt;= &lt;span style="background: rgb(255, 255, 230) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;"Ice Creams"&lt;/span&gt;,&lt;br /&gt;          &lt;span style="color:maroon;"&gt;Description &lt;/span&gt;= &lt;span style="background: rgb(255, 255, 230) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;"Lots of ice strawberry creams"&lt;br /&gt;&lt;/span&gt;        };&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: rgb(166, 83, 0);"&gt;UnitOfWork&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CurrentSession&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Save&lt;/span&gt;(&lt;span style="color:maroon;"&gt;o&lt;/span&gt;);&lt;br /&gt;  });&lt;br /&gt;}&lt;/pre&gt;hmmmmm…how is that possible? all the magic is in the With class and UnitOfWork implementation which are available with lots of other stull in Rhino Tools as free as it can be. In brief here’s how it works :&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public static class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;Using&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;public static void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Transaction&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Action &lt;/span&gt;&lt;span style="color:maroon;"&gt;action&lt;/span&gt;)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:navy;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;UnitOfWork&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Start&lt;/span&gt;())&lt;br /&gt;&lt;span style="color:navy;"&gt;       using &lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;tx &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;UnitOfWork&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Current&lt;/span&gt;.&lt;span style="color:maroon;"&gt;BeginTransaction&lt;/span&gt;())&lt;br /&gt;      {&lt;br /&gt;          &lt;span style="color:navy;"&gt;try&lt;br /&gt;          &lt;/span&gt;{&lt;br /&gt;              &lt;span style="color:maroon;"&gt;action&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Invoke&lt;/span&gt;();&lt;br /&gt;              &lt;span style="color:maroon;"&gt;tx&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Commit&lt;/span&gt;();&lt;br /&gt;          }&lt;br /&gt;          &lt;span style="color:navy;"&gt;catch&lt;br /&gt;          &lt;/span&gt;{&lt;br /&gt;              &lt;span style="color:maroon;"&gt;tx&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Rollback&lt;/span&gt;();&lt;br /&gt;              &lt;span style="color:navy;"&gt;throw&lt;/span&gt;;&lt;br /&gt;          }&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;You should note that this stuff will still work with pre .NET 3.5 via anonymous methods. This one was a real eye-opener! This provides centeral exception handling, automatic transaction management, etc. which will lead to much cleaner design. What else do you need?! Ideas?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-7185771948185614940?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/7185771948185614940/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=7185771948185614940' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7185771948185614940'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7185771948185614940'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/12/atm-with-nhibernate.html' title='ATM with NHibernate'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-2259541140828269149</id><published>2008-12-29T15:07:00.003+03:30</published><updated>2008-12-29T15:15:21.816+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='NHibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='ESB'/><title type='text'>NH + Rhino ESB = Headache?</title><content type='html'>&lt;p&gt;I was to make Rhino Service Bus work with NHibernate the other day. Although, Nhibernate and Rhino Service Bus each work flawlessly, when coupled together the problem started. What I wanted to do was simple. Gather some orders to make a package large enough for shipment gateway to process : &lt;/p&gt; &lt;center&gt;&lt;a href="http://www.hightech.ir/BlogPics/NHRhinoESB_A2C4/ESBOrderingWorkflow.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="ESB-Ordering-Workflow" alt="ESB-Ordering-Workflow" src="http://www.hightech.ir/BlogPics/NHRhinoESB_A2C4/ESBOrderingWorkflow_thumb.png" width="567" border="0" height="303" /&gt;&lt;/a&gt; &lt;/center&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;As soon as I hit the database to check if I have enough orders to create a shipment package, strange errors occured. Here’s what I did when a new order is recieved :&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;ShipmentSaga &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;ConsumerOf&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;NewOrderMessage&lt;/span&gt;&amp;gt;&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:navy;"&gt;public const int &lt;/span&gt;&lt;span style="color:maroon;"&gt;PackageLimit &lt;/span&gt;= &lt;span style="background: rgb(230, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;5&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color:maroon;"&gt;ShipmentSaga&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IServiceBus &lt;/span&gt;&lt;span style="color:maroon;"&gt;bus&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;this&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Bus &lt;/span&gt;= &lt;span style="color:maroon;"&gt;bus&lt;/span&gt;;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IServiceBus &lt;/span&gt;&lt;span style="color:maroon;"&gt;Bus&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;       &lt;span style="color:navy;"&gt;get&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color:navy;"&gt;private set&lt;/span&gt;;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;public void &lt;/span&gt;&lt;span style="color:maroon;"&gt;Consume&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;NewOrderMessage &lt;/span&gt;&lt;span style="color:maroon;"&gt;orderMessage&lt;/span&gt;)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;packagefull &lt;/span&gt;= &lt;span style="color:maroon;"&gt;HasEnoughOrders&lt;/span&gt;();&lt;br /&gt;       &lt;span style="color:navy;"&gt;if &lt;/span&gt;(&lt;span style="color:maroon;"&gt;packagefull&lt;/span&gt;)&lt;br /&gt;       {&lt;br /&gt;           &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;message &lt;/span&gt;= &lt;span style="color:navy;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(166, 83, 0);"&gt;PrepareShipmentMessage&lt;/span&gt;();&lt;br /&gt;           &lt;span style="color:maroon;"&gt;Bus&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Send&lt;/span&gt;(&lt;span style="color:maroon;"&gt;message&lt;/span&gt;);&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:navy;"&gt;private bool &lt;/span&gt;&lt;span style="color:maroon;"&gt;HasEnoughOrders&lt;/span&gt;()&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:navy;"&gt;using&lt;/span&gt;(&lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;session &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;IoC&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Resolve&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ISessionFactory&lt;/span&gt;&amp;gt;().&lt;span style="color:maroon;"&gt;OpenSession&lt;/span&gt;())&lt;br /&gt;       {&lt;br /&gt;           &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;todayOrders &lt;/span&gt;= &lt;span style="color:maroon;"&gt;session&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CreateCriteria&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Order&lt;/span&gt;))&lt;br /&gt;                                    .&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Expression&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Eq&lt;/span&gt;(&lt;span style="background: rgb(255, 255, 230) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;"OrderDate"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Now&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Date&lt;/span&gt;))&lt;br /&gt;                                    .&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Expression&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Eq&lt;/span&gt;(&lt;span style="background: rgb(255, 255, 230) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;"IsShipped"&lt;/span&gt;, &lt;span style="color:navy;"&gt;false&lt;/span&gt;))&lt;br /&gt;                                    .&lt;span style="color:maroon;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;Order&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;           &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;todayOrders&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Count &lt;/span&gt;&amp;gt;= &lt;span style="color:maroon;"&gt;PackageLimit&lt;/span&gt;;&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;As soon as the execution of HasEnoughOrders finished and system tried to dispose and close the session, an exception occured saying : “Disconnect cannot be called while a transaction is in progress”. Ayende says this is the problem of NHibernate handling TransactionScopes. NH registers the session for DTC transactions but it does not defer the transaction’s disposal the way it should. Hopefully the bug will be fixed in future releases of NH and there is a workaround. To “temporarily” avoid this problem, do not explicitly dispose the session : &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:navy;"&gt;private bool &lt;/span&gt;&lt;span style="color:maroon;"&gt;HasEnoughOrders&lt;/span&gt;()&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;session &lt;/span&gt;= &lt;span style="color: rgb(166, 83, 0);"&gt;IoC&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Resolve&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ISessionFactory&lt;/span&gt;&amp;gt;().&lt;span style="color:maroon;"&gt;OpenSession&lt;/span&gt;();&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;var &lt;/span&gt;&lt;span style="color:maroon;"&gt;todayOrders &lt;/span&gt;= &lt;span style="color:maroon;"&gt;session&lt;/span&gt;.&lt;span style="color:maroon;"&gt;CreateCriteria&lt;/span&gt;(&lt;span style="color:navy;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Order&lt;/span&gt;))&lt;br /&gt;                           .&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Expression&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Eq&lt;/span&gt;(&lt;span style="background: rgb(255, 255, 230) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;"OrderDate"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Now&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Date&lt;/span&gt;))&lt;br /&gt;                           .&lt;span style="color:maroon;"&gt;Add&lt;/span&gt;(&lt;span style="color: rgb(166, 83, 0);"&gt;Expression&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Eq&lt;/span&gt;(&lt;span style="background: rgb(255, 255, 230) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;"IsShipped"&lt;/span&gt;, &lt;span style="color:navy;"&gt;false&lt;/span&gt;))&lt;br /&gt;                           .&lt;span style="color:maroon;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(166, 83, 0);"&gt;Order&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:navy;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;todayOrders&lt;/span&gt;.&lt;span style="color:maroon;"&gt;Count &lt;/span&gt;&amp;gt;= &lt;span style="color:maroon;"&gt;PackageLimit&lt;/span&gt;;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Rhino Service Bus is still under development so there might be other minor issues when used together with other technologies and frameworks, but it is a great piece of software. If you’re not familiar with &lt;a target="_blank" href="http://sourceforge.net/projects/rhino-tools"&gt;Rhino Tool&lt;/a&gt; set, you definitely need to take a look. Thank you &lt;a target="_blank" href="http://ayende.com/Blog/"&gt;Ayende&lt;/a&gt; for creating such a great toolkit.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-2259541140828269149?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/2259541140828269149/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=2259541140828269149' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2259541140828269149'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2259541140828269149'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/12/nh-rhino-esb-headache.html' title='NH + Rhino ESB = Headache?'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-7117562016569218975</id><published>2008-12-27T14:01:00.001+03:30</published><updated>2008-12-27T14:01:35.259+03:30</updated><title type='text'>TransactionScope and DTC</title><content type='html'>&lt;p&gt;When working with Rhino Service Bus, the application kept crashing as I hit the database. The exception message says that MSDTC is disabled. It turns out this is because of the tightened security on Windows XP SP2 and DTC services and the problem would manifest if your running your data acess services on a separate machine other than your DB&amp;#160; and instanciate a new transaction using TransactionScope in your services.&lt;/p&gt;  &lt;p&gt;To solve the issue, first you need to check if DTC is properly configured. Do the following steps :&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Go to “Control Panel” –&amp;gt; “Administrative Tools” –&amp;gt; “Component Services”&lt;/li&gt;    &lt;li&gt;On the “Console Root” open the “Component Servcies” until you see “My Computer”&lt;/li&gt;    &lt;li&gt;Right-Click “My Computer” node and select “Properties”&lt;/li&gt;    &lt;li&gt;In “MSDTC” tab click “Security Configuration”&lt;/li&gt;    &lt;li&gt;Make sure “Allow Remote Clients”, “Enable XA Transactions” and “Enable TIP Transactions” have check marks.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Try again to see if the problem is solved. If not, check if the firewall is blocking any calls to DTC by a utility called &lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?familyid=5E325025-4DCD-4658-A549-1D549AC17644&amp;amp;displaylang=en"&gt;DTCPing&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-7117562016569218975?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/7117562016569218975/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=7117562016569218975' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7117562016569218975'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7117562016569218975'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/12/transactionscope-and-dtc.html' title='TransactionScope and DTC'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-2816317017703233794</id><published>2008-12-26T14:50:00.001+03:30</published><updated>2008-12-26T14:50:34.977+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='FarsiLibrary'/><category scheme='http://www.blogger.com/atom/ns#' term='Calendars'/><title type='text'>Farsi Library for the Web</title><content type='html'>&lt;p&gt;There’s been a lot of request for web controls supporting multiple cultures and calendars, like the already existing WinForm Controls of Farsi Library. The existing ASP.NET Calendar control does not render correctly when used in other cultures and this is the case when you’re using FA-IR culture. Although this problem is partially because FA-IR has a wrong calendar set for its default calendar (GregorianCalendar instead of PersianCalendar), there’s no workaround. What I did was to create a new control to correctly render the calendar information in Gregorian Calendar, Persian Calendar and Hijri Calendar.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.hightech.ir/BlogPics/FarsiLibraryfortheWeb_BBA2/FADatePickerWeb.png"&gt;&lt;img title="FADatePicker-Web" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="354" alt="FADatePicker-Web" src="http://www.hightech.ir/BlogPics/FarsiLibraryfortheWeb_BBA2/FADatePickerWeb_thumb.png" width="328" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The control currently works exactly as ASP.NET calendar, meaning it supports all the use cases of standard ASP.NET calendar control like styling, using on update panels, etc. This control with a complementary DatePicker control will be added to the Farsi Library project as I’m planning for the next major release. The pre-beta sources will soon be available in case you are intrested.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-2816317017703233794?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/2816317017703233794/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=2816317017703233794' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2816317017703233794'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/2816317017703233794'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/12/farsi-library-for-web.html' title='Farsi Library for the Web'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-1988699241824073938</id><published>2008-12-20T12:33:00.002+03:30</published><updated>2008-12-20T12:35:16.341+03:30</updated><title type='text'>Cleanup that Console</title><content type='html'>&lt;p&gt;In the middle of investigating the new Rhino Service Bus (more on this on later posts), I was trying to create a console application that’d act like a Console Application Server. Using a console app as a development application service is very common, because running it is a breeze and you can get the log output directly on your screen. But what I didn’t know was that you can actually close the console application by pressing CTRL + C or CTRL + Break without properly closing the application and this sometime will results to very bad things happening like your port being left open and you won’t be able to re-run the application because the port is not properly closed. To my surprise you can not use any managed code to listen to those events!&lt;/p&gt;    &lt;p&gt;My initial console startup code was like this :   &lt;br /&gt;&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Program&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;private static readonly &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ILog &lt;/span&gt;logger = &lt;span style="color: rgb(43, 145, 175);"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;DefaultHost&lt;/span&gt;));&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;public static void &lt;/span&gt;Main(&lt;span style="color:blue;"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:blue;"&gt;var &lt;/span&gt;host = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DefaultHost&lt;/span&gt;();&lt;br /&gt;       host.Start(&lt;span style="color: rgb(43, 145, 175);"&gt;Assembly&lt;/span&gt;.GetExecutingAssembly().FullName);&lt;br /&gt;&lt;br /&gt;       logger.Debug(&lt;span style="color: rgb(163, 21, 21);"&gt;"Press CTRL+C to exit..."&lt;/span&gt;);&lt;br /&gt;       &lt;span style="color:blue;"&gt;bool &lt;/span&gt;shouldexit = &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color:blue;"&gt;while&lt;/span&gt;(!shouldexit)&lt;br /&gt;       {&lt;br /&gt;           &lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleKeyInfo &lt;/span&gt;key = &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.ReadKey(&lt;span style="color:blue;"&gt;true&lt;/span&gt;);&lt;br /&gt;           &lt;span style="color:blue;"&gt;if &lt;/span&gt;(key.Modifiers == &lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleModifiers&lt;/span&gt;.Control &amp;amp;&amp;amp; key.Key == &lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleKey&lt;/span&gt;.C)&lt;br /&gt;           {&lt;br /&gt;               logger.Debug(&lt;span style="color: rgb(163, 21, 21);"&gt;"Closing the Service Bus"&lt;/span&gt;);&lt;br /&gt;               host.Close();&lt;br /&gt;&lt;br /&gt;               logger.Debug(&lt;span style="color: rgb(163, 21, 21);"&gt;"Exiting the application"&lt;/span&gt;);&lt;br /&gt;               shouldexit = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;br /&gt;           }&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;…and surprisingly application closed before I could run the code in the if block! Time to do a little unmanaged magic. There is existing functionality in &lt;strong&gt;&lt;em&gt;Kernel32.dll&lt;/em&gt;&lt;/strong&gt; to listen to console events but those are not exposed as .NET Console events. So first, let’s import that functionality into our managed world :&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public enum &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleEventTypes&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   CtrlC = 0,&lt;br /&gt;   Break = 1,&lt;br /&gt;   Close = 2,&lt;br /&gt;   Logoff = 5,&lt;br /&gt;   Shutdown = 6&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;public delegate void &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleEventHandler&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleEventTypes &lt;/span&gt;consoleEvent);&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleController&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;public event &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleEventHandler &lt;/span&gt;ConsoleEvent;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;public &lt;/span&gt;ConsoleController()&lt;br /&gt;   {&lt;br /&gt;       SetConsoleCtrlHandler(Handler, &lt;span style="color:blue;"&gt;true&lt;/span&gt;);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;private void &lt;/span&gt;Handler(&lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleEventTypes &lt;/span&gt;consoleEvent)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:blue;"&gt;if &lt;/span&gt;(ConsoleEvent != &lt;span style="color:blue;"&gt;null&lt;/span&gt;)&lt;br /&gt;           ConsoleEvent(consoleEvent);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [&lt;span style="color: rgb(43, 145, 175);"&gt;DllImport&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"kernel32.dll"&lt;/span&gt;)]&lt;br /&gt;   &lt;span style="color:blue;"&gt;private static extern bool &lt;/span&gt;SetConsoleCtrlHandler(&lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleEventHandler &lt;/span&gt;e, &lt;span style="color:blue;"&gt;bool &lt;/span&gt;add);&lt;br /&gt;}&lt;/pre&gt;With this, the startup code looks like this :&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Program&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;private static readonly &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ILog &lt;/span&gt;logger = &lt;span style="color: rgb(43, 145, 175);"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;DefaultHost&lt;/span&gt;));&lt;br /&gt;   &lt;span style="color:blue;"&gt;private static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DefaultHost &lt;/span&gt;host;&lt;br /&gt;   &lt;span style="color:blue;"&gt;private static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleController &lt;/span&gt;console;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;public static void &lt;/span&gt;Main(&lt;span style="color:blue;"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;   {&lt;br /&gt;       console = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleController&lt;/span&gt;();&lt;br /&gt;       console.ConsoleEvent += ConsoleEventRaised;&lt;br /&gt;&lt;br /&gt;       host = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DefaultHost&lt;/span&gt;();&lt;br /&gt;       host.Start(&lt;span style="color: rgb(43, 145, 175);"&gt;Assembly&lt;/span&gt;.GetExecutingAssembly().FullName);&lt;br /&gt;&lt;br /&gt;       logger.Debug(&lt;span style="color: rgb(163, 21, 21);"&gt;"Press CTRL+C to exit..."&lt;/span&gt;);&lt;br /&gt;       &lt;span style="color:blue;"&gt;while &lt;/span&gt;(&lt;span style="color:blue;"&gt;true&lt;/span&gt;)&lt;br /&gt;       {&lt;br /&gt;           &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.ReadKey(&lt;span style="color:blue;"&gt;true&lt;/span&gt;);&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style="color:blue;"&gt;private static void &lt;/span&gt;ConsoleEventRaised(&lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleEventTypes &lt;/span&gt;consoleEvent)&lt;br /&gt;   {&lt;br /&gt;       &lt;span style="color:blue;"&gt;if&lt;/span&gt;(consoleEvent == &lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleEventTypes&lt;/span&gt;.CtrlC || consoleEvent == &lt;span style="color: rgb(43, 145, 175);"&gt;ConsoleEventTypes&lt;/span&gt;.Break)&lt;br /&gt;       {&lt;br /&gt;           logger.Debug(&lt;span style="color: rgb(163, 21, 21);"&gt;"Closing the service bus"&lt;/span&gt;);&lt;br /&gt;           host.Close();&lt;br /&gt;&lt;br /&gt;           logger.Debug(&lt;span style="color: rgb(163, 21, 21);"&gt;"Exiting the application now"&lt;/span&gt;);&lt;br /&gt;           &lt;span style="color: rgb(43, 145, 175);"&gt;Environment&lt;/span&gt;.Exit(-1);&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;There actual attached code differs somehow. You need to keep the reference of ConsoleEventHandler in case of a garbage collection so the code has a couple of line of code to property handle disposing of the ConsoleController. You can download the source code &lt;a target="_blank" href="http://cid-4962b6ceabc2cbd7.skydrive.live.com/self.aspx/BlogFiles/ConsoleController.zip"&gt;here&lt;/a&gt;. Hope it helps.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-1988699241824073938?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/1988699241824073938/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=1988699241824073938' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1988699241824073938'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1988699241824073938'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/12/cleanup-that-console.html' title='Cleanup that Console'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-4331434751854998978</id><published>2008-12-18T12:26:00.002+03:30</published><updated>2008-12-18T12:31:18.121+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='General'/><title type='text'>Blog Feed Changes</title><content type='html'>I just switched to &lt;a href="http://www.feedburner.com"&gt;FeedBurner&lt;/a&gt;. If you are one of the people subscribed to this blog's feed, this is just a reminder for you to re-subscribe as you may not get the posts in your blog reader using the old address. Here's the link to &lt;a href="http://feeds.feedburner.com/SeeSharp"&gt;subscribe&lt;/a&gt;. Sorry for the caused inconvenience.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-4331434751854998978?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/4331434751854998978/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=4331434751854998978' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/4331434751854998978'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/4331434751854998978'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/12/blog-feed-changes.html' title='Blog Feed Changes'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-1348475695476185341</id><published>2008-12-16T12:09:00.000+03:30</published><updated>2008-12-16T12:12:26.429+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='WPF'/><title type='text'>WPF Controls : Design-Time Integration</title><content type='html'>One of the things that affects the overall friendliness of your custom WPF controls - but you may not consider implementing it - is the design-time integration. With Cider continuing to exist in VS 2010, there’s a lot of design-time functionality at your disposal. Since the model is different in Cider than creating designers for WinForm controls, I’m trying to shed some light on the subject. To demonstrate this, I’ll show some simple steps on adding design-time capabilities to my FarsiLibrary controls which are a pack of date related controls (DatePickers, MonthView, etc.) freely available here. This is not meant to be a complete guide but will let you build a basic designer if you need to.&lt;br&gt;&lt;br&gt;&lt;em&gt;Note: In order for your designer to get loaded by the design-time host, it is essential that you name like YourProjectName.VisualStudio.Design. Guess this is to force developers move out their design-time logic out of main control’s dll (as was encouraged in WinForms).&lt;/em&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;&lt;font size="Medium"&gt;&lt;span style="color: #3366ff"&gt;Preparing MetaData&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;br&gt;First thing you should do, is to plug into meta-data registration mechanism. We do this by creating any class and implementing IRegisterMetadata interface. Then we need to create our various attributes on a Attribute Table. Here’s how you might do it :&lt;br&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;VisualStudioMetadata &lt;/span&gt;: &lt;span style="color: rgb(43,145,175)"&gt;IRegisterMetadata&lt;br&gt;&lt;/span&gt;{&lt;br&gt;   &lt;span style="color: blue"&gt;public void &lt;/span&gt;Register()&lt;br&gt;   {&lt;br&gt;       &lt;span style="color: rgb(43,145,175)"&gt;AttributeTableBuilder &lt;/span&gt;builder = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;FarsiLibraryVisualStudioAttributeTableBuilder&lt;/span&gt;();&lt;br&gt;       &lt;span style="color: rgb(43,145,175)"&gt;MetadataStore&lt;/span&gt;.AddAttributeTable(builder.CreateTable());&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color: blue"&gt;internal class &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;FarsiLibraryVisualStudioAttributeTableBuilder &lt;/span&gt;: &lt;span style="color: rgb(43,145,175)"&gt;AttributeTableBuilder&lt;br&gt;&lt;/span&gt;{&lt;br&gt;   &lt;span style="color: blue"&gt;internal &lt;/span&gt;FarsiLibraryVisualStudioAttributeTableBuilder()&lt;br&gt;   {&lt;br&gt;   }&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;In order to get this metadata loaded, you need to have the assembly containing this interface beside your Controls assembly and it will eventually gets loaded when needed. Also when updating the design-time assembly, VS.NET unloads the old one and uses the updated assembly. This will bring you a lot of performance gain and ease when creating designers for your controls because you no longer need to exit the current instance of VS.NET and run a new one to get the changes loaded.&lt;br&gt;&lt;br&gt;In fact, unlike WinForm model where design attributes were added to the actual control’s source code, in VS.NET 2008 (and later?) the new model allows you to switch designer assemblies at a later stage or even create a designer for a control you don’t have access to the source code.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;&lt;font size="Medium"&gt;&lt;span style="color: #3366ff"&gt;Toolbox Integration&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;br&gt;WPF controls are usually consisted of various primitive type controls, like shapes, buttons, labels, content controls, etc. These parts are composed to build the actual Control. When the final control’s assembly is loaded in design-time environment, (e.g. VS.NET), control parts will be added to the toolbox but this may not be appropriate since you don’t want your control’s building blocks to appear on the toolbox. However, contols will disappear if a proper toolbox attribute is to them.&lt;br&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;internal class &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;FarsiLibraryVisualStudioAttributeTableBuilder &lt;/span&gt;: &lt;span style="color: rgb(43,145,175)"&gt;AttributeTableBuilder&lt;br&gt;&lt;/span&gt;{&lt;br&gt;   &lt;span style="color: blue"&gt;internal &lt;/span&gt;FarsiLibraryVisualStudioAttributeTableBuilder()&lt;br&gt;   {&lt;br&gt;       AddToolboxBrowsableAttributes();&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;private void &lt;/span&gt;AddToolboxBrowsableAttributes()&lt;br&gt;   {&lt;br&gt;       &lt;span style="color: blue"&gt;var &lt;/span&gt;builder = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;AttributeTableBuilder&lt;/span&gt;();&lt;br&gt;&lt;br&gt;       builder.AddCustomAttributes(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;FXMonthViewButton&lt;/span&gt;),            &lt;span style="color: rgb(43,145,175)"&gt;ToolboxBrowsableAttribute&lt;/span&gt;.No);&lt;br&gt;       builder.AddCustomAttributes(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;FXMonthViewContainer&lt;/span&gt;),         &lt;span style="color: rgb(43,145,175)"&gt;ToolboxBrowsableAttribute&lt;/span&gt;.No);&lt;br&gt;       builder.AddCustomAttributes(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;FXMonthViewHeader&lt;/span&gt;),            &lt;span style="color: rgb(43,145,175)"&gt;ToolboxBrowsableAttribute&lt;/span&gt;.No);&lt;br&gt;       builder.AddCustomAttributes(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;FXMonthViewItem&lt;/span&gt;),              &lt;span style="color: rgb(43,145,175)"&gt;ToolboxBrowsableAttribute&lt;/span&gt;.No);&lt;br&gt;       builder.AddCustomAttributes(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;FXMonthViewWeekDayHeaderCell&lt;/span&gt;), &lt;span style="color: rgb(43,145,175)"&gt;ToolboxBrowsableAttribute&lt;/span&gt;.No);&lt;br&gt;&lt;br&gt;       &lt;span style="color: rgb(43,145,175)"&gt;MetadataStore&lt;/span&gt;.AddAttributeTable(builder.CreateTable());&lt;br&gt;   }&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;a href="http://www.hightech.ir/BlogPics/WPFControlsDesignTimeIntegration_B925/FarsiLibraryDesignerIcons.png"&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="FarsiLibrary-DesignerIcons" border="0" alt="FarsiLibrary-DesignerIcons" align="left" src="http://www.hightech.ir/BlogPics/WPFControlsDesignTimeIntegration_B925/FarsiLibraryDesignerIcons_thumb.png" width="254" height="84"&gt;&lt;/a&gt; This will result your controls not being visible when the toolbox is loaded. In case you to show your custom icons appear in the toolbox you should create a BMP icon and place it on your main control’s assembly (not design-time assembly), and give it a proper name. The name should be &lt;em&gt;YourControl.Icon.bmp&lt;/em&gt; and you should set the bitmap’s action to “Embedded Resource”.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;&lt;font size="Medium"&gt;&lt;span style="color: #3366ff"&gt;Property Grid Integration&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;br&gt;You can specify where and how the control’s property will be shown in PropertyGrid of VS.NET when the control is opened in the design-mode.&lt;br&gt;&lt;br&gt;To hide properties similar to BrowsableAttribute in WinForms you can use the following snippet :&lt;br&gt;&lt;pre class="code"&gt;builder.AddCustomAttributes(&lt;span style="color: rgb(43,145,175)"&gt;FXMonthView&lt;/span&gt;.ButtonStyleProperty, &lt;span style="color: rgb(43,145,175)"&gt;BrowsableAttribute&lt;/span&gt;.No);&lt;/pre&gt;&lt;br&gt;&lt;br&gt;and to specify the category of the Control’s property similar to CategoryAttribute in WinForms:&lt;br&gt;&lt;br&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;behaviorCategory = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;CategoryAttribute&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;"Behavior"&lt;/span&gt;);&lt;br&gt;builder.AddCustomAttributes(&lt;span style="color: rgb(43,145,175)"&gt;FXMonthView&lt;/span&gt;.ViewDateTimeProperty, behaviorCategory);&lt;br&gt;&lt;/pre&gt;&lt;br&gt;Some attributes still work in WPF world. For example you can use the EditorBrowsableAtribute on your properties to make them show in Advanced section of the new WPF Property Grid. The usage is the same as before :&lt;br&gt;&lt;pre class="code"&gt;[&lt;span style="color: rgb(43,145,175)"&gt;EditorBrowsable&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;EditorBrowsableState&lt;/span&gt;.Advanced)]&lt;br&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;DateTime &lt;/span&gt;MaxDate&lt;br&gt;{&lt;br&gt;&lt;span style="color: blue"&gt;    get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;DateTime&lt;/span&gt;)GetValue(MaxDateProperty); }&lt;br&gt;   &lt;span style="color: blue"&gt;set &lt;/span&gt;{ SetValue(MaxDateProperty, &lt;span style="color: blue"&gt;value&lt;/span&gt;); }&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;em&gt;Note: Visual Studio WPF Designer supports the full extensibility framework, but Expression Blend only supports property editors, metadata loading, and licensing. and it does not support menu actions and adorners.&lt;/em&gt;&lt;br&gt;&lt;br /&gt;&lt;center&gt;&lt;br /&gt;&lt;table border="0" cellspacing="0" cellpadding="0" width="400"&gt;&lt;br&gt;&lt;br /&gt;&lt;tbody&gt;&lt;br /&gt;&lt;tr&gt;&lt;br&gt;&lt;br /&gt;&lt;td&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="PropertyGrid-AdvancedProperties" border="0" alt="PropertyGrid-AdvancedProperties" src="http://www.hightech.ir/BlogPics/WPFControlsDesignTimeIntegration_B925/PropertyGridAdvancedProperties_thumb.png" width="332" height="328"&gt;&lt;br&gt;&lt;span style="font-size: xx-small"&gt;Fig. 1 – How advanced properties look on property grid&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/center&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;&lt;font size="Medium"&gt;&lt;span style="color: #3366ff"&gt;Menu Actions&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;br&gt;Another way to change your control’s design-time behavior is to add actions to VS.NET Context-menu which will appear when the control is right-clicked upon. Unlike WinForm’s Designer Verbs which also appeared on the bottom of the Property Grid, these menu actions will only appear when the control is selected on the design surface.&lt;br&gt;&lt;a href="http://www.hightech.ir/BlogPics/WPFControlsDesignTimeIntegration_B925/FarsiLibraryDesignerActions.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="FarsiLibrary-DesignerActions" border="0" alt="FarsiLibrary-DesignerActions" src="http://www.hightech.ir/BlogPics/WPFControlsDesignTimeIntegration_B925/FarsiLibraryDesignerActions_thumb.png" width="514" height="384"&gt;&lt;/a&gt;&lt;br&gt;You can add your menu actions by extending PrimarySelectionContextMenuProvider class and load it with your IRegisterMetadata instance : &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;AddMonthViewMenuItems()&lt;br&gt;{&lt;br&gt;   AddCallback(&lt;span style="color: blue"&gt;typeof &lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;FXMonthView&lt;/span&gt;), builder =&amp;gt; builder.AddCustomAttributes(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;FeatureAttribute&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof &lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;MonthViewDesignMenuProvider&lt;/span&gt;))));&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;MonthViewDesignMenuProvider &lt;/span&gt;: &lt;span style="color: rgb(43,145,175)"&gt;PrimarySelectionContextMenuProvider&lt;br&gt;&lt;/span&gt;{&lt;br&gt;   &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;MenuAction &lt;/span&gt;aboutMenuAction;&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;public &lt;/span&gt;MonthViewDesignMenuProvider()&lt;br&gt;   {&lt;br&gt;       &lt;span style="color: blue"&gt;var &lt;/span&gt;grp = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;MenuGroup&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;"FarsiLibrary"&lt;/span&gt;, &lt;span style="color: rgb(163,21,21)"&gt;"Farsi Library"&lt;/span&gt;);&lt;br&gt;       aboutMenuAction = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;MenuAction&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;"About..."&lt;/span&gt;);&lt;br&gt;       aboutMenuAction.Execute += OnAboutActionExecuted;&lt;br&gt;&lt;br&gt;       grp.Items.Add(aboutMenuAction);&lt;br&gt;       Items.Add(grp);&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;private void &lt;/span&gt;OnAboutActionExecuted(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: rgb(43,145,175)"&gt;MenuActionEventArgs &lt;/span&gt;e)&lt;br&gt;   {&lt;br&gt;       &lt;span style="color: blue"&gt;var &lt;/span&gt;dialog = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;AboutUI&lt;/span&gt;();&lt;br&gt;       dialog.WindowStartupLocation = &lt;span style="color: rgb(43,145,175)"&gt;WindowStartupLocation&lt;/span&gt;.CenterScreen;&lt;br&gt;       dialog.Topmost = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;br&gt;       dialog.ShowDialog();&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;b&gt;&lt;font size="Medium"&gt;&lt;span style="color: #3366ff"&gt;Design-Time Adorners&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;br&gt;To have more control on how users interact with your custom control and to provide them an elaborated experience, you can benefit control adorners to create a more robus design-time functionality. You can create WPF UserControls that manipulate your control in a developer friendly way. You can create a Design-Time Control Adorner by extending PrimarySelectionAdornerProvider class which resides in Microsoft.Windows.Design.Extensibility assembly. Just a couple of methods to override and you’re all set here. Here’s the method’s skeleton to show you what to do :&lt;br&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;MonthViewDesignAdorner &lt;/span&gt;: &lt;span style="color: rgb(43,145,175)"&gt;PrimarySelectionAdornerProvider&lt;br&gt;&lt;/span&gt;{&lt;br&gt;   &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;MonthViewDesignerUI &lt;/span&gt;designerUI;&lt;br&gt;   &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;AdornerPanel &lt;/span&gt;adornersPanel;&lt;br&gt;   &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;ModelItem &lt;/span&gt;calendarModelItem;&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;public &lt;/span&gt;MonthViewDesignAdorner()&lt;br&gt;   {&lt;br&gt;       designerUI = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;MonthViewDesignerUI&lt;/span&gt;();&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;protected override void &lt;/span&gt;Activate(&lt;span style="color: rgb(43,145,175)"&gt;ModelItem &lt;/span&gt;item, &lt;span style="color: rgb(43,145,175)"&gt;DependencyObject &lt;/span&gt;view)&lt;br&gt;   {&lt;br&gt;       calendarModelItem = item;&lt;br&gt;&lt;br&gt;       CreateAdornerPanel();&lt;br&gt;       PlaceAdornerPanel();&lt;br&gt;       SubscribeDesignerEvents();&lt;br&gt;&lt;br&gt;       &lt;span style="color: blue"&gt;base&lt;/span&gt;.Activate(item, view);&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;protected override void &lt;/span&gt;Deactivate()&lt;br&gt;   {&lt;br&gt;       UnsubscribeDesignerEvents();&lt;br&gt;&lt;br&gt;       &lt;span style="color: blue"&gt;base&lt;/span&gt;.Deactivate();&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;private void &lt;/span&gt;OnDesignerUIPropertyChanged(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: rgb(43,145,175)"&gt;PropertyChangedEventArgs &lt;/span&gt;e)&lt;br&gt;   {&lt;br&gt;&lt;span style="color: green"&gt;        //Set the actual control's property with the updated value&lt;/span&gt;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;private void &lt;/span&gt;PlaceAdornerPanel()&lt;br&gt;   {&lt;br&gt;      &lt;span style="color: green"&gt;//Create an AdornerPanel and set it’s placement&lt;br&gt;      //to display whereever you like when your contol gets selected&lt;br&gt;      //on design surface.&lt;/span&gt;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;private void &lt;/span&gt;OnDesignerUILoaded(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: rgb(43,145,175)"&gt;RoutedEventArgs &lt;/span&gt;e)&lt;br&gt;   {&lt;br&gt;&lt;span style="color: green"&gt;        //When the control designer is loaded we need to synchronoze the designer’s&lt;br&gt;       //properties with actual property values of the control and set the&lt;br&gt;       //element that displays this value on the designer accordingly.&lt;/span&gt;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;private void &lt;/span&gt;SubscribeDesignerEvents()&lt;br&gt;   {&lt;br&gt;       designerUI.Loaded += OnDesignerUILoaded;&lt;br&gt;       designerUI.PropertyChanged += OnDesignerUIPropertyChanged;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;private void &lt;/span&gt;UnsubscribeDesignerEvents()&lt;br&gt;   {&lt;br&gt;       designerUI.Loaded -= OnDesignerUILoaded;&lt;br&gt;       designerUI.PropertyChanged -= OnDesignerUIPropertyChanged;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;private void &lt;/span&gt;CreateAdornerPanel()&lt;br&gt;   {&lt;br&gt;&lt;span style="color: green"&gt;        //Create a new AdornerPanel and set our&lt;br&gt;       //DesignerUI as its child.&lt;/span&gt;&lt;br&gt;   }&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;To synchronize the values between our Designer and Control, the designer should implement INotifyPropertyChanged interface to report back when a property value is change in design-time and we set property on the control accordingly. Control’s properties are accessible via our ModelItem instance which is set in OnActivate method. For example to access a dependency property on our control and set it’s value we need to do the following : &lt;br&gt;&lt;br&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;ModelProperty &lt;/span&gt;prop = calendarModelItem.Properties[&lt;span style="color: rgb(163,21,21)"&gt;"SelectedDateTime"&lt;/span&gt;];&lt;br&gt;prop.SetValue(&lt;span style="color: rgb(43,145,175)"&gt;DateTime&lt;/span&gt;.Now);&lt;br&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;pre&gt;&lt;/pre&gt;&lt;br&gt;As for our designer goes, you can create a WPF user interface, a WinForm dialog or even both (through WPF and WinForm Integration mechanism). Here, I’ve created a UserControl to act as my designer and added some simple controls to edit the properties of my actual controls. To handle editing of SelectedDateTime property (which is of nullable DateTime type), I have used another instance of my FXDatePicker on the design surface (the design-time control’s are skinned). Here’s the designer’s XAML :&lt;br&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;UserControl &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;="FarsiLibrary.WPF.VisualStudio.Design.MonthViewDesignerUI"&lt;br&gt;   &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;br&gt;   &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;br&gt;   &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Controls&lt;/span&gt;&lt;span style="color: blue"&gt;="clr-namespace:FarsiLibrary.WPF.Controls;assembly=FarsiLibrary.WPF"&amp;gt;&lt;br&gt;      &lt;br&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Expander &lt;/span&gt;&lt;span style="color: red"&gt;IsExpanded&lt;/span&gt;&lt;span style="color: blue"&gt;="False" &lt;/span&gt;&lt;span style="color: red"&gt;Margin&lt;/span&gt;&lt;span style="color: blue"&gt;="10" &lt;/span&gt;&lt;span style="color: red"&gt;Padding&lt;/span&gt;&lt;span style="color: blue"&gt;="10" &lt;/span&gt;&lt;span style="color: red"&gt;Header&lt;/span&gt;&lt;span style="color: blue"&gt;="Options..."&amp;gt;&lt;br&gt;       &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;StackPanel &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;="ContentPanel" &lt;/span&gt;&lt;span style="color: red"&gt;SnapsToDevicePixels&lt;/span&gt;&lt;span style="color: blue"&gt;="True"&amp;gt;&lt;br&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;CheckBox &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;="Show Today Button" &lt;/span&gt;&lt;span style="color: red"&gt;IsChecked&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=ShowTodayButton}" /&amp;gt;&lt;br&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;CheckBox &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;="Show Empty Button" &lt;/span&gt;&lt;span style="color: red"&gt;IsChecked&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=ShowEmptyButton}" /&amp;gt;&lt;br&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;CheckBox &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;="Show WeekDay Names" &lt;/span&gt;&lt;span style="color: red"&gt;IsChecked&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=ShowWeekDayNames}" /&amp;gt;&lt;br&gt;&lt;br&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Rectangle &lt;/span&gt;&lt;span style="color: red"&gt;Fill&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;DynamicResource &lt;/span&gt;&lt;span style="color: red"&gt;Text1Brush&lt;/span&gt;&lt;span style="color: blue"&gt;}" &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;="1" &lt;/span&gt;&lt;span style="color: red"&gt;SnapsToDevicePixels&lt;/span&gt;&lt;span style="color: blue"&gt;="True" &lt;/span&gt;&lt;span style="color: red"&gt;Margin&lt;/span&gt;&lt;span style="color: blue"&gt;="-5,5,-5,5" /&amp;gt;&lt;br&gt;          &lt;br&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;StackPanel &lt;/span&gt;&lt;span style="color: red"&gt;Orientation&lt;/span&gt;&lt;span style="color: blue"&gt;="Horizontal"&amp;gt;&lt;br&gt;               &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;="Selected Date : " &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;="150" /&amp;gt;&lt;br&gt;               &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Controls&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;FXDatePicker &lt;/span&gt;&lt;span style="color: red"&gt;SelectedDateTime&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=SelectedDateTime}" &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;="120" /&amp;gt;&lt;br&gt;           &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;          &lt;br&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Rectangle &lt;/span&gt;&lt;span style="color: red"&gt;Fill&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;DynamicResource &lt;/span&gt;&lt;span style="color: red"&gt;Text1Brush&lt;/span&gt;&lt;span style="color: blue"&gt;}" &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;="1" &lt;/span&gt;&lt;span style="color: red"&gt;SnapsToDevicePixels&lt;/span&gt;&lt;span style="color: blue"&gt;="True" &lt;/span&gt;&lt;span style="color: red"&gt;Margin&lt;/span&gt;&lt;span style="color: blue"&gt;="-5,5,-5,5" /&amp;gt;&lt;br&gt;&lt;br&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;StackPanel &lt;/span&gt;&lt;span style="color: red"&gt;Orientation&lt;/span&gt;&lt;span style="color: blue"&gt;="Horizontal"&amp;gt;&lt;br&gt;               &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;="Maximum Selectable Date : " &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;="150" /&amp;gt;&lt;br&gt;               &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Controls&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;FXDatePicker &lt;/span&gt;&lt;span style="color: red"&gt;SelectedDateTime&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=MaxDate}" &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;="120" /&amp;gt;&lt;br&gt;           &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;           &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;StackPanel &lt;/span&gt;&lt;span style="color: red"&gt;Orientation&lt;/span&gt;&lt;span style="color: blue"&gt;="Horizontal"&amp;gt;&lt;br&gt;               &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;="Minimum Selectable Date : " &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;="150" /&amp;gt;&lt;br&gt;               &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Controls&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;FXDatePicker &lt;/span&gt;&lt;span style="color: red"&gt;SelectedDateTime&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=MinDate}" &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;="120" /&amp;gt;&lt;br&gt;           &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;       &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;   &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Expander&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;UserControl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;br&gt;Basically, when a property is changed on our designer, we’ll raise the INotifyPropertyChanged event with the newly changed value and matching property name on the control that should be changed. You could directly bind your designer’s control to the actual control’s properties and it would work but this gives you more control. The code-behind looks very simple :&lt;br&gt;&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;&lt;br&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Designer control to visually set FXMonthView's&lt;br&gt;&lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;properties on design-time.&lt;br&gt;&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;MonthViewDesignerUI &lt;/span&gt;: &lt;span style="color: rgb(43,145,175)"&gt;INotifyPropertyChanged&lt;br&gt;&lt;/span&gt;{&lt;br&gt;   &lt;span style="color: blue"&gt;public event &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;PropertyChangedEventHandler &lt;/span&gt;PropertyChanged;&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;private bool &lt;/span&gt;_ShowEmptyButton;&lt;br&gt;   &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;DateTime&lt;/span&gt;? _SelectedDateTime;&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;public &lt;/span&gt;MonthViewDesignerUI()&lt;br&gt;   {&lt;br&gt;       InitializeComponent();&lt;br&gt;&lt;br&gt;       &lt;span style="color: blue"&gt;this&lt;/span&gt;.Loaded += OnLoaded;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;private void &lt;/span&gt;OnLoaded(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: rgb(43,145,175)"&gt;RoutedEventArgs &lt;/span&gt;e)&lt;br&gt;   {&lt;br&gt;       &lt;span style="color: blue"&gt;this&lt;/span&gt;.DataContext = &lt;span style="color: blue"&gt;this&lt;/span&gt;;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;public bool &lt;/span&gt;ShowEmptyButton&lt;br&gt;   {&lt;br&gt;       &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_ShowEmptyButton; }&lt;br&gt;       &lt;span style="color: blue"&gt;set&lt;br&gt;       &lt;/span&gt;{&lt;br&gt;           _ShowEmptyButton = &lt;span style="color: blue"&gt;value&lt;/span&gt;;&lt;br&gt;           RaisePropertyChanged(&lt;span style="color: rgb(163,21,21)"&gt;"ShowEmptyButton"&lt;/span&gt;, &lt;span style="color: blue"&gt;value&lt;/span&gt;);&lt;br&gt;       }&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;DateTime&lt;/span&gt;? SelectedDateTime&lt;br&gt;   {&lt;br&gt;       &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_SelectedDateTime; }&lt;br&gt;       &lt;span style="color: blue"&gt;set&lt;br&gt;       &lt;/span&gt;{&lt;br&gt;           _SelectedDateTime = &lt;span style="color: blue"&gt;value&lt;/span&gt;;&lt;br&gt;           RaisePropertyChanged(&lt;span style="color: rgb(163,21,21)"&gt;"SelectedDateTime"&lt;/span&gt;, &lt;span style="color: blue"&gt;value&lt;/span&gt;);&lt;br&gt;       }&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;protected void &lt;/span&gt;RaisePropertyChanged(&lt;span style="color: blue"&gt;string &lt;/span&gt;propertyName, &lt;span style="color: blue"&gt;object &lt;/span&gt;value)&lt;br&gt;   {&lt;br&gt;       &lt;span style="color: blue"&gt;if&lt;/span&gt;(PropertyChanged != &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;br&gt;       {&lt;br&gt;           PropertyChanged(&lt;span style="color: blue"&gt;this&lt;/span&gt;, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;DesignerPropertyChangedEventArgs&lt;/span&gt;(propertyName, value));&lt;br&gt;       }&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;Now, PropertyChangedEventArgs sent by INotifyPropertyChanged does not send the actual value (though you can read it through the sender). To easily read the changed value I’m using an extension to PropertyChangedEventArgs named DesignerPropertyChangedEventArgs (works just like PropertyChangedEventArgs but also has the value inside). With this, our synchronization code looks like this :&lt;br&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;OnDesignerUIPropertyChanged(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: rgb(43,145,175)"&gt;PropertyChangedEventArgs &lt;/span&gt;e)&lt;br&gt;{&lt;br&gt;   &lt;span style="color: rgb(43,145,175)"&gt;ModelProperty &lt;/span&gt;prop = calendarModelItem.Properties[e.PropertyName];&lt;br&gt;   &lt;span style="color: blue"&gt;var &lt;/span&gt;args = e &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;DesignerPropertyChangedEventArgs&lt;/span&gt;;&lt;br&gt;&lt;br&gt;   &lt;span style="color: blue"&gt;if&lt;/span&gt;(prop != &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; args != &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;br&gt;   {&lt;br&gt;       prop.SetValue(args.Value);&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;br /&gt;&lt;center&gt;&lt;a href="http://www.hightech.ir/BlogPics/WPFControlsDesignTimeIntegration_B925/FarsiLibraryDesignAdorner.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="FarsiLibrary-DesignAdorner" border="0" alt="FarsiLibrary-DesignAdorner" src="http://www.hightech.ir/BlogPics/WPFControlsDesignTimeIntegration_B925/FarsiLibraryDesignAdorner_thumb.png" width="690" height="384"&gt;&lt;/a&gt;&lt;/center&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;&lt;font size="medium"&gt;&lt;span style="color: #3366ff"&gt;Conclusion&lt;/span&gt;&lt;/font&gt;&lt;/b&gt;&lt;br&gt;Designer integration with seems way better than WinForm world and with WPF powers you can create very sophisticated designers. The only drawback is that Cider and Expression Blend designers are different, both in how they work and what functionality they provide so you might need to create separate design assemblies one for each environment which is an unnecessary burden to me. This is just a scratch in the surface of what you can achieve by creating custom designers for WPF Controls. &lt;br /&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-1348475695476185341?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/1348475695476185341/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=1348475695476185341' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1348475695476185341'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/1348475695476185341'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/12/wpf-controls-design-time-integration.html' title='WPF Controls : Design-Time Integration'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-4372320931228261301</id><published>2008-12-16T11:26:00.001+03:30</published><updated>2008-12-16T11:26:26.393+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Interoperation'/><title type='text'>Java &amp; .NET Integration : Who is Who?</title><content type='html'>&lt;p&gt;There’s a new kid on the block! An affort to port Java frameworks to .NET is not a new thing and it’s been around since the early days of .NET 1, but integration of .NET Framework and Java Development Kit (JDK) is something I’m not used to see everyday! The new project named “&lt;em&gt;&lt;a target="_blank" href="http://www.janetdev.org/"&gt;Ja.NET&lt;/a&gt;&lt;/em&gt;” enables you to use first class development and runtime environment for .NET in which you use supposedly can use your existing Java SE code base and knowledge but also leverage existing functionalities available .NET Framework! &lt;/p&gt;  &lt;p&gt;Ja.NET provides you with equivalance of Java 5 SE SDK implemented from scrach, JDK tools, class libraries and .NET CLR runtime environment. Also you can use available functionalities in .NET world such as Custom Attributes, Value Types, Extension Methods, etc. The question is : Are we back on J# days? Definetly no. I’m talking about an open-source community here and the roadmap says there will be release comparable to J2ME and J2EE sometime in the future.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-4372320931228261301?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/4372320931228261301/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=4372320931228261301' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/4372320931228261301'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/4372320931228261301'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/12/java-net-integration-who-is-who.html' title='Java &amp;amp; .NET Integration : Who is Who?'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-6193711610563783065</id><published>2008-12-11T12:00:00.000+03:30</published><updated>2008-12-11T12:02:43.315+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Interoperation'/><title type='text'>Java Interoperability</title><content type='html'>&lt;p&gt;One thing we’ve learned the hard way when developing large scale applications, was to use incomparable existing functionalities in Java world! Things like Message Queues, Task Scheduling, Load Balancing and fail over, etc, and yes, that means connecting your .NET application to a Java application server on the backend. I’ve been working on a &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Proof_of_concept"&gt;PoC&lt;/a&gt; for this on the last couple of weeks, so there’ll be yet another enterprise comparison between what new EJB 3.0 and .NET can offer you. Stay tuned. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-6193711610563783065?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/6193711610563783065/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=6193711610563783065' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6193711610563783065'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/6193711610563783065'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/12/java-interoperability.html' title='Java Interoperability'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-7925537416300925105</id><published>2008-11-17T16:49:00.000+03:30</published><updated>2008-11-17T16:55:02.798+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Resharper'/><category scheme='http://www.blogger.com/atom/ns#' term='Add-Ins'/><title type='text'>Resharper : The Most frequently used feature</title><content type='html'>A while a go, &lt;a href="http://rorybecker.blogspot.com/"&gt;Rory&lt;/a&gt; asked me to blog about Resharper add-in for Visual Studio.NET and which shortcut keys I use the most. This one has been waiting on my draft list for a couple of weeks now. This post is about Resharper shortcuts I mostly use.&lt;br /&gt;&lt;br /&gt;Resharper is &lt;a target="_blank" href="http://devexpress.com/CodeRush"&gt;one&lt;/a&gt; the most advanced and useful add-in available for Visual Studio .NET. I've started using the tool when they published their release for Visual Studi 2003. Back then, Resharper added lots of feature and brought productivity to the IDE which without them, your programming life would have been a nightmare.&lt;br /&gt;&lt;br /&gt;The problem with Resharper though, is that it has become so big and featurefull, that is won't be possible to use it in large scale projects. We have a bloated Solution containing about 80 projects that started back in 2003 with only 10-12 projects. When I open the project in VS with Resharper running, the files with 2-3K LOC make Resharper run out of memory. 2-3K LOC might sound unrealistic to you, but automatically generated codes like DataSets or Forms code behinds are usually this large. So, it'd be a good idea if we could disable some Resharper features to gain some more speed, e.g. disabling code analysis in .Designer files, disabling XAML analysis, etc.&lt;br /&gt;&lt;br /&gt;As said before, there are a lot of features in Resharper, which makes your programming experience a pleasant one. Here I’m only pointing to some of the ShortCut keys I mostly use.&lt;br /&gt;&lt;br /&gt;&lt;span style="COLOR: rgb(51,102,255);font-size:100%;" &gt;&lt;span style="FONT-WEIGHT: bold"&gt;Find Usage Window (Alt + F7)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;I’m Sure you've tried to figure out how / where an interface is implemented, or where instance of a certain class is being used, or other similar scenarios. If you use plain VS, you have no other choice than to use "Find All References". That will work, but it takes time every time you do a search like this. This is due to the fact that VS does not keep an index of your application structure, but since Resharper does, you'll gain a lot of speed when doing these kind of operation.&lt;br /&gt;&lt;br /&gt;Also, the "Find Usage Window" is more rich in Resharper than VS. You can sort and group the results based on Namespace, Usage, File, Folder, etc. and filter by Read, Writer, Invocation, etc. but in VS all you see is a flat search result. One of the great things about Find Usage function is that it actually finds the usage in Xaml code too!&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.hightech.ir/BlogPics/ResharperTheMostfrequentlyusedfeature_D57B/ResharperFindUsageResults.png"&gt;&lt;/a&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title="Resharper-FindUsageResults" border="0" alt="Resharper-FindUsageResults" src="http://www.hightech.ir/BlogPics/ResharperTheMostfrequentlyusedfeature_D57B/ResharperFindUsageResults_thumb.png" width="724" height="377" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#3366ff;"&gt;&lt;strong&gt;Using Hints and Red / Yellow Bulb (Alt + Enter)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;Resharper has a good way of letting you know that you can do something better, by displaying "Hints". Hints let you know if you've forgotten to import a namespace, if there's a refactoring available or suggest a context action. There are lots of things you can do in Resharper, just by pressing Alt + Enter! Some of them are :&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Change scope of the Class or Method &lt;/li&gt;&lt;li&gt;Create overload for a method / Check if the variable is null &lt;/li&gt;&lt;li&gt;Create derived types &lt;/li&gt;&lt;li&gt;Import missing namespace / remove unused namespace &lt;/li&gt;&lt;li&gt;Implement an interface / abstract class &lt;/li&gt;&lt;li&gt;Change property to Automated property and vice versa &lt;/li&gt;&lt;li&gt;Invert if statement / Convert to conditional statement and vice versa &lt;/li&gt;&lt;li&gt;Join / Split declaration and assignment of the variable &lt;/li&gt;&lt;li&gt;Introduce variable / use var keyword &lt;/li&gt;&lt;li&gt;Generate body of the switch statement &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;…and a lot more. Depending on where the cursor is in the editor, where you click and the code you have written, you may get different options.&lt;/p&gt;&lt;br /&gt;&lt;span style="COLOR: rgb(51,102,255); FONT-WEIGHT: bold"&gt;Goto Type / File (Ctrl + N, Ctrl + Shift + N)&lt;/span&gt;&lt;br /&gt;If you're looking for a specific file in your large project or if you're using multiple class per file strategy, you might get lost easily. If you need to know where that file is located, or in which file you class resides, you can use these shortcuts to get you there. The "Goto Type" function has support for a feature called CammleHumps. Using this feature, if you're looking for OrderApprovalService, you can search for OAS instead and Resharper will find it for you.&lt;br /&gt;&lt;br /&gt;&lt;table border="0" cellspacing="10" cellpadding="2" width="400" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td valign="top" width="200"&gt;&lt;p align="center"&gt;&lt;a href="http://www.hightech.ir/BlogPics/ResharperTheMostfrequentlyusedfeature_D57B/FindByType.png"&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="FindByType" border="0" alt="FindByType" src="http://www.hightech.ir/BlogPics/ResharperTheMostfrequentlyusedfeature_D57B/FindByType_thumb.png" width="374" height="240" /&gt;&lt;/a&gt; &lt;/p&gt;&lt;/td&gt;&lt;td valign="top" width="200"&gt;&lt;a href="http://www.hightech.ir/BlogPics/ResharperTheMostfrequentlyusedfeature_D57B/FindByFilename.png"&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="FindByFilename" border="0" alt="FindByFilename" src="http://www.hightech.ir/BlogPics/ResharperTheMostfrequentlyusedfeature_D57B/FindByFilename_thumb.png" width="336" height="125" /&gt;&lt;/a&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3366ff;"&gt;Code Completion and Type Lookup (Ctrl + Space, Ctrl + Alt + Space)&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;When you’re working with large libraries you may easily get lost. Sometimes you know the name of type you’re going to use, but can’t remember the darn namespace! This shortcut will help you find the type you are looking for in ALL the namespaces in an easy to use fasion.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.hightech.ir/BlogPics/ResharperTheMostfrequentlyusedfeature_D57B/FindByCtrlSpace.png"&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="FindByCtrlSpace" border="0" alt="FindByCtrlSpace" src="http://www.hightech.ir/BlogPics/ResharperTheMostfrequentlyusedfeature_D57B/FindByCtrlSpace_thumb.png" width="473" height="199" /&gt;&lt;/a&gt; &lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3366ff;"&gt;Code Generation (ALT + Ins, Ctrl + J)&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;The code generation feature in Resharper is just great. You can use the primary code generation function by ALT + Ins that will let you generate : &lt;/p&gt;&lt;ul&gt;&lt;li&gt;Constructors &lt;/li&gt;&lt;li&gt;Properties / Readonly Properties from backing fields &lt;/li&gt;&lt;li&gt;Implement Method / Property stubs for an Interface &lt;/li&gt;&lt;li&gt;Override base class Methods &lt;/li&gt;&lt;li&gt;Provide equality implementation (Equality Operators, IEquatable&amp;lt;T&amp;gt; interface, overriding Equals and GetHashCode methods) &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;While this is great, Resharper gives you the power to write your own snippets! A lot of people have already created snippet templates for Resharper to create Unit tests, Mocks, NHibernate, ActiveRecord and WPF stuff, etc.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.hightech.ir/BlogPics/ResharperTheMostfrequentlyusedfeature_D57B/ResharperCodeGeneration.png"&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Resharper-CodeGeneration" border="0" alt="Resharper-CodeGeneration" src="http://www.hightech.ir/BlogPics/ResharperTheMostfrequentlyusedfeature_D57B/ResharperCodeGeneration_thumb.png" width="656" height="181" /&gt;&lt;/a&gt; &lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;You can download Resharper templates on Joey Beninghove’s &lt;a target="_blank" href="http://joeydotnet.com/blog/archive/2007/04/14/ReSharper-Template-Library---Updated.aspx"&gt;blog&lt;/a&gt;, or see what other people use on StackOverflow &lt;a target="_blank" href="http://stackoverflow.com/questions/186970/what-resharper-40-live-templates-for-c-do-you-use"&gt;page&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#3366ff;"&gt;Summary&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;There is a &lt;a target="_blank" href="http://www.jetbrains.com/resharper/docs/ReSharper40DefaultKeymap.pdf"&gt;document&lt;/a&gt; on JetBrain’s website that shows all the ShortCut keys you’ll ever need to know. Intense Resharper users might need to hang it on their wall. There are lots of other features I use like Unit Test Runner, Stack Trace browser, File Structure window, and ToDo List, but that’s for another post. Do you use productivity tools too? Which one? Which feature do you use the most? &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-7925537416300925105?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/7925537416300925105/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=7925537416300925105' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7925537416300925105'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/7925537416300925105'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/11/resharper-most-frequently-used-feature.html' title='Resharper : The Most frequently used feature'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-3714874884378241759</id><published>2008-11-11T15:25:00.001+03:30</published><updated>2008-11-11T15:25:44.255+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='VS.NET'/><title type='text'>Visual Studio 2010 CTP Test Drive</title><content type='html'>It's been a couple of week that the CTP version of Visual Studio 2010 and .NET Framework 4.0 is out, but I finally got mine (and some free time of course), so I dived to see what's new.   &lt;p&gt;First thing I expected to see, but didn't, was the new WPF shell. Although some modifications are made in the current release shell, it is obvious that it is not entirely a WPF shell. The first thing that is obvious in current release is the WPF TextEditor that looks like an enhanced version of Expression Blend Xaml editor! Turns out, there is a registery key to notify the IDE to show the WPF version of the Shell, but that is not recommended. I Guess there are more work to do in that area and it didn't make it to the CTP version, so you'd better wait for next releases. This CTP release is a Team System edition, and there are lots of features to play with, but some of them might not be in other editions like Professional when the final version is out.    &lt;br /&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;h4&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;UML Diagrams&lt;/font&gt;&lt;/strong&gt;&lt;/h4&gt; In previous versions of Visual Studio diagram designs were limited to a Class Designer, and of course custom designers for the late Linq2Sql, etc. but in this release there are more UML designing powers at your disposal that makes VS into a CASE tool. You don’t have to leave your IDE or bug another product just to use UML Diagrams and since it built into the IDE, extension and integration with it is very easy. Available diagrams are Activity Diagram, Sequence Diagram, Use Case Diagram, Component Diagram and our old Class Diagram.   &lt;br /&gt;  &lt;br /&gt;  &lt;table border="0" cellspacing="5" cellpadding="5" width="400" align="center"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="200"&gt;&lt;a href="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/ActivityDiagram.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ActivityDiagram" border="0" alt="ActivityDiagram" src="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/ActivityDiagram_thumb.png" width="240" height="155" /&gt;&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="200"&gt;&lt;a href="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/SequenceDiagram.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SequenceDiagram" border="0" alt="SequenceDiagram" src="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/SequenceDiagram_thumb.png" width="240" height="252" /&gt;&lt;/a&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;Call Hierarchies&lt;/font&gt;&lt;/strong&gt;&lt;/h4&gt; The IDE now has a new feature to find other references to a Variable, Method, or Constructor. This feature, while IS useful, does not come close to Navigations of productivity tools such as Resharper, where you can find *everything* about a reference you may think of, but again, this is just CTP and things may get better in the final release. I tested this feature on the “Dinner Now” demo project and the Call Hierarchy window didn’t take long to appear, but I have no idea how much time it takes in large scale projects.   &lt;br /&gt;&lt;a href="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/CallHierarchy.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="CallHierarchy" border="0" alt="CallHierarchy" src="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/CallHierarchy_thumb.png" width="709" height="355" /&gt;&lt;/a&gt;   &lt;br /&gt;  &lt;h4&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;Quick Search&lt;/font&gt;&lt;/strong&gt;&lt;/h4&gt; There is a new search tool available that lets you find various types. The new search tool is named Quick Search. By entering a keyword in the search dialog, the IDE finds all the types matching that name. I think a simple search dialog would not be enough and more control on the type that is being searched (e.g. Interface, Class, etc.) and the ability to group the search result (e.g. by namespace, folder, assembly) is necessary.   &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/QuickSearch.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="QuickSearch" border="0" alt="QuickSearch" align="center" src="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/QuickSearch_thumb.png" width="570" height="315" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;br /&gt;  &lt;h4&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;Background Compilation&lt;/font&gt;&lt;/strong&gt;&lt;/h4&gt; VS now uses a background compiler to compile the code as you write. As you write your code the IDE marks the errors so you don't have to compile your big project to find out about small typos here and there. You can use this feature in parallel with the other feature named “Create from usage”.   &lt;br /&gt;  &lt;br /&gt;&lt;a href="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/BackgroundCompilation.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="BackgroundCompilation" border="0" alt="BackgroundCompilation" src="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/BackgroundCompilation_thumb.png" width="568" height="132" /&gt;&lt;/a&gt;&amp;#160; &lt;br /&gt;  &lt;br /&gt;  &lt;h4&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;Create From Usage&lt;/font&gt;&lt;/strong&gt;&lt;/h4&gt; Visual Studio now offers to create your types based on their usages. This is something mostly used in TDD with Test First approach and tools like Resharper had something even better than this, ages ago! As you write code (test?), VS checks it with the new background compiler and marks it as error, so you get an option to fix the error by creating the missing Class, interface or even Methods and Properties.   &lt;br /&gt;  &lt;br /&gt;&lt;a href="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/NewTypeDialog.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="NewTypeDialog" border="0" alt="NewTypeDialog" src="http://www.hightech.ir/BlogPics/VisualStudio2010CTPTestDrive_9BB4/NewTypeDialog_thumb.png" width="343" height="321" /&gt;&lt;/a&gt;   &lt;br /&gt;  &lt;br /&gt;  &lt;br /&gt;  &lt;h4&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;Wix Integration&lt;/font&gt;&lt;/strong&gt;&lt;/h4&gt; Finally WiX, which was initially an open source project, got integrated with Visual Studio. For those not heard of it, Wix is an installation maker that is flexible, simple, and powerful. Creating setup files with WiX is simple : Create an XML file describing your files, and installation package (where is an .XSD available which can be used as intellisense helper or to validate your XML file), then the WiX engine will create working MSI files. Back when I tested WiX 2.0, you had to manually write the XML file, but now there are commercial and free WiX designers to help you create the XML files. The version of WiX that will ship with Visual Studio apparently is WiX 3.0.   &lt;p&gt;   &lt;br /&gt;&lt;/p&gt;  &lt;h4&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;Overall&lt;/font&gt;&lt;/strong&gt;&lt;/h4&gt; Depending on your point of view, changes in the TextEditor and Extensibility of the IDE might or might not be an issue, but so far I didn’t see a BIG change in the IDE, though there may be more changes in the next releases. Newly added feature seem somewhat unfinished, but this is pre-beta stuff so this is natural. These are just IDE features and there are great stull in the .NET Framework 4.0 that also come with the CTP version.    &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6069495622049300789-3714874884378241759?l=heskandari.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://heskandari.blogspot.com/feeds/3714874884378241759/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6069495622049300789&amp;postID=3714874884378241759' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/3714874884378241759'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6069495622049300789/posts/default/3714874884378241759'/><link rel='alternate' type='text/html' href='http://heskandari.blogspot.com/2008/11/visual-studio-2010-ctp-test-drive.html' title='Visual Studio 2010 CTP Test Drive'/><author><name>Hadi Eskandari</name><uri>http://www.blogger.com/profile/13572678534063335373</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/_Z5KTIfnfuNs/SiS9r4PVMRI/AAAAAAAAAO0/6moWfacoonE/s1600-R/Me-Small_bigger.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6069495622049300789.post-4913696014105875618</id><published>2008-11-03T17:00:00.024+03:30</published><updated>2008-11-04T20:03:59.251+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='WPF'/><category scheme='http://www.blogger.com/atom/ns#' term='Ribbon'/><title type='text'>WPF Ribbon Control</title><content type='html'>&lt;p&gt;&lt;a href="http://weblogs.asp.net/Scottgu/" target="_blank"&gt;Scott Guthrie&lt;/a&gt; announced the new Ribbon Control a few days ago, so I thought give it a shot to see how it works.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;&lt;span style="color: rgb(51, 102, 255);"&gt;Referencing the Ribbon&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;First, create a window and import the control's namespace. If you're using the dll provided in RibbonBinaries folder, you can not use namespace mapped to Xmlns! This is because the file "AssemblyAttrs.cs" is not included in the project file, so the Xml namespace won't map to CLR namespace correctly. To fix this, you should recompile the control. Open the project file and add the existing "AssemblyAttrs.cs" file to the project, remove the redundant code that is duplicated in AssemblyInfo.cs, and build. Use the newly compiled dll file instead. After doing so, you can import the mapped namespace :&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Window &lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;Class&lt;/span&gt;&lt;span style="color:blue;"&gt;="MSRibbon.MainWindow"&lt;br /&gt;&lt;/span&gt;&lt;span style="color:red;"&gt;    xmlns&lt;/span&gt;&lt;span style="color:blue;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;br /&gt;&lt;/span&gt;&lt;span style="color:red;"&gt;    xmlns&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;x&lt;/span&gt;&lt;span style="color:blue;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;br /&gt;&lt;/span&gt;&lt;span style="color:red;"&gt;    xmlns&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:red;"&gt;rib&lt;/span&gt;&lt;span style="color:blue;"&gt;="http://schemas.microsoft.com/wpf/2008/ribbon"&lt;br /&gt;&lt;/span&gt;&lt;span style="color:red;"&gt;    Title&lt;/span&gt;&lt;span style="color:blue;"&gt;="Main Window" &lt;/span&gt;&lt;span style="color:red;"&gt;Height&lt;/span&gt;&lt;span style="color:blue;"&gt;="600" &lt;/span&gt;&lt;span style="color:red;"&gt;Width&lt;/span&gt;&lt;span style="color:blue;"&gt;="800"&amp;gt;&lt;br /&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;DockPanel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;DockPanel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Window&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Adding a ribbon control to the window is a no brainer. Just add a ribbon control and dock it to the &lt;strong&gt;Top&lt;/strong&gt;.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;DockPanel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;rib&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Ribbon &lt;/span&gt;&lt;span style="color:red;"&gt;DockPanel.Dock&lt;/span&gt;&lt;span style="color:blue;"&gt;="Top"&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;rib&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Ribbon&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;DockPanel&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;em&gt;Note : Office UI Design guidelines clearly says that you &lt;strong&gt;SHOULD ALWAYS&lt;/strong&gt; dock the Ribbon to the top of your window.&lt;/em&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;Adding Controls to Ribbon&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;Next thing would be to add a few controls to our ribbon. In this new ribbon, unlike other ribbons in the market, the approach to add controls to the ribbon is more View-Model centric, meaning you always use a RibbonCommand, which is a subclass of RoutedCommnad, to specify a tool's Title, Description, To
