Skip to content

Share a procedure's remembered parameter names by source, not by command - #312

Open
electricessence wants to merge 2 commits into
masterfrom
fix/name-cache-owner-source
Open

electricessence wants to merge 2 commits into
masterfrom
fix/name-cache-owner-source

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

A command made from a factory or pool that implements only the non-generic interface, from a Func<IDbConnection> through StoredProcedure, or from the SQL Server packages' Func<SqlConnection> Command and StoredProcedure, was given a new owner of the remembered procedure-parameter names for every command. Each command's first run therefore read the names from the database again instead of sharing what the factory, pool or function had already read. Every command made from one source now shares one set.

  • ExpressiveCommand(IDbConnectionFactory) goes through the function constructor with the factory's Create method, so the factory is the identity. A null factory still throws ArgumentNullException naming connectionFactory.
  • The pool that wraps a non-generic pool reports the wrapped pool (or what that one stands for) as its identity.
  • The owner is worked out in one place, when names are found or remembered: a pool made around a factory or function stands for that factory or function, anything else for itself. The command's own owner property is gone, and the definition runner only maps a transaction to its connection and asks the same place. This also covers a typed function whose connection type is narrower than the command's (Func<SqlConnection>), which shared nothing before.
  • The Func extensions of Core and the SQL Server packages use AsPool, as the typed ones already did, instead of wrapping the function in a new factory for each call.

Different factories, pools and functions still do not share. A function is identified by its target: a lambda that captures a local or parameter and is built again for each call is a new source each time, and lambdas that capture nothing share their class. Commands from one object share names whichever of its methods they use. The key has no login, so two logins to one server and database share a set; a DbException from the database or from the caller's own code inside the call still forgets the shared set, so concurrent callers may read again together. An empty answer stays cached as before. A struct pool passed to ExpressiveCommand(IDbConnectionPool) is boxed per command and does not share. These limits are documented in expressive-commands.md, and the compiler-dependent cases are pinned by tests that say they pin current compiler behavior, not a contract.

Cost: one interface type check per name lookup replaces the command's own type-pattern check; one delegate is allocated per command construction through the factory constructor (not per lookup). The schema read now happens once per source instead of once per command. No public API change (the PublicAPI files are untouched and the analyzers report nothing).

Tests: 24 new tests in NameCacheSourceTests.cs (fake provider counting schema reads; the two SQL Server packages against LocalDB). Before the fix 13 of them failed (two commands from one source read twice); all pass after. The null-argument entry points are pinned with their current exception types and parameter names.

Verification: solution Release and Debug (VS MSBuild) 0 warnings, 0 errors; Core.Tests 16178 passed with the generator on, 16157 passed and 16 skipped with it off; 267 sample checks; style and analyzer checks 0 findings on all projects; docfx 0 warnings; style scan clean.

Reviews: a correctness lens (no defect that breaks sharing, leaks or changes exception types; documentation gaps applied) and a rules, docs and tests lens (layout and wording findings applied).

🤖 Generated with Claude Code

electricessence and others added 2 commits December 3, 2026 14:34
A command made from a factory or pool that implements only the
non-generic interface, or from a Func<IDbConnection> through
StoredProcedure, or from the SQL Server packages' Func<SqlConnection>
Command and StoredProcedure, was given a new owner of the remembered
names for every command. Each command's first run therefore read the
procedure's parameter names from the database again, instead of sharing
what the factory, pool or function had already read.

Now every command made from one source shares one set:

- ExpressiveCommand(IDbConnectionFactory) goes through the function
  constructor with the factory's Create method, so the factory is the
  identity. A null factory still throws ArgumentNullException naming
  connectionFactory.
- The pool that wraps a non-generic pool reports the wrapped pool (or what
  that one stands for) as its identity.
- The owner is worked out in one place, when names are found or
  remembered: a pool made around a factory or function stands for that
  factory or function, anything else for itself. The command's own owner
  property is gone, and the definition runner only maps a transaction to
  its connection, then asks the same place. This also covers a typed
  function whose connection type is narrower than the command's
  (Func<SqlConnection> for example), which shared nothing before.
- The Func extensions of Core and the SQL Server packages use AsPool, as
  the typed ones already did, instead of wrapping the function in a new
  factory for each call.

Different factories, pools and functions still do not share. A function
is identified by its target: a lambda that captures a variable and is
built again for each call is a new source each time, and lambdas that
capture nothing share their class. Commands from one object share names
whichever of its methods they use, and the key has no login, so two
logins to one server and database share a set; a DbException from the
database or from the caller's own code inside the call still forgets the
shared set, so concurrent callers may read again together. An empty
answer stays cached as before. These limits are documented.

Cost: one interface type check per name lookup (a command's run, or a
query's run), replacing the type-pattern check the command made before; a
command made from a factory or function now reads the names once for the
source instead of once for each command. No public API change: the build
raises no PublicAPI analyzer finding and the PublicAPI files are
untouched.

New tests count the reads at the provider for each entry point (factory,
pool, function, typed function and factory, and both SQL Server packages
on LocalDB), pin what a function's identity is, the null source of each
entry point, and what a DbException forgets. The doc comment that said a
static lambda has no target was wrong: a lambda that captures nothing has
the compiler's shared class as its target. A release note and the
expressive commands article say how the names are shared.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Lay the new tests out as the contributing guide asks: the arrow of an
expression body on the next line, long call chains broken after the first
call, shorter comment lines, and small helpers for the SQL Server runs. The
tests that pin what the compiler does today (lambdas that capture nothing
share one class, a static method group converted twice) now say they pin
current compiler behavior, not a contract, and the static method group test
no longer asserts that the two delegates are the same object, only the read
count. Wrap three comments and remove a stray blank line where the command's
owner property was deleted.

Documentation: the release note says a separate cache was kept per command
and that lambdas that capture nothing, in one class, share a source. The
function-identity remark now covers a lambda that captures only this, and
no longer states the compiler's delegate caching of a static method as
fact. The expressive commands article says how methods, lambdas and struct
pools are told apart, and that the cache key has no login or default
schema.

No behavior change in the library.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant