  The metric dimension of a graph is the minimum size of a set of vertices such
that each vertex is uniquely determined by the distances to the vertices of
that set. Our aim is to upper-bound the order $n$ of a graph in terms of its
diameter $d$ and metric dimension $k$. In general, the bound $n\leq d^k+k$ is
known to hold. We prove a bound of the form $n=\mathcal{O}(kd^2)$ for trees and
outerplanar graphs (for trees we determine the best possible bound and the
corresponding extremal examples). More generally, for graphs having a tree
decomposition of width $w$ and length $\ell$, we obtain a bound of the form
$n=\mathcal{O}(kd^2(2\ell+1)^{3w+1})$. This implies in particular that
$n=\mathcal{O}(kd^{\mathcal{O}(1)})$ for graphs of constant treewidth and
$n=\mathcal{O}(f(k)d^2)$ for chordal graphs, where $f$ is a doubly-exponential
function. Using the notion of distance-VC dimension (introduced in 2014 by
Bousquet and Thomass\'e) as a tool, we prove the bounds $n\leq (dk+1)^{t-1}+1$
for $K_t$-minor-free graphs, and $n\leq (dk+1)^{d(3\cdot 2^{r}+2)}+1$ for
graphs of rankwidth at most $r$.
