Monday, May 14, 2012

Catel creates a perfect combination with PRISM

Introduction


I have been very "busy" watching the final of the baseball season. The past week, my team was eliminated on quarterfinals, so now I have  time to write.

Talking about competitions, I just remembered the history about how Catel.Extensions.Prism was born.

You may think that Catel and PRISM are competitors. The fact is that most people think that they are competitors. Actually I had the same idea when I discovered Catel, some time after I have been written LoB systems with PRISM. If you read the highlights, is not hard to notice the "isolated" features, mainly if you are looking for guidance or a stating point for the MVVM pattern implementation.

Catel has an out of the box view model base implementation, plus services, with full support for validation, property change notification and commanding. From my point of view Catel beats PRISM as MVVM pattern implementation guidance, with a cleaner approach on separation of views and view models and better testability. The fact is that PRISM has no a ViewModelBase implementation.

So, at some point, I considered removing all the PRISM references from my projects. But then I would loose many PRISM features, that allow you manage the modularity and view composition, that are hard to leave behind when you actually used it. I heard some people talking about the PRISM approach saying that it is a bit complex or strange. They provide example terms like bootstrapper, shell, module catalog, modules, region manager, etc. But the truth is that PRISM provides a good scaffold for modular application development.

That was the moment when we thought that they should be work together and that was the reason for we wrote Catel.Extensions.Prism and it is now available via NuGet.

User Interface Composition with Catel.Extensions.Prism


Starting with Catel 3.1 the Prism extension is available. PRISM includes a feature named "User Interface Composition". Basically it allows building a mosaic like application by loading multiple views that comes from different modules into an active region exposed by a control, also know as the shell.



The PRISM UI composition approach requires that you reference the view or the view type in order to associate it with one region. That is correct if you are coding a module class for instance, but all this is about view models.

In order to understand the code samples you need to read more about UIVisualizerService, and the way it can resolve a view from an instance of a view model. Now you are able to create a composite user interface without actually referencing any view from a view model class.

Making the region manager available

First of all, you must make available the region manager on the instance of ServiceLocator (IoC container of Catel). A PRISM based application uses MEF or Unity as primary IoC container. Therefore, you must synchronize this container with the Catel one, overriding the ConfigureContainer method of the application bootstrapper class, using the following code:

protected override void ConfigureContainer()
{
 base.ConfigureContainer();
 if (ServiceLocator.Instance.IsExternalContainerSupported(this.Container))
 {
  ServiceLocator.Instance.RegisterExternalContainer(this.Container);
 }
}

Activating a view into a specific region

To activate a view into a specific region from inside a view model, use the following code:
 var viewModel = new EmployeeViewModel();
 var uiVisualizerService = GetService<IUIVisualizerService>();
 uiVisualizerService.Activate(viewModel, "MainRegion");

Deactivating a view

To deactivate a view, use the following code:
uiVisualizerService.Deactivate(viewModel);

If you keep your view model alive (see: Keeping view models alive), you can reactivate a deactivated the view using Activate method without specify the region name. 

Deactivating a view automatically

If you close the view model using SaveAndCloseViewModel or CancelAndCloseViewModel or CloseViewModel methods, the view that belongs to this view model will be automatically deactivated from the region where it was activated.

Conclusions   

Nowadays, I enjoy write applications with Catel and PRISM, or with PRISM and Catel, the order doesn't matter. I will just give you one advice, try out this extension and like a host of a popular TV show that I know say, "draw your own conclusions by yourself" ;)

Wednesday, April 25, 2012

Accelerating Catel coding workflow with CatelR#

Overview


The  first release  of ReSharper  (R#)  provoked a  commotion, at  least between my programming  fellowships. Expressions like  "Visual Studio is a great development  tool, but with ReSharper is  awesome" were commonly repeated.

The main reason  of R# success was that the JetBrains team identified a weak point of the Visual Studio coding editor.

Visual Studio, at least in it's firsts versions (.NET versions, 2003, 2005), had no refactoring features,  just like other Java IDEs already had, such as Eclipse or IntelliJ. Also note that  the last two versions of Visual  Studio  improved it's refactoring  features but nothing come close with the power of the R#.

But I will not to talk about R# history, actually the preceding paragraph could be not totally accurate.  I don't  really know how R# was born. (If you know it, don't doubt, just comment it here, allow me to be in the know)

R# as coding workflow guidance


The R#  quick fix  and/or context  action is  very simple  and powerful workflow.  Basically  you can  locate  the  caret  position near  to  an element that  you want to alter, press  Alt + Enter,  and all available suggestions will be displayed. For instance, if you  press Alt + Enter in a  range  corresponding  to  a  "public" modifier  of  a  property,  the suggestions  "To internal", "To  protected" or "To private" would be available, only the options that do not provoke a broken usage are displayed.
 
 {caret}public string FirstName { get; set; }

But all  scenarios are not covered by R#,  there are tons of frameworks and/or toolkit that have it own coding workflows.

Catel coding workflow


If  you are  writing  a WPF  or Silverlight  application  and don't  use Catel, it is  probably that you are not using the right library. See the comparison sheet, for detailed libraries comparison.

But Catel has a "down side". If you want to convert a simple class into a data  or model class in order to  support property changed  notifications and validations, you need re-coding a class like this one:

public class Person
{
 public string FirstName { get; set; }

 public string MiddleName { get; set; }

 public string LastName { get; set; }
}

into this one:

public class Person
{
 public static readonly PropertyData FirstNameProperty = RegisterProperty("FirstName", typeof(string));
 
 public static readonly PropertyData LastNameProperty = RegisterProperty("LastName", typeof(string));
 
 public static readonly PropertyData MiddleNameProperty = RegisterProperty("MiddleName", typeof(string));
 
 public string FirstName
 {
            get { return  this.GetValue<string>(FirstNameProperty); }
            set { this.SetValue(FirstNameProperty, value);  }
 }
 
 public string LastName
 {
            get { return this.GetValue<string>LastNameProperty); }
            set { this.SetValue(LastNameProperty, value); }
 }
 
 public string MiddleName
 {
            get { return this.GetValue<string>(MiddleNameProperty); }
            set { this.SetValue(MiddleNameProperty, value); }
 }
}

As you could be noticed, there is an implementation pattern that should be fully automated. In fact, every Catel version comes with a code snippets package that assist you to write down this class from scratch.

But now, Catel team comes with a new tool. Using the R# SDK, we implement a R# extension that cover the Catel coding workflow and we named CatelR# (obviously).

Now you can do this




You can also do it faster with the R# generation options (Alt + Ins)



The   beta  version,  of CatelR#,  is  released. You can read more about CatelR# and download the beta. We are expecting for your feedback and new features request.

Enjoy it, and have nice coding experience with CatelR#.

Thursday, April 19, 2012

Introduction

I was writing a couple of features of a new tool for Catel developers and users, and Geert advised me to write about this experience.

Then I noticed that I have no place where could do it and just created this blog, applied the simple theme, in the orange fashion, and started to write these lines.

Btw, the orange color is about my favorite baseball team Villa Clara, also known as the explosive oranges.

So, what will you find on this blog in the future?

Actually, I'm a software engineer since 2005 and daily I interacts with friends (or not), sharing (or discussing) about experiences in term of software development practices, including wide range of topics such as:
  • Well coding practices, write code semantically right vs. just "syntactically" right.
  • Continuous integration, building, testing and deploying (devops).
  • Synchronization of version control and issue tracker systems.
  • Multithreading.
  • Automation of common or repetitive development tasks.
  • Version control systems and extreme synchronization scenarios. 
  • IoC, reflection, type descriptors, dynamic proxies, wavers.
and more...

This blog is about the software development. Yes, you heard well, this is just another blog about software development, but with a small difference. Here I will tell you the history just "like was told to me".

Ah, what about the new tool for Catel developers and user?  Expect news about it here or here, it will come soon and would be great ;).

X-ray StoneAssemblies.MassAuth with NDepend

Introduction A long time ago, I wrote this post  Why should you start using NDepend?  which I consider as the best post I have ever...