Wednesday, January 30, 2013

Creating an Attribute in C#

In C#, attribute is one of the common declarative technique that most of us is using into our application. Usually, we use the attribute to tag or flag a class, method or property in the form of object base recognition. It also simplify our determination in every object.

Furthermore, in C# there are lots of pre-defined attribute that we can embed into our application. One common sample is SerializableAttribute which flag/tag our class to participate in serialization/deserialization process. If however you would like to extend the attribute functionality and define your own, you can extend/inherit the attribute class from System namespace.

Please visit C# Flags Attribute blog before you continue. The reason is to let you explore the enumeration named Privilege.

Now, we are expecting that you already explore the Privilege enumeration form mentioned  blog above.

Let say for example you would like to define a scenario that gives your code-level to determine whether a class (or a stub object) can be deleted from database. See below our sample custom attribute.

public class SecurityAccess : Attribute
{
    public SecurityAccess()
    {
    }

    public SecurityAccess(Privilege privilege)
        : this()
    {
        this.Privilege = privilege;
    }

    public Privilege Privilege
    {
        get;
        set;
    }
}

What the class SecurityAccess is doing above is just simply handling the value for the Privilege enumeration. This property defines whether the class that declare this attribute has an enough level of privilege before doing necessary action in the database.

How to use custom attribute?

Now, in this section, we will going to guide you how to use the attribute we created above.

Suppose we have two class that inherits from one object. Let's call them Animal, and the two other class in Cat and Dog. See below implementation.

public class Animal
{
    public bool IsDeletable
    {
        get;
        protected set;
    }
}

public class Cat : Animal
{
    public Cat()
    {
    }
}

public class Dog : Animal
{
    public Dog()
    {
    }
}

The Dog and Cat is an Animal base on our implementation. With the use of our custom attribute named SecurityPrivilege, we can declare each privilege in every class. Suppose we would like different breed of Cat cannot be deleted from our database, then we can declare our custom attribute like below.

[SecurityAccess(Privilege = Privilege.Read | Privilege.Write | Privilege.Create)]
public class Cat : Animal
{
    public Cat()
    {

    }
}

And the Dog breed is deletable. See below.

[SecurityAccess(Privilege = Privilege.Read | Privilege.Write | Privilege.Create | Privilege.Delete)]
public class Dog : Animal
{
    public Dog()
    {

    }
}

How to access an attribute value declared in the class/method and other object?

Now, in this section, we will going to guide you how to access the value of the attribute per class level.

In C#, the only way to get the attribute value is to use Reflection. First, we need to get the current type of the object, from there check if there are declared custom attributes and determine the attribute type. If its match the attribute we're looking for, then that is the custom attribute we had created. See below the process.

protected T GetCustomAttribute<T>(object @object)
{
    if (!object.ReferenceEquals(@object, null))
    {
        var type = @object.GetType();
        var attrs = type.GetCustomAttributes(false);
        if (attrs.Length > 0)
        {
            foreach (var attr in attrs)
            {
                if (attr is T)
                {
                    return (T)(object)attr;
                }
            }
        }
    }
    return default(T);
}

What the code is doing above is to simply return the embedded attribute in the object you passed in the parameter named @object. If there is no attribute found, then it will return null. Best to place this code in the Animal base class so both derive class can use it.

And now, in the construction of Dog and Cat class, you should call the method directly from there.

public Dog()
{
    var attr = this.GetCustomAttribute<SecurityAccess>(this);
    base.IsDeletable = (attr.Privilege & Privilege.Delete) == Privilege.Delete;
}

public Cat()
{
    var attr = this.GetCustomAttribute<SecurityAccess>(this);
    base.IsDeletable = (attr.Privilege & Privilege.Delete) == Privilege.Delete;
}

Now, in the base class IsDeletable property, we then set it depends on the privilege level we declared on the class.

Please note that we can override the attribute value declared in the base class into derive class. So if you want that the Siamese Cat breed be deletable, you can declare your own SecurityAccess attribute in Siamese class.

That's all about this blog. Please follow us so you will get more interesting topics soon.

Please visit Microsoft documentation for further details.

Sunday, January 27, 2013

WPF Binding to a Static Instance

This tutorial will guide you how to bind in a static property of a class in a two directional way in WPF.

Two way Binding in a Static Property

In order for our class StaticBinder to participates in the two directional binding, we should implement the IPropertyNotifyChanged event. In our case, we are requiring you to read our pre-requisite blog so you will fully understand the implementation.

List of pre-requisite blogs.

And here we are expecting that you're finish reading the pre-requisite blogs above.

Now let's start with our explanation.

We need to modify our existing implementation of StaticBinder class and force inherit from the BaseObject class. With this, the StaticBinder will also inherit the implementation of the super class PropertyNotifier that implements the IPropertyNotifier interface.

In each property of the StaticBinder class, we should use the BaseObject GetValue/SetValue accessor so it will notify the listener on every property value changed. Please note that you need not to implement each method in a static way. In our case, you must remove the static keyword in each property.

Now, last thing to do within StaticBinder class is to declare an additional static property named Instance. This approach is a single-ton approach for the StaticBinder class. This new property named Instance will participate in WPF binding. See below our new class implementation.

public class StaticBinder : BaseObject
{
    private static StaticBinder __instance = null;

    static StaticBinder()
    {
        __instance = new StaticBinder();
    }

    public StaticBinder()
    {
        MSG_Cancel = "Welcome";
        MSG_OK = "OK";
        MSG_Welcome = string.Format("Welcome {0}!", "WPF Binding");
    }

    public string MSG_Cancel
    {
        get { return base.GetValue<string>("MSG_Cancel"); }
        set { base.SetValue("MSG_Cancel", value); }
    }

    public string MSG_OK
    {
        get { return base.GetValue<string>("MSG_OK"); }
        set { base.SetValue("MSG_OK", value); }
    }

    public string MSG_Welcome
    {
        get { return base.GetValue<string>("MSG_Welcome"); }
        set { base.SetValue("MSG_Welcome", value); }
    }

    public static StaticBinder Instance
    {
        get { return __instance; }
    }
}

Binding in XAML

Now, if we are binding a static property in XAML, we need to use the namespace referencing and direct call the static property of that instance. Additionally, you can bind directly to the property of that shared instance which is in our case it is the StaticBinder class.

With the help of the Binding (x:Static) keyword, the trick will be addressed. See below our codes in the XAML in yellow background.

<Window x:Class="CodesDirectory.WIN_StaticBinding"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WIN Static Binding" Height="300" Width="300"
        xmlns:classes="clr-namespace:CodesDirectory.Classes"
        WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Label Grid.Row="0"
               Content="{Binding Source={x:Static classes:StaticBinder.Instance}, Path=MSG_Welcome}"></Label>
        <StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal">
            <Button Name="okayButton"
                    Click="okayButton_Click" Content="{Binding Source={x:Static classes:StaticBinder.Instance}, Path=MSG_OK}"
                    Width="100"
                    Margin="5"></Button>
            <Button Name="cancelButton"
                    Content="{Binding Source={x:Static classes:StaticBinder.Instance}, Path=MSG_Cancel}"
                    Width="100"
                    Margin="5"></Button>
        </StackPanel>
    </Grid>
</Window>

For you to check the actual two-dimensional binding, please follow the code behind implementation below.

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

    private void okayButton_Click(object sender, RoutedEventArgs e)
    {
        Classes.StaticBinder.Instance.MSG_OK = "Okay";
    }
}

You will notice that upon click of the OK button, the changes will then reflect to the UI.

And that's all about it. You just finished reading this blog.

WPF Binding to a Static Property

In WPF, the binding provides an efficient and easy way of data interaction between components. Every FrameworkElement can be bound to any data sources in the form of XAML and CLR.

The binding technique is one of the best way to simplify the implementations in WPF. But you should understand that every class in .NET only participates in two way binding if you have implemented the IPropertyChanged interface or you used DependencyProperty object.

Binding to a Static Property

Binding to static property of the class is slightly complex than normal binding. If you have a class with static property, you atleast need to address the requirement whether the binding is one or two dimensional. Please note that static property is not an instance type property and you should do your own trick to notify the changes in the XAML.

