How to create a new Control
Suppose you need to implement the following user story:
As a user, I want to create a
CustomLabel
that not only has a propertyText
of the Label but also aAnotherText
property that stores another text. When someone modifies the value ofAnotherText
, it should trigger an event indicating that this property has been changed.
In order to implement this user story, you will have to do the following things:
An Observable Class
A Data Transfer Object
A Mapper
Register the mapper in the RegistrationClass
1. Creating CustomLabel
Observable Class
CustomLabel
Observable ClassPlease follow the following steps:
Create the class
Add
ObservableAttribute
Add the required properties and add
InterceptedAttribute
to the properties that you want them to be persisted throughout the WebMAP's life cycle.
Observations when adding ObservableAttribute
ObservableAttribute
When you add this attribute, it means the the class will be an observable class. So please take into account the following observations:
An observable class can have
private
,internal
orpublic
modifier.An observable class can be
sealed
.An observable class can be
abstract
.When you add
Observable
attribute to a class. All classes that inherits from that class will be observables. Even if attribute is not present in one of those classes.
Observations when adding InterceptedAttribute
InterceptedAttribute
When you add this attribute, it means the the property will be intercepted, this means that this property will live under the WebMAP's life cycle, its value will be persisted throughout the requests. So please take into account the following observations:
Properties can be virtual or non virtual.
Properties can be
public
,private
,internal
orprotected
.Properties must be auto implemented
Property type can be any value type or string, or any type marked as an observable.
Properties can be static
2. Creating CustomLabel
Data Transfer Object
CustomLabel
Data Transfer ObjectPlease follow the following steps:
Create a Data Transfer Object class
Add
DataTransferAttribute
to the classAdd the properties that you want to synchronize between frontend and backend
CSharp [DataTransfer("cstmlbl")] public class CustomLabel { public string Text { get; set; } public string AnotherText { get; set; } }
Observations
classes should not inherit from any other classes
use nullable types for value types. For example
int?
instead ofint
the name used on the
DataTransfer
attribute should beunique
as short as possible
it should start with a lowercase consonant
it should not use vowels
if it is made up of several words the next word should have just its first letter in uppercase
3. Creating CustomLabel
Mapper
CustomLabel
MapperPlease follow the following steps:
Create the mapper class
Implement
IMapper
interface, in which the first type parameter of this generic interface should be the model. The second parameter of generic interface is the DTO class.Implement the methods of
IMapper
Observations
Map
: this method is called to create a DTO instance and populate from the model. If you do not want to send anything to the client you can returnnull
ApplyChanges
: this method is called tosync
back data from the client. In this method you can take data sent from the client and apply those changes to the model on the backend. E.g. If there were a change ofAnotherText
from client side, it will update the value and trigger the AnotherTextChanged event.Configure
: this method is called to setup which data elements from the model must be considered as references.The extension method
GetChange
must be used. This returns null when the property doesn't been modified during the request, otherwise, it returns the value of the modified property.
4. Register the mapper
This final step is simply going to the Registration
class (situated under DTO project) and add the following line to the RegisterMapper method: