Docs / Stored Procedure First

Stored Procedure First

What this page covers#

This page explains why the main MooDb surface is centered on stored procedures.

The core idea#

The main MooDbContext methods treat the command text as a stored procedure name.

For example:

</> C#
var count = await db.ScalarAsync<int>("dbo.usp_User_CountActive");

This is not an accident or a temporary design decision. It is part of the product philosophy.

Why this is useful#

A stored procedure-first API gives you:

  • a consistent default path
  • a clear separation between procedure calls and SQL text
  • a call site that communicates intent quickly
  • a natural fit for teams that already organize database behaviour in stored procedures

What raw SQL looks like instead#

Raw SQL is not hidden. It goes through db.Sql:

</> C#
var count = await db.Sql.ScalarAsync<int>(
    "select count(*) from dbo.tbl_User where IsActive = 1");

That difference in calling style is valuable because it makes the code self-explanatory.

When to prefer the stored procedure path#

Prefer the main MooDbContext surface when:

  • the operation already exists as a stored procedure
  • you want business database behaviour centralized in SQL Server
  • you want the default and most guided MooDb path