Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Xamarin

Why You Really Need META-PROGRAMMING COMMANDS CONTRACTS.

5.00/5 (1 vote)
9 Sep 2019CPOL2 min read 2.3K  
In this article we will dicuss when new feature added recently to the meta-programming library which is the command contracts.

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 two 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)