Why You Really Need META-PROGRAMMING COMMANDS CONTRACTS.

Ahmed Fouad
Sep 7 · 2 min read

In the 2 previous articles Boost Your Xamarin App Development With Meta Programming and Async Command A Modern Implementation of ICommand we have introduced the concept of meta programming in xamarin development and how it can enhance your application code maintainability while cutting the development time.

In this article we will dicuss when new feature added recently to the meta-programming library which is the command contracts.

The command contract is an aspect that can advice an ICommand property about when CanExecute should return false and when the CanExecuteChanged event should be fired.

Example

[ViewModel(typeof(MainPageViewModelConfiguration))]                                       public class MainPageViewModel                                   
{
[DefaultValue("")]
public string Name { get; set; }
public string WelcomeMsg => $"Hello {Name}"; public Command<object> DownloadCommand { get; set; } public MainPageViewModel()
{
DownloadCommand=new MetaProgramming.Commands.Command<object>(DownloadData);
}
}

It is kind of similar to the example in the Boost Your Xamarin App Development With Meta Programming article.

Now lets imagine that we want to synchronize the DownloadCommand CanExecute Status with the Connectivity to make it only available when there is an Internet connection.

[Serializable]
public class ConnectivityContract : BaseContract {
public override void SetupSatisfactionChanged()
{
CrossConnectivity.Current.ConnectivityChanged += (s, e) =>
base.RaiseSatisfactionChanged();
}

public override bool IsSatisfied(object parameter)
{
return CrossConnectivity.Current.IsConnected
}
}

It is a very simple class with 2 methods the first one (SetupSatisfactionChanged) is responsible of the association between the domain event (in this case ConnectivityChanged) and the contract satisfaction changed event by calling base.RaiseSatisfactionChanged();

The second method IsSatisfied() simple return a Boolean value if the Contract is satisfied or not.

The final setup is to add the contract on the DownloadCommand in the view model

[ViewModel(typeof(MainPageViewModelConfiguration))]                                       public class MainPageViewModel                                   
{
[DefaultValue("")]
public string Name { get; set; }
public string WelcomeMsg => $"Hello {Name}";[ConnectivityContract]
public Command<object> DownloadCommand { get; set; }
public MainPageViewModel()
{
DownloadCommand=new MetaProgramming.Commands.Command<object>(DownloadData);
}
}

Now you can run your application and you will find that the command status is synchronized with the internet connection , ie the button associated with DownloadCommand will be disabled when there is no connection and will get reenabled when there is a connection.

The xamarin meta programming project is one of the most promising projects in xamarin but it is moving slowly as i am the only contributor , so i appreciate if any one would like to help , please leave a comment on the article or contact me on Github.

you can find more xamarin articles on Feed spot xamarin feed.

Ahmed Fouad

Written by

Hello, I’m Ahmed. I’m a software engineer living in Vienna, Austria. I am a fan of technology, web development, and programming. I’m also interested in xamarin

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade