Refactor DBcore without changing behaviour

This PR contains some of the refactoring from #919. It extends the `Type` API and clearly separates responsibilities. The new API allows us to remove a lot of similar code.

For a review I suggest to first look at the final `types.py]` file and the changes in the model implementation, modulo the changes in 19d3ca822ef539ac99ec9143d97e625c34c94516. This will get you an overview of the goal. Then each commit should be reviewed. The changes are atomic and should leave the behaviour of the program unchanged.