Chapter 9. Modeling More Complex Applications with dartling

In Chapter 4, Modeling Web Applications with Model Concepts and Dartlero, we discussed the importance of modeling the data in your domain before starting your app. We used model concepts to visually represent the model, and showed a simple modeling framework called Dartlero. In this chapter, we will discuss the full blown domain modeling framework dartling, which can take the JSON export of model concepts as an input to generate code for the model as well as to generate default app code. We do this by developing the Travel Impressions model and app.

Next, we look at the Model View Controller (MVC) design pattern and why it is a good fit for web applications. This is put into practice by developing a todomvc application in spirals. Again, model concepts and dartling are used to generate the basic application code. So, the following are the topics of this chapter:

  • The dartling domain modeling framework
  • Design of the Travel Impressions model in spirals
  • Code generation of Travel Impressions from the model
  • Initializing the Travel Impressions model with data
  • Testing the Travel Impressions model
  • What is the MVC pattern and why is it used in software development?
  • The TodoMVC app

The dartling domain modeling framework

The dartling (https://github.com/dzenanr/dartling) is a domain model framework for the development of complex models with many concepts and relationships. It takes care of a lot of functionality, greatly reducing the amount of code you would need to write. A dartling model is first designed in the graphical tool model concepts, as shown in Chapter 4, Modeling Web Applications with Model Concepts and Dartlero. A dartling model consists of concepts, concept attributes, and concept neighbors. Two neighbors make a relationship between two concepts. A relationship has two directions; each direction going from one concept to another neighbor concept. Standard one-to-many, many-to-many, and is-a relationships are supported. When both concepts are the same, the relationship is reflexive. When there are two relationships between the same but different concepts, the relationships are twins.

The code for a dartling model is generated from the JSON representation of a graphical model. The model is initialized with some basic data and tested (models' tests are the best way to start learning dartling). We can also generate a default web application; whose purpose is to validate the model by a user in order to discover missing concepts, attributes, and relationships; and improve the existing ones.

A dartling model has access to actions, action pre and post validations (these are validations of the data done before and after actions), error handling, select data views, view update propagations, reaction events, transactions (a transaction is an action that contains other actions), and sessions with the history of actions and transactions so that undos and redos on the model may be done.

In dartling, there may be multiple domains and multiple models within the domains, which can be used together. A model has entry points that are entities. From an entity in one of the entry entities, child entities may be obtained. Data navigation is done by following the parent or child neighbors. You can add, remove, update, find, select, and order (sort) the data. Actions or transactions may be used to support unrestricted undos and redos in a domain session. The domain allows any object to react to actions (action events) in its models.

To understand what else you can do with dartling, clone it from Git and examine its API defined in the abstract classes with its API at the end of their names. The two most important ones are EntitiesApi and EntityApi; they provide the public methods available in dartling to handle the entities. Dart generics are used to enforce a specific type for a dartling entity, as shown in the following code snippet:

abstract class EntitiesApi<E extends EntityApi<E>> implements Iterable<E> {
  Concept get concept;
  ValidationErrorsApi get errors;
  EntitiesApi<E> get source;

  E firstWhereAttribute(String code, Object attribute);
  E random();
  E singleWhereOid(Oid oid);
  EntityApi singleDownWhereOid(Oid oid);
  E singleWhereCode(String code);
  E singleWhereId(IdApi id);
  E singleWhereAttributeId(String code, Object attribute);

  EntitiesApi<E> copy();
  // sort, but not in place
  EntitiesApi<E> order([int compare(E a, E b)]);  
  EntitiesApi<E> selectWhere(bool f(E entity));
  EntitiesApi<E> selectWhereAttribute(String code, Object attribute);
  EntitiesApi<E> selectWhereParent(String code, EntityApi parent);
  EntitiesApi<E> skipFirst(int n);
  EntitiesApi<E> skipFirstWhile(bool f(E entity));
  EntitiesApi<E> takeFirst(int n);
  EntitiesApi<E> takeFirstWhile(bool f(E entity));
  List<Map<String, Object>> toJson();
  void clear();
  void sort([int compare(E a, E b)]); // in place sort
  bool preAdd(E entity);
  bool add(E entity);
  bool postAdd(E entity);
  bool preRemove(E entity);
  bool remove(E entity);
  bool postRemove(E entity);
}

abstract class EntityApi<E extends EntityApi<E>> implements Comparable {
  Concept get concept;
  ValidationErrorsApi get errors;
  Oid get oid;
  IdApi get id;
  String code;

  Object getAttribute(String name);
  bool setAttribute(String name, Object value);
  String getStringFromAttribute(String name);
  bool setStringToAttribute(String name, String string);
  EntityApi getParent(String name);
  bool setParent(String name, EntityApi entity);
  EntitiesApi getChild(String name);
  bool setChild(String name, EntitiesApi entities);

  E copy();
  Map<String, Object> toJson();
}

Note that EntityApi implements Comparable, and EntitiesApi implements Iterable. Thus, all the public members of Iterable are available in the dartling entities. There are several dartling examples at GitHub; you can look at (all the URLs have the same structure, https://github.com/dzenanr/, followed by the name of the project, for example, https://github.com/dzenanr/art_pen):

  • art_pen: This is a drawing tool based on the Logo programming language for children
  • game_parking: This is a game based on Rush Hour
  • dartling_examples: This contains different types of relationships
  • concept_attribute: This deals with different categories of test data that can be used in the generation of tests
  • travel_impressions: This is the model we will discuss to illustrate dartling in this chapter
  • dartling_todos: This is a web application based on TodoMVC with dartling, action undos, and web components

Schematically, the following sections show how to:

  1. Design a graphical model step by step:
  2. Generate code for the dartling model from model.json:
  3. Initialize the dartling model with some basic data.
  4. Test the model.

Note

If you want to follow new developments in dartling, consult On Dart Blog (http://dzenanr.github.io/) and On Dart Education (http://ondart.me/).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.70.170