Let us now apply Dartlero to a model with two concepts and one relationship, our category-links model. Once again, clone the project from GitHub with:
git clone git://github.com/dzenanr/dartlero_category_links.git
The model is implemented in the lib
folder of the dartlero_category_links
application (refer to the following screenshot):
There are three dart files in the model
folder, one for the model and two for the two entities. The dartlero_category_links
library is defined in the dartlero_category_links.dart
file:
library dartlero_category_links; import 'package:dartlero/dartlero.dart'; part 'model/category_entities.dart'; part 'model/category_links_model.dart'; part 'model/link_entities.dart';
There are two classes in the category_entities.dart
file, one for the entity definition and the other for a collection of entities. The
Category
class of the model extends the ConceptEntity
class of Dartlero
(line (1)
), while the Categories
class inherits its properties and methods from the ConceptEntities
class (line (2)
):
part of dartlero_category_links; class Category extends ConceptEntity<Category> { (1) String description; Links links = new Links(); Category newEntity() => new Category(); String toString() { return ' { ' ' ${super.toString()}, ' ' description: ${description} ' ' } '; } Map<String, Object> toJson() { Map<String, Object> entityMap = super.toJson(); entityMap['description'] = description; entityMap['links'] = links.toJson(); return entityMap; } fromJson(Map<String, Object> entityMap) { super.fromJson(entityMap); description = entityMap['description']; links.fromJson(entityMap['links']); } bool get onProgramming => description.contains('programming') ? true : false; } class Categories extends ConceptEntities<Category> { (2) Categories newEntities() => new Categories(); Category newEntity() => new Category(); }
The Category
class inherits the code property from the ConceptEntity
class; it has its own description and links properties. Note that a parent-child relationship direction is represented as the links property in the Category
class. In addition, the Category
class inherits the public application interface of the ConceptEntityApi
class. The Categories
class (line (2)
) is simple because most of its behavior is defined in the ConceptEntities
class of Dartlero
. The Link
and Links
classes are created in a similar way (refer link_entities.dart
).
The CategoryLinksModel
class extends the ConceptModel
class of Dartlero (refer to category_links_model.dart
). The newEntries
method in the CategoryLinksModel
class provides the only entry into the model. The init
method creates a few categories and links. The display
method shows data of the model in the console. To see the model in action, run the following programs in the test
folder: category_links_model_test.dart
, category_entities_test.dart
, or link_entities_test.dart
. They exercise the same methods and tests as we saw in the first Dartlero example. For example, the model test program calls the init
and display
methods:
import 'package:unittest/unittest.dart'; import 'package:dartlero/dartlero.dart'; import 'package:dartlero_category_links/dartlero_category_links.dart'; testModel() { CategoryLinksModel categoryLinksModel; Categories categories; group("Testing Model: ", () { setUp(() { categoryLinksModel = new CategoryLinksModel(); categoryLinksModel.init(); categories = categoryLinksModel.categories; }); tearDown(() { categories.clear(); expect(categories.isEmpty, isTrue); }); test('Display model', () { categoryLinksModel.display(); }); }); } main() { testModel(); }
This code produces output similar to the previous code snippet.
18.224.30.19