Hello MvbActions

Hi all!
In this post i will extend the Hello Mvb example with the usage of Mvbactions.
You can find MVB framework info on the official Github page and on WIKI.
You can find the code of this example HERE.

What are MvbActions?

MVB expose a component called MvbActions.. a MvbAction is declared as a public property on a ModelBinder and is responsible to notify when a task has been done. So are used for the notification from the ModelBinder to the UI project.

MVB guarantee that the actions on MvbActions will be run on the right thread !

In this example i will use two MvbActions:

  • SayHelloDone
  • OnValidationError

Let's start!

In the ModelBinder declare a public property and initialize in ctor like so:

public MvbActions SayHelloDone;

public HelloWorldModelBinder()
{
    this.SayHelloDone = new MvbActions();
    this.InitBinder();
}

in the SayHelloTo method we will notify when the action is done like so:

private async Task HelloWorker(string name)
        {
            this.OutPut = "MVB wants to greet..";
            await Task.Delay(1000);
            this.OutPut = $"{name.ToUpper()}!!";
            await Task.Delay(1000);
            this.OutPut = "See you soon!";
            this.SayHelloDone.Invoke();
        }

The first MvbAction is done.
As you can see we simply notify when the HelloWorker has done his job.

Now we want a validation on user input to check if the name is valid (the name must not be null or empty). To accomplish this task i use a MvbAction with a Exception parameter.

public MvbActions SayHelloDone;
public MvbActions<Exception> OnValidationError;

public HelloWorldModelBinder()
{
    this.SayHelloDone = new MvbActions();
    this.OnValidationError = new MvbActions<Exception>();
    this.InitBinder();
}

This second MvbAction is closed on Exception object (could be any kind of object).. this object will be the parameter for the involved actions. Let's go invoke this action in the SayHelloTo method:

private async Task HelloWorker(string name)
{
   //Validation
   if (string.IsNullOrEmpty(name))
   {
       this.OnValidationError.Invoke(new Exception("Name cannot be null or empty!"));
       return;
   }
   this.OutPut = "MVB wants to greet..";
   await Task.Delay(1000);
   this.OutPut = $"{name.ToUpper()}!!";
   await Task.Delay(1000);
   this.OutPut = "See you soon!";
   this.SayHelloDone.Invoke();
}

If the parameter is null or empty we'll invoke the OnValidationError MvbActions passing a new Exception object with a custom message.

The ModelBinder section for MvbAction is done!

Now we'll add UI Reaction.

In the InitBinderActions on the UI Project we add those reactions:

this._binder.SayHelloDone.AddAction(() =>
 {
     MessageBox.Show("The SayHello method is done!");
 });

 this._binder.OnValidationError.AddAction(exception =>
 {
     this.OutPut.Text = exception.Message;
 });

Note:
You can add many actions on a MvbActions!

So.. when SayHelloDone is invoked we w'll show a MessageBox.

enter image description here

When OnValidationError is invoked we add the Exception message on output label!

enter image description here

You can do exactly the same thing for WPF,Universal App, Xamarin Android, Xamarin IOS,Xamarin Mac,Windows Forms and console app!!

In the next post i will extend this example with another component: MvbCollections

That's all!

Mark Jack Milian

29-August-2016 @ 22:47

“Any darn fool can make something complex; it takes a genius to make something simple.” ― Pete Seeger

share post :