Use SqlSource in several projects
Use InternalsVisibleTo between a test project, class libraries and an app that use SqlSource, and catch the exception of a method by its type.
Use this when more than one project of a solution uses SqlSource, such as a main project and its test project. Use it too when two projects need the queries of one .sql file.
Let a test project see the internals of the main project
The main project grants InternalsVisibleTo to its test project:
<!-- App.csproj -->
<ItemGroup>
<InternalsVisibleTo Include="App.Tests" />
</ItemGroup>
The test project can use SqlSource too:
// App.Tests, which references the SqlSource package too, with "output": "methods" in the sqlsource.json of
// Queries, since a static type has no repository methods
using SqlSource;
[SqlSourceGenerate(Source = "Queries")]
internal static partial class TestQueries;
It catches by its type the exception that a method of App throws:
// App.Tests catches the exception that a method of App throws by its type
await Should.ThrowAsync<UnexpectedRowCountException>(() => users.GetActiveUser(new GetActiveUserParams(email)));
What you get. The test project uses App's UnexpectedRowCountException, TransactionRolledBackException and CollectionItemException, whether it uses SqlSource or not. So it catches by its type what a method of either project throws. The attributes that each project gets do not conflict.
The rules you need.
- A project that sees the exceptions in a project it references gets none of its own. Its query methods throw the exceptions of the other project. See projects that share internals.
- If App uses the exceptions of a shared project, the shared project grants
InternalsVisibleToto App.Tests too. See the shared project of an app that sees two data projects.
Share the exceptions between two data projects and an app
<!-- Data.Shared.csproj: references the SqlSource package, and has no query -->
<PropertyGroup>
<!-- .NET 6 or later, alone or beside an older target such as netstandard2.0 -->
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Data.Orders" />
<InternalsVisibleTo Include="Data.Users" />
<InternalsVisibleTo Include="App" />
<!-- and each test project of a data project -->
<InternalsVisibleTo Include="Data.Orders.Tests" />
<InternalsVisibleTo Include="Data.Users.Tests" />
</ItemGroup>
<!-- Data.Orders.csproj and Data.Users.csproj: each references the SqlSource package and has its queries -->
<ItemGroup>
<ProjectReference Include="../Data.Shared/Data.Shared.csproj" />
<InternalsVisibleTo Include="App" />
</ItemGroup>
// App, which sees the internals of all three
catch (UnexpectedRowCountException exception)
{
// thrown by a method of Data.Orders or of Data.Users
}
What you get. Each data project uses the exceptions of Data.Shared instead of writing its own. So the app sees one copy, and catches by its type what a method of either data project throws.
The rules you need.
- Grant
InternalsVisibleTofrom Data.Shared to every project that catches the exceptions. - Grant
InternalsVisibleTofrom Data.Shared to every project that uses SqlSource and sees the internals of a data project. The test projects of the data projects are among them. - A project that uses SqlSource and does not see the copy of Data.Shared gets its own copy. Its
catchcompiles, but misses what a data project throws. - Without the shared project, an app that uses SqlSource and sees the internals of both data projects gets SQLSRC022. See projects that share internals.
- Data.Shared must target .NET 6 or later, or add such a target to its
TargetFrameworks. Onnetstandard2.0ornetstandard2.1alone, a data project gets SQLSRC038. - On .NET 6 or later, the
CollectionItemExceptionof Data.Shared passes onSqlState,IsTransientandBatchCommand.
One .sql file in two projects
If two projects must use the same queries, keep the .sql file in one project and list it in the other:
App.slnx
App/
App.csproj
sqlsource.json "dialect": "postgres"
Queries/
UserRepository.cs
UserRepository.sql used by App and by Reports
UserRepository.sql.json one sidecar for both projects
Reports/
Reports.csproj
UserRepository.cs
<!-- Reports/Reports.csproj: references the SqlSource package -->
<ItemGroup>
<AdditionalFiles Include="../App/Queries/UserRepository.sql" />
<AdditionalFiles Include="../App/Queries/UserRepository.sql.json" />
</ItemGroup>
// Reports/UserRepository.cs
using Npgsql;
using SqlSource;
namespace Reports;
[SqlSourceGenerate(Source = "../App/Queries/UserRepository.sql")]
public partial class UserRepository(NpgsqlDataSource dataSource);
$ dotnet sqlsource describe App.slnx
What you get. Each project gets its own UserRepository, with a method and models for each query of the file. The file has one sidecar, and the run on the solution describes its queries for both projects.
The rules you need.
- A
.sqlfile outside the project's folder is not included. List it and its sidecar asAdditionalFilesitems. See Which.sqlfiles are included. Sourceis relative to the folder of the C# file, never to the project. See Source.- Both projects read the settings of the file from its markers and the settings files of its own folder. So both projects need the same entries in its sidecar. See a file in two projects.
- If one project finds a settings file that the other does not, a run on it can leave out or remove entries that the other needs. Name every settings file
sqlsource.json, and describe the whole solution in one run. See a file in two projects.
If it does not work
- SQLSRC004 in the second project. The project does not list the
.sqlfile. Add it as anAdditionalFilesitem. See SQLSRC004. - SQLSRC241 in the second project, and the sidecar has the entry. The project does not list the sidecar. Add it as an
AdditionalFilesitem. See SQLSRC241. - SQLSRC241 in one project after a run on the other. The two projects read different settings for the file. Describe the whole solution in one run. See a file in two projects.
- SQLSRC022 in the app. Put the exceptions in a shared project, as Share the exceptions between two data projects and an app shows. See SQLSRC022.
- SQLSRC038 in a data project. Add a target of .NET 6 or later to Data.Shared. See SQLSRC038.
- A
catchcompiles, but misses what a data project throws. Grant the projectInternalsVisibleTofrom Data.Shared. See Share the exceptions between two data projects and an app.