Pure.DI and preserving module level encapsulation #34
-
Hello, I am not sure how or is it possible to be able to register classes in the executing assembly container of different modules/assemblies without the need to make the types public. Example: Assembly A contains those types
Executing Assembly need to register the dependency this way
but it won't compile because MyInterfaceImpl is internal class Is it possible to avoid making MyInterfaceImpl public and still be able to register the dependency? We currently use Unity container and we do this by exposing extension method in Assembly A which register the dependencies on the container instance. The Executing assembly calls this method. This way we have encapsulation on module level but still be able to register dependencies. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
The place in an application where you configure DI is usually the top-level module responsible for the composition of the application and providing an entry point into it. Mark Seemann in his book Dependency Injection has covered this issue in one of the chapters. The point is that this module can depend on all the modules of the application to be able to perform the application composition. In other words you can make all required internal types visible to this module where you configure DI. If you are using the modern .NET project model, then add the <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="MyConsoleApp" />
</ItemGroup>
</Project> And the top module where you configure DI is called |
Beta Was this translation helpful? Give feedback.
-
@ekalchev Was my answer helpful? Feel free to ask if something is wrong. |
Beta Was this translation helpful? Give feedback.
-
Microsoft introduced Dependency Registrations abstractions through Microsoft.Extensions.DependencyInjection.Abstractions after the advent of .NET Core. Leveraging these abstractions, Microsoft has developed a series of NuGet packages that efficiently handle registrations using the provided abstractions. The genius of this approach lies in the fact that each of these NuGet packages exposes a class with extension methods for IServiceCollection, simplifying the service registration process. This solution is agnostic to the specific Dependency Injection (DI) framework in use, as long as it adheres to Microsoft.Extensions.DependencyInjection.Abstractions. Our codebase, consisting of over 200 C# projects, is organized into multiple NuGet packages that our seven top-level projects utilize. In alignment with Microsoft's approach, we've adopted a strategy where each NuGet package can autonomously manage registrations by exposing an extension method for IServiceCollection. This extension method efficiently registers all services within the associated C# projects into the provided IServiceCollection container. I realize this might be a significant change, but I'm excited about the potential of using something like Pure.DI in our codebase. Specifically, I'm curious about enabling registrations from different assemblies within our projects. It could make our project more modular and easier to maintain. I'm not sure if Pure.DI already supports this. Do you think it's doable with your library, or would it need some modifications? Your insights would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
-
Yes, I may have misunderstood the question. Does this approach work for different projects, different assemblies, or NuGet packages? It could potentially be possible to use DependsOn for sub-projects. I'll try to play with it. |
Beta Was this translation helpful? Give feedback.
In short, your proposed scenario does not fit the pure DI paradigm. There is no complexity to analyze all types, including those not visible in the project where DI is configured. But what to do with them next? Pure.DI generates code in the pure DI paradigm, which is just a set of nested constructors, for example: