liveBook · Manning

Declarative data retrieval

As explained in Grokking Software Architecture

Declarative querying is a data-access technique in which code specifies the desired result while a query processor determines how to retrieve it.

Declarative data retrieval lets you describe the result you want, while an ORM translates that request into SQL. Instead of spelling out retrieval steps and raw SQL syntax, you build a query from your programming language. section 8.3.1

Listing 8.4 C# (Entity Framework / LINQ - https://learn.microsoft.com/en-us/ef):
var users = context.Users
    .Where(u => u.Age > 21 && u.IsActive)
    .OrderBy(u => u.LastName)
    .ToList();

This C# query selects active users older than 21 and sorts them by last name. section 8.3.1

The tradeoff is less direct control over SQL syntax in exchange for queries expressed in application code. The treatment also connects ORM querying to type safety and parameterization as a defense against SQL injection; parameterization details are deferred. section 8.3.1

How Grokking Software Architecture covers it

How deep it goes
Defines the declarative-versus-imperative distinction and demonstrates ORM queries, while deferring parameterization details.
Examples
Filters active users older than 21 and sorts by last name in SQL, C# LINQ, Python SQLAlchemy, and TypeScript Prisma.
What you can do after
Apply it via Entity Framework/LINQ, SQLAlchemy, or Prisma to express filters and ordering as query objects.
How it connects
Connects ORM querying with SQL, type safety, SQL injection, and parameterization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.