Skip to content

Instantly share code, notes, and snippets.

async Task FetchDocuments(BatchBlock<Document> batchDocuments, string connectionString, string sql, ProgressTask status)
{
status.Description = "[cyan]Fetching documents from database...[/]";
using SqlConnection sqlConnection = new(connectionString);
await sqlConnection.OpenAsync();
// Use READ UNCOMMITTED to avoid holding shared locks while streaming rows.
// This prevents blocking the concurrent UPDATE statements in ProcessMigratedDocuments,
// which operate on the same table. Dirty reads are acceptable here because:
// - We only read rows where the key IS NULL (not yet migrated)
// - Processed rows get a non-NULL key, so they won't be re-read
SELECT
tl.request_session_id,
tl.resource_type,
tl.resource_description,
tl.request_mode,
tl.request_status,
es.login_name,
es.program_name,
st.text AS sql_text
FROM sys.dm_tran_locks AS tl
SELECT
blocking.session_id AS blocking_session,
blocked.session_id AS blocked_session,
blocked.wait_type,
blocked.wait_time / 1000.0 AS wait_seconds,
blocked.status,
sq.text AS blocked_sql,
bsq.text AS blocking_sql
FROM sys.dm_exec_requests AS blocked
JOIN sys.dm_exec_sessions AS blocking
private class FilterComparer : IComparer<Filter>
{
public int Compare(Filter x, Filter y)
{
// Nulls always have to be less than non-nulls
if (x == null && y == null)
{
return 0;
}
if (x == null)
/// <summary>
/// Filter provider for Unity.
/// </summary>
public class UnityFilterAttributeFilterProvider : FilterAttributeFilterProvider
{
private readonly IDependencyResolver _resolver;
/// <summary>
/// Initializes a new instance of the <see cref="UnityFilterAttributeFilterProvider"/> class.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class OurOwnAuthorizeAttribute : AuthorizeAttribute
{
public OurOwnAuthorizeAttribute()
{
}
public OurOwnAuthorizeAttribute(params string[] requiredRights)
{
// Parameter objects
public record ProductFilters(
[FromQuery] string? Query,
[FromQuery] string? Category,
[FromQuery] decimal? MinPrice,
[FromQuery] decimal? MaxPrice
);
public record PaginationParams(
[FromQuery] int Page = 1,
public class GetOrderParams
{
[FromRoute]
public int OrderId { get; set; }
[FromHeader(Name = "X-Tenant-Id")]
public string TenantId { get; set; } = default!;
[FromQuery]
public bool IncludeLineItems { get; set; }
public record ProductSearchParams(
string? Query,
string? Category,
decimal? MinPrice,
decimal? MaxPrice,
int Page = 1,
int PageSize = 20
);
app.MapGet("/products/search", async ([AsParameters] ProductSearchParams search, ProductsDb db, CancellationToken ct) =>
{
// handler logic...
});