🔏 Internal modules concrete encapsulation

A neat way to hide the concrete implementation between internal modules.

🔏 Internal modules concrete encapsulation
Image source: https://www.thinkwithgoogle.com/intl/en-emea/future-of-marketing/privacy-and-trust/research-customer-privacy-practices/

This article is an extension of the article about the encapsulation of module internals using Friend Assemblies:

https://hintea.com/friend-assemblies-for-software-engineering

The given article describes how we encapsulate module operations that are meant for internal use only, and not exposed to consuming clients.

This article builds on top of that, and presents a neat way to hide the concrete implementation between internal modules themselves.


The trick is to use .NET nested types.

And that's because nested types can be private! Which means that we can define an internal interface, which is visible to friend assemblies, and nest within it a private class with its concrete implementation!

Like this:

internal interface ImAnInterface
{
    Task DoStuff();

    private class Concrete : ImAnInterface
    {
        public Task DoStuff()
        {
            throw new System.NotImplementedException();
        }
    }
}


[...]

services.AddSingleton<ImAnInterface, ImAnInterface.Concrete>();
//or some other IoC pattern

Pretty fkn sweet, I'd say. 🍬