ScopeGuard 0.1.1
dotnet add package ScopeGuard --version 0.1.1
NuGet\Install-Package ScopeGuard -Version 0.1.1
<PackageReference Include="ScopeGuard" Version="0.1.1" />
<PackageVersion Include="ScopeGuard" Version="0.1.1" />
<PackageReference Include="ScopeGuard" />
paket add ScopeGuard --version 0.1.1
#r "nuget: ScopeGuard, 0.1.1"
#:package ScopeGuard@0.1.1
#addin nuget:?package=ScopeGuard&version=0.1.1
#tool nuget:?package=ScopeGuard&version=0.1.1
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; }
}
}
Any of the following from an unauthorized namespace now fails to build:
// In MyApp.UI.Controllers
var user = new User(); // error SG001 — object creation
var hash = user.PasswordHash; // error SG001 — member access
// In MyApp.UI
class AdminUser : User { } // error SG001 — inheritance
class UserList : IEnumerable<User> { } // error SG001 — generic type argument
void Handle(User user) { } // error SG001 — method parameter
User GetCurrent() => null!; // error SG001 — return type
private User _current; // error SG001 — field type
What Is Checked
ScopeGuard catches every place a restricted type appears:
- Member access — calling a method, reading or writing a property or field on a restricted type
- Object creation —
new RestrictedType() - Inheritance and interface implementation —
class Sub : RestrictedType,class Impl : IRestrictedInterface - Method signatures — a restricted type as a parameter type or return type, including constructors
- Field and property types —
private User _user,public User Current { get; set; } - Delegate signatures — a restricted type as a parameter or return type of a delegate declaration
- Event types —
public event UserChanged OnChanged - Generic type arguments — in all of the above:
List<User>,IRepository<User>,Action<User>,IHandler<Command<User>>
The attribute is placed on the type, not on individual members. All uses of the type are restricted.
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 { ... }
Notes
[VisibleTo]can only be applied to classes and structs. It cannot be placed on methods, properties, or fields.- The attribute is not inherited — subclasses of a restricted type are not themselves restricted unless they carry their own
[VisibleTo]. - Same-layer references: because member signatures are checked, types in the same layer that reference each other must include their own namespace in the allowed list. For example, if
UserandOrderare both inMyApp.DomainandOrderappears in aUsermethod signature,Order's[VisibleTo]must include"MyApp.Domain.**"alongside any other allowed namespaces.
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.