ScopeGuard 0.0.2
See the version list below for details.
dotnet add package ScopeGuard --version 0.0.2
NuGet\Install-Package ScopeGuard -Version 0.0.2
<PackageReference Include="ScopeGuard" Version="0.0.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="ScopeGuard" Version="0.0.2" />
<PackageReference Include="ScopeGuard"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add ScopeGuard --version 0.0.2
#r "nuget: ScopeGuard, 0.0.2"
#:package ScopeGuard@0.0.2
#addin nuget:?package=ScopeGuard&version=0.0.2
#tool nuget:?package=ScopeGuard&version=0.0.2
ScopeGuard
A Roslyn-based analyzer that enforces architectural boundaries at compile time.
The Problem
Many architectural patterns divide code into layers where access between them should be controlled. In practice this breaks down at one specific place: types that must be public for technical reasons, but were never meant to be used everywhere.
A common example is EF Core — entity classes must be public for the ORM to work, but that makes them accessible from any layer in the solution:
// In a UI controller — this compiles fine, but violates your architecture
var user = new UserRepository().GetById(id);
Console.WriteLine(user.PasswordHash); // Domain internals, exposed to the UI
The type is public because it has to be. But it was never meant to be used here.
The Solution
ScopeGuard introduces a [VisibleTo] attribute that lets you declare which namespaces are allowed to use a type. Any violation becomes a compile-time error — not a code review comment, not a runtime exception, a build failure.
using ScopeGuard.Attributes;
namespace MyApp.Domain
{
[VisibleTo("MyApp.Application.**")]
public class User
{
public string PasswordHash { get; set; }
}
}
Now this:
// In MyApp.UI.Controllers
var hash = user.PasswordHash; // error SG001: Access denied by ScopeGuard
Fails to build with:
error SG001: Member 'PasswordHash' is available to 'MyApp.Application.**',
but is being accessed by 'MyApp.UI.Controllers.UserController.Index()'.
Access denied by ScopeGuard.
How It Works
ScopeGuard is a Roslyn analyzer — it runs inside the compiler during every build. There is no runtime overhead and no separate tool to run.
When it sees a member access, property read/write, or method call, it checks whether the caller's namespace matches any of the patterns declared on the target type. If not, it emits a compiler error.
Pattern Syntax
| Pattern | Matches |
|---|---|
MyApp.Application |
Exactly that namespace |
MyApp.Application.* |
Any single segment under Application (e.g. MyApp.Application.Handlers, but not MyApp.Application.Sub.Handlers) |
MyApp.Application.** |
Any namespace rooted at Application, at any depth |
Multiple patterns are combined with OR logic — access is granted if the caller matches any of them.
[VisibleTo("MyApp.Application.**", "MyApp.Tests.**")]
public class Order { ... }
Examples
Protect an entire class
namespace MyApp.Domain
{
[VisibleTo("MyApp.Application.**")]
public class Invoice
{
public decimal Total { get; set; }
public void Approve() { }
}
}
All members of Invoice are restricted. Only code in MyApp.Application or its sub-namespaces may call Approve() or read Total.
Allow multiple layers
[VisibleTo("MyApp.Application.**", "MyApp.Tests.**")]
public class OrderLine { ... }
Both application code and tests can access OrderLine. Everything else cannot.
Diagnostic Reference
| ID | Severity | Message |
|---|---|---|
| SG001 | Error | Member '{0}' is available to '{1}', but is being accessed by '{2}'. Access denied by ScopeGuard. |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.