ardalis / ardalis/Specification

Raw SQL with Specification - What's the recommended approach?

Open
#540 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2.3k
Forks
262
PR merge metrics
No merged PRs in 30d

Description

I'm currently working with Ardalis.Specification and have a use case that requires raw SQL queries, specifically for spatial data with PostGIS functions. For example:

```csharp
var nearbySegments = await _context.StreetSegments
.FromSqlInterpolated($"""
SELECT *
FROM "public"."StreetSegments"
WHERE "CityId" = {act.CityId}
-- exclude segments already processed in the direct pass:
AND NOT ("OsmWayId" = ANY({directWayIds}))
-- use meters with geography casting:
AND ST_DWithin("Geom"::geography, {routeLine}::geography, {sightRadiusMeters}, true)
""")
.AsNoTracking();
```

What is the recommended approach for handling raw SQL queries like this while still using the Specification pattern? Do you have any suggestions for integrating these types of queries into the Ardalis.Specification workflow, or is it better to handle them separately outside the specification pattern?

I'm curious what other developers typically do when they encounter scenarios where LINQ expressions aren't sufficient for their query needs but they want to maintain the benefits of using specifications.

Contributor guide

Open the contributing guide

Research direction

Start with the issue's FromSqlInterpolated example and the repository's specification workflow. Determine whether raw SQL with PostGIS belongs inside a specification or should remain separate, then document a clear recommended approach and its limitations for users.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, postgresql, sql
Domain
backend-api-design, databases
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.