Released Catel.Fody 1.6.0

It has been a long time since we last released a version of Catel.Fody. In the past few months, we have been working very hard to add new features and combine smart functionality in Catel to allow users to get the best out of Catel. This blog post will describe the most important changes in Catel.Fody 1.6. The reason that the new release took so long is because Catel.Fody is fully refactored under the hood, which allows us to add new features in the future in a modular way.

Weaving properties

Before Catel.Fody 1.6, weaving of properties was pretty basic. It was only possible to automatically weave any auto-property to a Catel property on any class deriving from ModelBase. In the new version, it is possible to tune the weaving features.

Specifying default values

One of the features we added is the weaving of the default values. A default value of a Catel property can now be accomplished by using the DefaultValue attribute. The following code:

public class Person : ModelBase
{
    [DefaultValue("Geert")]
    public string FirstName { get; set; }
}

will be weaved into:

public class Person : ModelBase
{
    public string FirstName
    {
        get { return GetValue<string>(FirstNameProperty); }
        set { SetValue(FirstNameProperty, value); }
    }

    public static readonly PropertyData FirstNameProperty = RegisterProperty("FirstName", typeof(string), "Geert");
}

Ignoring properties or types

To ignore specific properties or types, simply decorate the property or type with the NoWeaving attribute:

[NoWeaving]
public class MyClass : ModelBase
{
    ...
}

Weaving argument checks

Another feature that is introduced in the new version of Catel.Fody is the weaving of argument checks. The following method definition:

public void DoSomething([NotNullOrEmpty] string myString, [NotNull] object myObject)
{
}

will be weaved into:

public void DoSomething(string myString, object myObject)
{
    Argument.IsNotNullOrEmpty(“myString”, myString);
    Argument.IsNotNull(“myObject”, myObject);
}

Exposing properties on view models

One of the features that already existed in Catel before is the ViewModelToModelAttribute. The goal of these attributes is to easily map properties from a model to the view model so as much of the plumbing (setting/getting properties, rechecking validation, etc) is done automatically for the developer. The advantage of moving the Expose functionality to Catel.Fody is that it is now supported on all platforms instead of WPF-only.

To expose properties of a model, use the following code:

[Model]
[Expose("FirstName")]
private Person Person { get; set; }

will be weaved into:

[Model]
private Person Person
{
    get { return GetValue<Person>(PersonProperty); }
    set { SetValue(PersonProperty, value); }
}

public static readonly PropertyData PersonProperty = RegisterProperty("Person", typeof(Person));

[ViewModelToModel("Person")]
public string FirstName
{
    get { return GetValue<string>(FirstNameProperty); }
    set { SetValue(FirstNameProperty, value); }
}

public static readonly PropertyData FirstNameProperty = RegisterProperty("FirstName", typeof(string));