Mgen spa Engineering 2 tutorial (Part 1): Custom validators and data streams

Source: Internet
Author: User

Back to the tutorial directory

Directory

  • 1. Custom validators
  • 2. Custom Data Stream

 

Returned directory
1. Custom validators

The validators in the spa project must execute the ivalidator interface, which is in the mgen. spa. in the data namespace, this namespace should be the first time mentioned. The types in this namespace are the underlying Type Definitions of the SPA project. As the tutorial goes to the following sections, more types will be introduced here.

Let's take a look at the definition of the ivalidator interface. This interface looks very simple:

Public interface ivalidator

{

Type targettype {Get ;}

 

String validate (Object OBJ );

}

 

The targettype attribute is the property type of the validator. The SPA project uses targettype to determine whether the definition type is correct. If the type does not match, spafatalexception is thrown.

The validate method is the primary method for verification. The OBJ parameter is an object of the targettype to be verified. If no verification error is returned, null is returned. If any, an error message is returned.

 

For example, to define a user-defined validators for the int type, if the int object is not an even number, an error message is returned:

// + Using mgen. spa. Data;

Public class myspavalidator: ivalidator

{

Public type targettype

{

Get {return typeof (INT );}

}

 

Public String validate (Object OBJ)

{

If (INT) OBJ) % 2 = 0)

Return NULL;

Return "enter an even number ";

}

}

 

We know that the SPA project's validators are defined to pass. therefore, we also need to define a feature type. All spa Engineering Feature types are inherited from mgen. spa. the basic feature type in the validators namespace: spavalidatorattribute.

This type is also very simple:

Public abstract class spavalidatorattribute: spaattribute

{

Public abstract ivalidator getvalidator ();

}

You only need to inherit this type, and then implement the getvalidator method and finally return your own validators for execution.

 

Code (note that the type name should follow the method of the SPA project validator ):

// + Using mgen. spa. validators

Public class myspavalidatorruleattribute: spavalidatorattribute

{

Public override ivalidator getvalidator ()

{

// Return the myspavalidator type defined above

Return new myspavalidator ();

}

}

 

Finally, we can define a type to use our own validators:

Class myspatype

{

[Spaheader ("key1")]

[Textboxstringcontrol] // Control

[Stringtointflow] // data stream (convert string to int)

[Myspavalidatorrule] // our custom validators

Public int number {Get; set ;}

}

 

Running result:

 

Returned directory
2. Custom Data Stream

User-Defined data streams are similar to the above User-Defined validators. Here we will talk about the type directly.

 

The Data Stream interface is the idataflow interface in the mgen. spa. Data namespace:

Public interface idataflow

{

Type coretype {Get ;}

Type controltype {Get ;}

 

Object convert (Object OBJ );

Object flow (Object OBJ );

}

 

Coretype indicates the core type and controltype indicates the control type.

The convert method converts the control data to the core data, while the flow method converts the core data to the control data. If there are any errors during conversion, you can directly throw an exception. The SPA project outputs the exception. This is different from the custom validators. The custom validators directly return the error string.

 

For example, we define an interesting data stream and convert the int data of the control to a string object. The conversion rule is as follows: Take the absolute value of int and return the continuous A of the number of Int. For example, if int is 3, "AAA" is returned ". For the string-to-int rotation, the string length can be directly returned. Code:

// + Using mgen. spa. Data;

Public class myspadataflow: idataflow

{

Public type coretype

{Get {return typeof (string );}}

 

Public type controltype

{Get {return typeof (INT );}}

 

Public object convert (Object OBJ)

{

VaR c = math. Abs (INT) OBJ );

Return new string ('A', C );

}

 

Public object flow (Object OBJ)

{

Return (string) OBJ). length;

}

}

 

Then define the features. The data flow features of all SPA projects inherit the basic spadataflowattribute feature types in the mgen. spa. dataflow namespace:

Public abstract class spadataflowattribute: spaattribute

{

Public abstract idataflow getdataflow ();

}

The inheritance type is simple, and then the rewrite method returns the data stream type for execution. The Code is as follows:

// + Using mgen. spa. dataflow

Public class myinttostringflowattribute: spadataflowattribute

{

Public override idataflow getdataflow ()

{

Return new myspadataflow ();

}

}

 

Define a type for testing:

Class myspatype

{

String _ text;

 

[Spaheader ("key1")]

[Sliderintcontrol (1, 10)] // Control

[Myinttostringflow] // custom data stream

Public String text

{

Get {return _ text ;}

Set

{

_ Text = value;

MessageBox. Show (value );

}

}

 

Public myspatype ()

{

_ Text = "aaaa ";

}

}

 

Note that the text attribute has been set to "AAA" in the above Type constructor ". At the same time, MessageBox will output its value whenever the text is changed. Finally, you need to use objectoption. Edit to generate the interface, so that the control will read the core value.

 

Let's take a look at the running status of the SPA project. After the interface is displayed, the data stream automatically converts the core value to the control, so that the slider control displays 4:

If you slide the slider, MessageBox will output the corresponding number of.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.