Skip to content

Create a ModelBinder

Marco Milani edited this page Nov 20, 2016 · 6 revisions

#ModelBinder Creation API

###Create a ModelBinder

The ModelBinder component should extend the type Mvb.Core.Base.MvbBase and as the last statement inside the constructor must call base.InitBinder ( ) ;

public class HelloWorldModelBinder: MvbBase 
{
	public HelloWorldModelBinder()
	{
		//your custom init
		base.InitBinder();
	}
}

###Create Properties

Public properties should be defined as when you implement INotifyPropertyChanged .

public class HelloWorldModelBinder: MvbBase 
	{
		public HelloWorldModelBinder()
			{
				//your custom init
				base.InitBinder();
			}
		
		private bool _isBusy;			
		public bool IsBusy
		   {
		       get { return this._isBusy; }
		       set { this.SetProperty(ref this._isBusy, value); }
		   }
	}

###Add Collection

Public collections should be of type Mvb.Core.Components.MvbCollection .

public class HelloWorldModelBinder: MvbBase 
	{
		public HelloWorldModelBinder()
			{
				//your custom init
				this.Names= new MvbCollection<string>();
				base.InitBinder();
			}
		
		private bool _isBusy;			
		public bool IsBusy
		   {
		       get { return this._isBusy; }
		       set { this.SetProperty(ref this._isBusy, value); }
		   }
		   
		private MvbCollection<string> _names;			
		public MvbCollection<string> Names
		{
			get { return this._contacts; }
			set { this.SetProperty(ref this._contacts, value); }
		}
	}

###Input

The input methods are mostly void and should be fire & forget .

public class HelloWorldModelBinder: MvbBase 
	{
		public HelloWorldModelBinder()
			{
				//your custom init
				this.Names= new MvbCollection<string>();
				base.InitBinder();
			}
		
		private bool _isBusy;			
		public bool IsBusy
		   {
		       get { return this._isBusy; }
		       set { this.SetProperty(ref this._isBusy, value); }
		   }
		
		private string _outPut;			
		public string OutPut
		   {
		       get { return this._outPut; }
		       set { this.SetProperty(ref this._outPut, value); }
		   }
		   
		private MvbCollection<string> _names;			
		public MvbCollection<string> Names
		{
			get { return this._contacts; }
			set { this.SetProperty(ref this._contacts, value); }
		}


		public void SayHelloTo(string foo)
		{
			Task.Run(()=>
			{
				this.IsBusy  =true;
				await Task.Delay(1500);
				this.OutPut = $"Hi {foo}!!!";
				this.IsBusy  =false;
			}).ConfigureAwait(false);
		}
	}

###Interact between ModelBinder

A ModelBinder can communicate with others only through the helper Publisher / Subscriber MvbMessenger.

public class HelloWorldModelBinder: MvbBase 
	{
		public HelloWorldModelBinder()
			{
				//your custom init
				this.Names= new MvbCollection<string>();
				base.InitBinder();
			}
		
		private bool _isBusy;			
		public bool IsBusy
		   {
		       get { return this._isBusy; }
		       set { this.SetProperty(ref this._isBusy, value); }
		   }
		
		private string _outPut;			
		public string OutPut
		   {
		       get { return this._outPut; }
		       set { this.SetProperty(ref this._outPut, value); }
		   }
		   
		private MvbCollection<string> _names;			
		public MvbCollection<string> Names
		{
			get { return this._contacts; }
			set { this.SetProperty(ref this._contacts, value); }
		}


		public void SayHelloTo(string foo)
		{
			Task.Run(()=>
			{
				this.IsBusy  =true;
				await Task.Delay(1500);
				this.OutPut = $"Hi {foo}!!!";
				//Notify i do my work to other ModelBinders
				base.Send(this, "sayHelloDone", this.OutPut);
				this.IsBusy  =false;
			}).ConfigureAwait(false);
		}
	}

###Expose MvbAction to top layers

To notify their events/errors a ModelBinder can use the object MvbAction .

public class HelloWorldModelBinder: MvbBase 
	{
		public MvbActions OnJobDone;
		public MvbActions<Exception> OnModelBinderError;
		
		public HelloWorldModelBinder()
			{
				//your custom init
				this.OnJobDone= new MvbActions<bool>();
				this.OnModelBinderError= new MvbActions<Exception>();
				this.Names= new MvbCollection<string>();
				base.InitBinder();
			}
		
		private bool _isBusy;			
		public bool IsBusy
		   {
		       get { return this._isBusy; }
		       set { this.SetProperty(ref this._isBusy, value); }
		   }
		
		private string _outPut;			
		public string OutPut
		   {
		       get { return this._outPut; }
		       set { this.SetProperty(ref this._outPut, value); }
		   }
		   
		private MvbCollection<string> _names;			
		public MvbCollection<string> Names
		{
			get { return this._contacts; }
			set { this.SetProperty(ref this._contacts, value); }
		}


		public void SayHelloTo(string foo)
		{
			Task.Run(()=>
			{
				this.IsBusy  =true;
				try
				{
					await Task.Delay(1500);
					this.OutPut = $"Hi {foo}!!!";
					//Notify i do my work to other ModelBinders
					base.Send(this, "sayHelloDone", this.OutPut);
					//Notify for topLayers
					this.OnJobDone.Invoke();
					this.IsBusy  =false;
				}
				catch (Exception ex)
				{
					MvbMessenger.Send(this,"sayHelloError");
					this.OnModelBinderError(ex);
				}
				finally
				{
					this.IsBusy = false;
				}
				
			}).ConfigureAwait(false);
		}
	}

###Suggestion

This layer is a perfect place for Dependency Injection!

public class HelloWorldModelBinder: MvbBase 
	{
		private readonly IHelloService _helloService;
		public MvbActions OnJobDone;
		public MvbActions<Exception> OnModelBinderError;
		
		public HelloWorldModelBinder(IHelloService helloService)
			{
				//your custom init
				this._helloService = helloService;
				this.OnJobDone= new MvbActions<bool>();
				this.OnModelBinderError= new MvbActions<Exception>();
				this.Names= new MvbCollection<string>();
				base.InitBinder();
			}
		
		private bool _isBusy;			
		public bool IsBusy
		   {
		       get { return this._isBusy; }
		       set { this.SetProperty(ref this._isBusy, value); }
		   }
		
		private string _outPut;			
		public string OutPut
		   {
		       get { return this._outPut; }
		       set { this.SetProperty(ref this._outPut, value); }
		   }
		   
		private MvbCollection<string> _names;			
		public MvbCollection<string> Names
		{
			get { return this._contacts; }
			set { this.SetProperty(ref this._contacts, value); }
		}


		public void SayHelloTo(string foo)
		{
			Task.Run(()=>
			{
				this.IsBusy  =true;
				try
				{
					await Task.Delay(1500);
					this.OutPut = this._helloService.CreateText(foo);
					//Notify i do my work to other ModelBinders
					base.Send(this, "sayHelloDone", this.OutPut);
					//Notify for topLayers
					this.OnJobDone.Invoke();
					this.IsBusy  =false;
				}
				catch (Exception ex)
				{
					MvbMessenger.Send(this,"sayHelloError");
					this.OnModelBinderError(ex);
				}
				finally
				{
					this.IsBusy = false;
				}
				
			}).ConfigureAwait(false);
		}
	}

Clone this wiki locally