Suppose that we have three layers; UI, Business, Data. We are using DI. I don't wan't Data layer to be accessible from UI.
The problem is about DI registration of Data layer. Composition root is in UI and I don't wan't to have any reference to Data there. I've found this answer. As I understand, we should reference to all layers and think they are libraries not layers. This way I can specify that my Business layer use Data layer or anything else that I want. After all this is why DI is around.
It's correct, but there is a problem! We shouldn't use Data layer at UI. But once a developer accidentally referenced from UI to Data, and another one injected something from Data layer to a UI class directly. So he skipped the Business layer that I never want.
How can I handle such situations? I'd like to have some limitations, but on the other hand I want the DI flexibility.
Of course some folks believe that we can have a separate library just for dependency registration.
What is the best pattern here?
The answer you linked to has two answers that explain that having the composition root and the UI layer in the same assembly is not a problem:
But if you think this is a problem, why don't you just move the UI layer in its own assembly? The startup assembly doesn't need to have the UI layer! If you create a Windows Forms application, move all the Forms to their own assembly. If you're creating a ASP.NET MVC application, move the Controllers and views to their own assembly. Depending on the type of application you're building, you will have to apply certain tricks to get this working, but for most project types in .NET this is possible.
The real question is, is this worth the trouble? If you want this in order to prevent having to do code reviews, you are fooling yourself.