Skip to content

Datastore Overview

alexstaeding edited this page Mar 7, 2020 · 1 revision

Anvil defines a structured data flow from command to database that will be referred to as "The stack" or "The data stack" from now on. This data flow defines how user input (from commands) flows down the layers of your plugin until it reaches the database.

Dbo

It all starts with a Dbo. This layer doesn't "do" anything, it just defines the structure of your collections. The following code is an example of such a Dbo. It defines a collection where each document/row has two properties/columns, int bar and int baz.

public interface Foo<TKey> extends ObjectWithId<TKey> {

    int getBar();
    void setBar(int bar);

    int getBaz();
    void setBaz(int baz);
}

At this layer of abstraction, we are not aware of which database we are using and therefore are also not aware of id type for this database. This is what the type parameter TKey is for. Different databases have different id types and that needs to be accounted for at this layer.

Repository

The next layer is known as the Repository layer. You will define one of these for each abstract collection and datastore implementation combination plus a shared one. For example, if you have an abstract collection of "Foo", you'll have the following Repositories:

  • FooRepository (shared foo stuff between MongoDB and Xodus)
  • MongoFooRepository (MongoDB specific stuff)
  • XodusFooRepository (Xodus specific stuff)