ASP.Net vNext brought us a new notion: “Assembly Neutral Types”. And it will be the next “little thing with big impact” on .Net world.
tldr: Assembly Neutral Types will be .Net way to do loose coupling.
Role, contract, facade, protocole or whatever name you choose to use must follow strong typing rules on .Net. This means you must have a reference on the assembly containing the interface or abstract class. And this mandatory reference trigger lost of stagnation or lost of boilerplate wrapping code.
An example is better than a million of word particularly in this case. Suppose we want to share a general purpose interface (“à la” ILogger). We have two solutions: wait than Microsoft declare it in .Net or take the risk that this interface becomes duplicated everywhere. None is satisfactory. Everybody agree on an ILogger interface but almost everybody declare a new one (one in each library implementing loging and one in each library hiding several loging implementation). And this is mandatory for now: a library doesn’t want to have dependency upon any other sibling.
[AssemblyNeutral] is an attribute than can be placed on interfaces (concretely any interface, struct, class without IL) to say: this “declaration” is sharable and everyone can declare the same interface (same complete name and same contents). All this declarations will be “unify to the same type” at runtime.
This mechanism use a trick made possible by Roslyn compiler (see some difficulties for other languages can apply like in F# in ASP.NET 5: The good, the bad, and the really ugly).
Assembly Neutral Types must follow some understandable rules but one of them:
“The types must match exactly! We load the first type with a matching name, if they don’t match expect chaos.”
can be complicated to follow. I just hope that this “chaos” could be properly detected If two [AssemblyNeutral] types differs, consequences can only happen at runtime. We must have a way to detect them.
For more informations:
- detailed principles
- implementation on ASP.NET vNext using Roslyn
- ASP.NET vNext wiki on Assembly Neutral Interfaces.
Obviously, ANT will be integrated into .Net Core 5.