| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| <body> |
| <h1>Tutorial 7 - Properties</h1> |
| <p> |
| One of the goals of Apache Zest™ is to give you domain modeling tools that allow you to more concisely use |
| domain concepts in code. One of the things we do rather often is model Properties of objects as getters |
| and setters. But this is a very weak model, and does not give you any access to metadata about the property, |
| and also makes common tasks like UI binding non-trivial. There is also a lot of repetition of code, which |
| is unnecessary. Using JavaBeans conventions one typically have to have code in five places for one property, |
| whereas in Zest™ the same thing can be achieved with one line of code. |
| </p> |
| <p> |
| But lets start out easy. To declare a property you have to make a method in a mixin type that returns a value |
| of the type Property, and which does not take any parameters. Here's a simple example: |
| </p> |
| <pre> |
| Property<String> name(); |
| </pre> |
| <p> |
| This declares a Property of type String with the name "name". The Property interface has methods "get" and "set" |
| to access and mutate the value, respectively. |
| </p> |
| <p> |
| For now you will be responsible for implementing these methods, but later on these will be handled automatically, |
| thus reducing Properties to one-liners! |
| </p> |
| <p> |
| In the Mixin implementation of the interface with the Property declaration you should have an injection |
| of the Property, which is created for you by Zest™. The injection can be done in a field like this: |
| </p> |
| <pre> |
| @State Property<String> name; |
| </pre> |
| <p> |
| The State dependency injection annotation means that Zest™ will inject the Property for you. The field |
| has the name "name", which matches the name in the interface, and therefore that Property is injected. You can |
| then implement the method trivially by just returning the "name" field. |
| </p> |
| <p> |
| Properties can have Constraints just like method parameters. Simply set them on the Property method instead, |
| and they will be applied just as before when you call "set". |
| </p> |
| <p> |
| Steps for this tutorial: |
| </p> |
| <ol> |
| <li>Remove JavaBeans properties from HelloWorldState.</li> |
| <li>Remove HelloWorld and add the HelloWorldState and HelloWorldBehavior to the HelloWorldComposite interface.</li> |
| <li>Remove the HelloWorldBehaviourSideEffect.</li> |
| <li>Update the behaviour mixin to use the state interface accordingly.</li> |
| <li>Add Property methods with the correct type and the @NotEmpty annotation.</li> |
| <li>Update the state mixin to inject and return the properties as described above.</li> |
| </ol> |
| </body> |