In an ASP.NET Core modular monolith, how do internal types and InternalsVisibleTo keep a module's implementation private while its tests can still reach it?
answer
- public contracts, internal implementation
- a separate Contracts project
- <InternalsVisibleTo Include="...Tests" />
- never to the host or another module
- mocking internals needs DynamicProxyGenAssembly2
basics
~20 sMark everything in a module internal except its AddX and MapX methods and a small Contracts project of interfaces, DTOs and events. Other modules reference only Contracts. An InternalsVisibleTo item opens the internals to the module's own test project, nothing else.
solid answer
~40 sC#'s `internal` makes a type visible only inside its assembly, so if each module is its own project, `internal` is a compile-time module boundary. The usual split is two projects per module: `University.Fees.Contracts` with the public interface (`IFeesModule`), request and response records and integration events, and `University.Fees` with the implementation, all `internal` except `AddFeesModule` and `MapFeesEndpoints`. Other modules reference only the Contracts project. Tests still need the internals, so the implementation project adds `<InternalsVisibleTo Include="University.Fees.Tests" />`; the .NET SDK turns that item into an `[assembly: InternalsVisibleTo]` attribute. Granting it to the host or to another module defeats the boundary. One gotcha: mocking libraries built on Castle DynamicProxy, such as Moq, also need `DynamicProxyGenAssembly2` in the list to mock an internal interface.
code
xml · 19 lines<!-- University.Fees.csproj -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\University.Fees.Contracts\University.Fees.Contracts.csproj" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="University.Fees.Tests" />
<!-- only if tests mock internal interfaces with a Castle DynamicProxy library -->
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>
</Project>go deeper
Know what internal means: visible within one assembly only. Know that tests reach internals through InternalsVisibleTo.
Explain the Contracts and implementation project split, and how the InternalsVisibleTo MSBuild item becomes an assembly attribute.
Cover the gotchas: no grants to the host or other modules, the DynamicProxyGenAssembly2 grant for mocks, and an architecture test on exported types.
Decide how strict the boundary must be: internal by default with review of every public type, versus testing modules only through their contracts or endpoints.
## internal as a module boundary C# has an access modifier designed for exactly this job. A type or member marked `internal` is visible to code in the same **assembly** and nowhere else. If each module of a university monolith (Admissions, Fees, Timetabling) is its own project, and therefore its own assembly, then `internal` turns "don't use Fees' invoice calculator from Admissions" from a code-review rule into a compile error. That only works if the habit flips. C# developers write `public class` by reflex, so a module stays sealed only if the team makes `internal` the norm and `public` the exception. ## The contracts-and-implementation split A common shape is two projects per module: | Project | Visibility | Contains | Referenced by | |---|---|---|---| | `University.Fees.Contracts` | public | `IFeesModule`, request and response records, integration events such as `InvoiceIssued` | other modules, the Fees implementation | | `University.Fees` | internal, except entry points | `FeesDbContext`, handlers, endpoints, entities, `FeesModuleFacade : IFeesModule` | the host only | The public surface of the implementation project shrinks to the `AddFeesModule` and `MapFeesEndpoints` extension methods the host calls. Admissions references `University.Fees.Contracts` and receives `IFeesModule` by injection; it never sees `FeesDbContext`. Dependency injection, Minimal APIs and EF Core all work with internal types. Registration code inside the module can name them, `AddScoped<IFeesModule, FeesModuleFacade>()` is legal where both are visible, and EF Core maps internal entity classes. Nothing about ASP.NET Core requires implementation classes to be public. ## Letting tests in with InternalsVisibleTo Unit tests for the module live in another assembly, so they cannot see internal handlers. `InternalsVisibleTo` is the opt-in exception: an assembly-level attribute naming a friend assembly that may use the internals. In an SDK-style project you write it as an MSBuild item rather than an attribute in code: ```xml <ItemGroup> <InternalsVisibleTo Include="University.Fees.Tests" /> </ItemGroup> ``` The SDK's assembly-info generation turns each item into `[assembly: InternalsVisibleTo("University.Fees.Tests")]`, adding the public key when the assembly is strong-named. Rules worth stating in an interview: - Grant it to the module's **own** test project only. - Never grant it to the host: the host would then compile against internals and the boundary is gone. - Never grant it between modules: that is two modules pretending to be separate. - If a test needs many internals, consider testing through the module's public contract or its HTTP endpoints instead. ## Setting up a module this way For a new Timetabling module, the sequence is short: 1. Create `University.Timetabling.Contracts` and `University.Timetabling` with `dotnet new classlib`, and reference Contracts from the implementation. 2. Put the public interface, request and response records and integration events in Contracts. Nothing else goes there. 3. Change every generated class in the implementation project to `internal`, leaving only the static class with `AddTimetablingModule` and `MapTimetablingEndpoints` public. 4. Add `<InternalsVisibleTo Include="University.Timetabling.Tests" />` to the implementation project. 5. Reference the implementation project from the host only, and Contracts from any module that needs to call Timetabling. Step 3 is the one that decays, because every new class starts public; the architecture test below is what keeps it done. ## The mocking gotcha Mocking libraries that generate proxy classes at run time with Castle DynamicProxy, Moq among them, create those proxies in a dynamic assembly named `DynamicProxyGenAssembly2`. To mock an `internal` interface, the module must also grant `InternalsVisibleTo` to that assembly. The error otherwise is a runtime failure to create the proxy, not a compile error, which surprises people who already granted access to the test project. ## What internal does not do - It is not a security boundary: reflection can still reach internal members at run time. - It does not stop a module's **data** being reached: a shared table queried with raw SQL bypasses every access modifier. - It only helps if modules are separate assemblies. Folders inside one project share one assembly, so `internal` there hides nothing from the neighbouring folder. ## Checking the boundary over time Access modifiers erode one `public` at a time. Teams keep them honest by reviewing every new `public` type in an implementation project, and by an architecture test asserting that an implementation assembly exports no types other than its module entry point, for example by checking `typeof(FeesModule).Assembly.GetExportedTypes()`.
- Why not keep all modules in one project with folders and rely on internal?`internal` is scoped to the assembly, and a single project is a single assembly. Every folder could still use every other folder's internal types, so the modifier would hide nothing between modules. Module boundaries enforced by `internal` need one project per module, or at least one per module implementation.
- The host needs to call a module's internal seeding routine at startup. Should the module grant InternalsVisibleTo to the host?No. Add a public entry point for the purpose instead, such as a `SeedFeesModuleAsync(this IServiceProvider services)` extension or a hosted service registered inside `AddFeesModule`. Granting the host access to internals invites every future shortcut through the same door.
- Classes must be public for DI or EF Core to construct them.
- Granting InternalsVisibleTo to the host is fine because the host owns the modules.
- internal inside a single project keeps modules apart just as well as separate projects.
- Marking types internal makes them unreachable even through reflection.
- Mocking an internal interface needs InternalsVisibleTo for the test project only.