First of all, we should create a class (note: don't make it static). Then, place all static property you wish to put in this class. In our case, we will name the class StaticBinder and will put three property named MSG_OK, MSG_Cancel and MSG_Welcome. See below the implementation.

public class StaticBinder
{
    static StaticBinder()
    {
        MSG_Cancel = "Welcome";
        MSG_OK = "OK";
        MSG_Welcome = string.Format("Welcome {0}!", "WPF Binding");
    }

    public static string MSG_Cancel
    {
        get;
        set;
    }

    public static string MSG_OK
    {
        get;
        set;
    }

    public static string MSG_Welcome
    {
        get;
        set;
    }
}

You will notice that we created one static constructor. In there, we place all the property initialization and call each of them statically. Now, in the XAML, we need to create a Window with Label and Button controls embedded that can be bound on the class above. See below our implementation.

<Window x:Class="CodesDirectory.WIN_StaticBinding"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WIN Static Binding" Height="300" Width="300"
        WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Label Grid.Row="0"></Label>
        <StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal">
            <Button Name="okayButton"
                    Click="okayButton_Click"
                    Width="100"
                    Margin="5"></Button>
            <Button Name="cancelButton"
                    Width="100"
                    Margin="5"></Button>
        </StackPanel>
    </Grid>
</Window>

Now before you can participate your Window in the binding, you should create a reference to the class we created above via XAML. You should put an xmlns (XML Namespace) that directs to the namespace where the StaticBinder class resides, and you should create a new instance of StaticBinder class and put it in the Window resources. See below the additional codes in yellow background.

<Window x:Class="CodesDirectory.WIN_StaticBinding"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WIN Static Binding" Height="300" Width="300"
        xmlns:classes="clr-namespace:CodesDirectory.Classes"
        WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
    <Window.Resources>
        <ObjectDataProvider x:Key="staticBinder"
                            ObjectType="{x:Type classes:StaticBinder}"></ObjectDataProvider>
    </Window.Resources>
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Label Grid.Row="0"></Label>
        <StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal">
            <Button Name="okayButton"
                    Click="okayButton_Click"
                    Width="100"
                    Margin="5"></Button>
            <Button Name="cancelButton"
                    Width="100"
                    Margin="5"></Button>
        </StackPanel>
    </Grid>
</Window>

With the XAML codes above, you will notice that our namespace name is 'classes'. You can change it in any name if you wish. Also, we created a new object named ObjectDataProvider with key value staticBinder. The ObjectDataProvider is an object that provides us a dynamic resource data binder in the XAML. It can also be used to instantiate and call the class methods in your application.

Let's continue to the actual binding. Now in the Content property of the Button and Label, use the StaticResource that connects the ObjectDataProvider and set the Path property equal to the actual Property of the StaticBinder class. See below the codes in yellow background.

<Window x:Class="CodesDirectory.WIN_StaticBinding"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WIN Static Binding" Height="300" Width="300"
        xmlns:classes="clr-namespace:CodesDirectory.Classes"
        WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
    <Window.Resources>
        <ObjectDataProvider x:Key="staticBinder"
                            ObjectType="{x:Type classes:StaticBinder}"></ObjectDataProvider>
    </Window.Resources>
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Label Grid.Row="0"
               Content="{Binding Source={StaticResource ResourceKey=staticBinder}, Path=MSG_Welcome}"></Label>
        <StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal">
            <Button Name="okayButton"
                    Click="okayButton_Click"
                    Content="{Binding Source={StaticResource ResourceKey=staticBinder}, Path=MSG_OK}"
                    Width="100"
                    Margin="5"></Button>
            <Button Name="cancelButton"
                    Content="{Binding Source={StaticResource ResourceKey=staticBinder}, Path=MSG_Cancel}"
                    Width="100"
                    Margin="5"></Button>
        </StackPanel>
    </Grid>
</Window>

Please note that the implementation above doesn't participates in the two directional binding. Any changes happens on the StaticBinder class property will not reflect in the UI. Please continue read the blog WPF Binding to Static Instance if you wish to understand how to implement two directional binding.