Tye build SDK from .NET 8 onwards includes support for “interceptors”, which are a mechanism for emitting code during build that redirects method calls. In
the case of Dapper, this means we can replace the QuerySingleOrDefault<Customer>(...)
call (above) and replace it with a generated method that
has a compatible signature and behaves as we expect, but: using code generated entirely at build time. Making existing code compatible with AOT is
one of the main goals of “interceptors”, so: we’re not doing anything unexpected here.
Your project still needs to reference Dapper, so we can see what your code is trying to do. In many cases, enabling Dapper.AOT will completely replace the Dapper
code, and you won’t even need the Dapper library at runtime (although there are some scenarios in which it will remain - for example, if you’re using the QueryMultiple
API, the returned GridReader
is defined inside Dapper, so it will be retained).
Yes. Usually at least as well, sometimes better. In addition to general optimizations (made possible by not having to emit compatible IL, which is very hard), there are entire
new features being used in Dapper.AOT (for example, the DbBatch
API is now supported, allowing multiple operations to be performed without paying latency per operation).
Not yet - not by a long way:
private
types) can’t be implemented because we have to obey rules that we could simply bypass in ref-emit ILIn these cases, we just leave the original Dapper code alone (and maybe offer guidance).
Yes! A few examples (there are more):
SqlBulkCopy
(this is sikilar to FastMember)Yes; in Visual Studio, expand your project, Dependencies, Analyzers, Dapper.AOT.Analyzers, and look for the generated .cs
files at the bottom. For other IDEs: refer
to their documentation around “generators”.
Interceptors are only supported on C# currently - so the AOT bits are C# only.
Analyzers (for usage guidance) are supported on C# and VB (if you’re using VB you may prefer to use the Dapper.Advisor package instead of Dapper.AOT; this is the exact same analyzer package, but without the runtime library, since it will never be used).
Analyzers and interceptors are implemented via Roslyn, which supports C# and VB; no other languages are supported by Dapper.AOT or Dapper.Advisor.
We can’t physically stop you, but:
On your own head be it.
Maybe log an issue here.