Catel 3.4 is released!

by Geert 15. December 2012 16:43

It has been quite a while, but today a new version of Catel is released. It took us more time then usual, but in return we have a great version ready to be downloaded via the site or via NuGet.

Added WinRT and Windows Phone 8 support

This is probably the most noticeable “feature” of all. Both WinRT and Windows Phone 8 are fully supported with all features that you are familiar with in Catel. This means that we were able to port everything (not really everything, but 90 %) of it to WinRT and all known Windows Phone features to Windows Phone 8.

Performance improvements

For this release, we focused on performance. We made a lot of improvements by tracing and tweaking all the calls thoroughly. Below is a list of tweaks:

  • All reflection calls are now being cached
  • Logging is only executed when there are actually listeners registered
  • ModelBase.LeanAndMeanModel property to (temporarily) suspend both validation and change notifications, great when restoring models from disk
  • Added a FastObservableCollection which allows the adding of a range and only invokes a single event at the end
  • It is now possible to control (globally or per control) whether InfoBarMessageControl and WarningAndErrorValidator controls must be created or MVVM controls
  • Constructors in the ServiceLocator are cached which gives a major performance boost when creating new instances of types with dependency injection

Improved IoC container

The IoC container in Catel, the ServiceLocator, now supports registration of types with a tag. This allows the same type to be registered multiple times.

And much more…

Besides new features, we also fixed over 40 issues in this new release. Not all new features are described in this blog post, so make sure you get the latest version while it’s hot!

Tags:

MVVM | C# | Catel | Windows Phone 7 | Windows Phone 8 | WinRT | WPF | Silverlight

Showing a busy indicator per view in Silverlight using MVVM

by Geert 12. October 2012 10:03

Introduction

When using Silverlight, everything is asynchronous. It seems to be the trend, the same goes for Windows 8. This means that you will need to inform the user about progress in the background.

Silverlight uses the BusyIndicator for this behavior. Using MVVM, it might be a bit harder to implement the BusyIndicator in a correct way, but Catel provides the IPleaseWaitService for years which can be mocked easily during test scenarios.

However, it didn’t support a busy indicator per view yet. This fact has changed today, as you can see in the screenshot below:

image

A long requested feature in Catel was the support for tags in the ServiceLocator. The ServiceLocator is the IoC solution that Catel provides by default. A customer of Catel recently required the busy indicators to show up per view. I thought a bit about it and this could be solved by the recently (read: this week) introduction of the tags in the ServiceLocator.

Setting up the views

The view is responsible for registering the service. This can easily be done using the Catel user controls. Create a view like you always do, then use the following code-behind:

   1:  protected override void OnViewModelChanged()
   2:  {
   3:      var serviceLocator = ServiceLocator.Default;
   4:   
   5:      var viewModel = ViewModel;
   6:      if (viewModel != null)
   7:      {
   8:          serviceLocator.RegisterInstance(typeof (IPleaseWaitService), new PleaseWaitService(this), viewModel);
   9:      }
  10:  }

This code will be executed when the ViewModel property of the control changes. The view registers a view specific instance of the PleaseWaitService service in the ServiceLocator. It uses the new view model as tag so that will be used to distinguish the services.

Setting up the view models

The view model can retrieve the PleaseWaitService very easily because the tag is itself. To show the view specific please wait service, use this code:

   1:  var pleaseWaitService = GetService<IPleaseWaitService>(this);
   2:  pleaseWaitService.Show();

To hide the window again, use this code:

   1:  var pleaseWaitService = GetService<IPleaseWaitService>(this);
   2:  pleaseWaitService.Hide();

 

Customizing the PleaseWaitService

Customizing the please wait service is very, very easy. Just override the class like this:

   1:  public class MyCustomPleaseWaitService : PleaseWaitService
   2:  {
   3:      protected override FrameworkElement CreateBusyIndicator()
   4:      {
   5:          var busyIndicator = new MyBusyIndicatorControl();
   6:          
   7:          busyIndicator.SetBinding(System.Windows.Controls.BusyIndicator.BusyContentProperty, new Binding("Status"));
   8:          busyIndicator.SetBinding(System.Windows.Controls.BusyIndicator.IsBusyProperty, new Binding("IsBusy"));
   9:          
  10:          return busyIndicator();
  11:      }
  12:  }

The base implementation will automatically take care that the data context is updated and that the control is centered as required.

Demo and source

Last but not least, here is a demo and the source.

PleaseWaitServicePerView.zip (21.24 kb) [Downloads: 459]

Tags:

C# | Catel | MVVM | Silverlight

Catel 3.2 is released!

by Geert 8. July 2012 11:32

The Catel team is very proud the announce the new 3.2 release. As always, the new version is available via NuGet and via Codeplex (http://catel.codeplex.com).

In this blog post, I will try to explain the most important new changes in Catel.

Dependency injection support in the ServiceLocator

The ServiceLocator in Catel now fully supports dependency injection. This means that when a type is being resolved from the ServiceLocator.

Dependency injection via the ServiceLocator in Catel is enabled by default. This means that when a type is resolved from the container, it will automatically use dependency injection to construct the type if it is not registered as instance.

It will first search for all available constructors on the type that will be instantiated. Then, for each constructor, starting with the one with the most parameters, it will try to retrieve all values. If one fails, it will go to the next. If all fail, it will try to use the default constructor without parameters. If that fails as well, then the type cannot be constructed and an exception will be thrown.

To get a better understanding of what happens, see the class below:

public class MyClass
{
  private IFirstDependency _firstDependency;
  private ISecondDependency _secondDependency;
 
  public MyClass()
     : this(null) { }
 
  public MyClass(IFirstDependency firstDependency)
     : this(firstDependency, null) { }
 
  public MyClass(IFirstDependency firstDependency, ISecondDependency secondDepdenceny)
  {
    _firstDependency = firstDependency;
    _secondDependency = secondDependency;
  }
}

When the MyClass will be retrieved from the ServiceLocator, this will happen:

  1. Find constructor with most parameters (the one with both firstDependency and secondDependency). If both IFirstDependency and ISecondDependency can be resolved from the ServiceLocator, the type will be constructed with the constructor. Otherwise it will proceed with step 2.
  2. Find next constructor with most parameters (the one with only firstDependency). If IFirstDependency can be resolved from the ServiceLocator, the type will be constructed with the constructor. Otherwise it will proceed with step 3.
  3. At this point, no constructor could be used. In this case, the ServiceLocator will try to use the default constructor (the one without parameters) as last resort to instantiate the type.

Introducing the ViewModelFactory

In previous versions of Catel, it was only possible to inject a datacontext into a view model. In Catel 3.2, the new ViewModelFactory is introduced. This class is responsible for instantiating the view model.

By default, the implementation does exactly the same as before, but this gives great freedom when the view models cannot be instantiated by Catel itself (for example, when dependency injection is used, or when the registered view models of a control are interfaces).

Below is an example of a custom implementation of a view model factory:

public class CustomViewModelFactory : ViewModelFactory
{
  public override IViewModel CreateViewModel(Type viewModelType, object dataContext)
  {
      if (viewModelType == typeof(MySpecialViewModel))
      {
          // Use custom constructor with dependency injection
          return new MySpecialViewModel(dep1, dep2, dep3, dataContext);
      }
 
      // Fall back to default behavior
      return base.CreateViewModel(viewModelType, dataContext);
  }
}

All reflection is cached

In preparation for WinRT support, we moved all reflection to a single class with extension methods. This was a great opportunity to add caching to all reflection.

This means that all reflection is cached on the first call. This provides major performance improvements when using reflection.

More flexible behavior base classes

Catel already provided memory leak free base classes for behaviors and triggers. In the new version, a few new base classes are introduced to allow faster behavior developmentP:

  • CommandBehaviorBase – supports a command, command parameter and key modifiers out of the box
  • UpdateBindingBehaviorBase – supports binding updates on dynamic dependency properties

Core services now only use interfaces for MVVM integration

The core services (such as the MVVM behaviors) in Catel now use interfaces such as INotifyableViewModel and IRelationViewmodel. This makes it possible to use different base classes for view models and still use the full potential of Catel.

Updated to latest external libraries

We also took the time to update all external libraries to the latest version. The following libraries were updated:

  • Ninject (3.0)
  • Castle.Windsor (3.0)
  • FluentValidation (3.3.1)

Tags: ,

C# | Catel | MVVM | WPF | Windows Phone 7 | Silverlight

Why there won’t be a WinRT version of Catel…

by Geert 6. May 2012 14:00

So, lots of people came to me asking whether there will be a WinRT version of Catel. First, I said we were seriously considering it. But lately the idea of not supporting it became more and more the idea I wanted to follow.

A little history

Instead of going with the flow, let’s first take a look at the history of Catel and why it has been created anyway. Mid 2010, I needed a (WPF) MVVM framework that would contain the following features:

  • Support IDataErrorInfo for the view model so it could contain validation
  • Nested user controls (because I couldn’t create big trees of hierarchy view models)
  • Ease of development

With the latest I mean I didn’t want to go with the hype where everything needed to be “light”, simply because I always found myself writing extension methods and improving code. After I seriously considered Cinch, the moment I decided to write my own was when there was a “view model generator tool”.

When I started with MVVM and Xaml, all I did was WPF, which is simply the best implementation of Xaml for me. But, then people started asking: can you support Silverlight as well? Then the hell of missing features began. Lots of interfaces that Catel uses to allow property undo-ing such as INotifyPropertyChanging were not supported. It took me about 3 weeks to make sure everything in Catel also worked in Silverlight. Most of the time, I simply wrote the WPF features that were missing from Silverlight to allow the same functionality of Catel in Silverlight.

When WP7 came out, I was already prepared for the upcoming hell, but it wasn’t as bad as the transition from WPF => Silverlight. However, it again took me at least 2 weeks to create all the project and item templates and the Windows Phone specific implementations of the navigation services and tombstoning.

When the windows 8 beta came out, I quickly installed it to check out what has changed so I could convert Catel. But… it seems there are more, much more changes than I expected. For example, the whole reflection system has been changed.

Also, Catel supports a WeakEventListener and WeakActions for the MessageMediator, but this is simply not possible in WinRT because important reflection (although public) is not available.

What’s WinRT exactly?

After fighting with WinRT for about 4 weeks, I decided to take a look at the the current state and how much work there was in front of me. Then I realized I already left out the most important features (because I knew they wouldn’t compile anyway out of the box), but I couldn’t even get the Core libraries to compile (such as the WeakEventListener).

Then I thought: is Catel the way to go for WinRT anyway? And the answer is no. I see WinRT applications as a windows phone application without any specific LoB requirements. For example, checking the weather, checking facebook, etc. If you need a real LoB application, I still recommend to go for WPF or Silverlight which is the way to go.

Taking a look at the history of Catel, it was originally founded as a LoB framework, not as a framework to fit all purposes such as very light applications. I have decided to stick with the initial goal (targeting LoB applications) and the pain is just not worth the gain.

Is it definitely a no go?

For now: yes. If I am honest, apps written in WinRT are so light that a full-featured framework such as Catel is an overkill. You can better choose a light framework such as MVVM light which basically only supports property change notifications, a messenger and event to command.

I did write lots of code to allow reflection on both WinRT and not WinRT platforms in Catel , but it is not finished. Maybe, if Microsoft makes some changes so we can actually get, for example, the MethodInfo of a handler, we will re-evaluate the decision.

Tags: , ,

Catel | MVVM | WinRT

Catel 3.1 released

by Geert 6. May 2012 13:49

We just released Catel 3.1. You can grab the latest version:

In this blog post, we already described the most important changes, but you can find a full list of changes here.

As always, if you have any questions or feature requests, don’t hesitate to contact us or create an issue!

Tags: ,

C# | Catel | MVVM | Silverlight | Windows Phone 7 | WPF


About the Author

Geert van Horrik is an independent freelance software developer since January 1st, 2007. Since then he was been working on several projects from C++ to C# (WPF, Silverlight, ASP.NET, etc). Currently he loves to write his software using WPF (or Silverlight if WPF isn't an option).

Lately, Geert is spending a lot of time on Catel, a free open-source MVVM Framework for WPF and Silverlight. Actually, it's more than "just" an MVVM Framework, it's a complete application library!