Meta data of model

When you define a model, a meta data of the model is created by Annotation Processing Tool automatically. For example, when you define slim3.demo.model.Employee, slim3.demo.meta.EmployeeMeta is created. If you change your model definition, the meta data of the model is recreated.

A meta data of a model includes mapping method between a model and an entity as follows:
@Override
public Foo entityToModel(Entity entity) {
    model = new Employee();
    model.setKey(entity.getKey());
    model.setFirstName(
        (String)entity.getProperty("firstName"));

    model.setLastName(
        (String) entity.getProperty("lastName"));

    model.setHireDate(
        (Date) entity.getProperty("hireDate"));

    return model;
}

@Override
public Entity
modelToEntity(Object model) {
    Employee m = (Employee) model;
    Entity entity = null;
    if (m.getKey() != null) {
        entity = new Entity(m.getKey());
    } else {
        entity = new Entity("Employee");
    }
    entity.setProperty("firstName", m.getFirstName());
    entity.setProperty("lastName", m.getLastName());
    entity.setProperty("hireDate", m.getHireDate());
    return entity;
}

Slim3 creates mapping methods at compile time and uses it at runtime, so Slim3 does not need runtime reflection for mapping. As a result, Slim3 is faster than other frameworks.

A meta data of model has also attribute definitions for type-safe programming:
public final StringAttributeMeta<Blog> content =
    new StringAttributeMeta<Blog>(this, "content", "content");

public final CoreAttributeMeta<Blog, Key> key =
    new CoreAttributeMeta<Blog, Key>(this, "__key__", "key", Key.class);

public final StringAttributeMeta<Blog> title =
    new StringAttributeMeta<Blog>(this, "title", "title");

public final CoreAttributeMeta<Blog, Long> version =
    new CoreAttributeMeta<Blog, Long>(this, "version", "version", Long.class);

Next...

Continue to Core Value Types.


Comments