C
C#3y ago
bakk

Using dependency injection for internal classes inside a library

I'm making a library, where I feel like it would be useful to be able to use Microsoft's dependency injection library in order to deal with dependencies (both inside the library and letting other projects take advantage of it). However, I'm not quite sure how to deal with internal classes. I have a public class that users of the library would instantiate, and this class contains instances of internal classes or classes containing internal classes. I want to pass these in the various constructors to avoid creating them on the spot everywhere, but things like ActivatorUtilities.CreateInstance fail when they have to call an internal constructor at any point, even if it's inside the project itself. Any ideas how to deal with this? I don't want to make everything public!
6 Replies
Becquerel
Becquerel3y ago
things like ActivatorUtilities.CreateInstance fail when they have to call an internal constructor at any point, even if it's inside the project itself
I wasn't aware of this restraint - are you saying this happens with Microsoft.Extensions.DependencyInjection? because i've just now tried changing a class in my personal project to internal and it still worked in the DI container are you aware of the extension method pattern? e.g.
public static ClassInYourLibrary
{
public static IServiceCollection AddYourLibrary(this IServiceCollection services)
{
return services
.AddTransient<YourPublicLibraryClass>()
.AddSingleton<YourInternalLibraryClass>();
}
}
public static ClassInYourLibrary
{
public static IServiceCollection AddYourLibrary(this IServiceCollection services)
{
return services
.AddTransient<YourPublicLibraryClass>()
.AddSingleton<YourInternalLibraryClass>();
}
}
then the user of your library just calls services.AddYourLibrary() when they're setting up their own DI packages like automapper and mediatr do this
bakk
bakkOP3y ago
Hmm maybe I'm misintepreting then I am indeed using Microsoft.Extensions.DependencyInjection and am doing the extension method pattern ActivatorUtilities in a public class in the library tries to create an instance of another public class with an internal constructor that takes an internal class as a parameter
Becquerel
Becquerel3y ago
i'm not sure i follow - you're calling activatorutilities directly? if it's specifically exploding on internal constructors, well... i'd suggest making it an internal class with a public ctor instead
bakk
bakkOP3y ago
Maybe it's a bad design but I feel like it would be useful to have this class available from the outside but it should be created by another class, not by users of the library At first let them be injected to the constructor of the parent class, but then i tried directly calling ActivatorUtilities as well but got the same error even though the calls are inside a constructor in a class in the library itself
bakk
bakkOP3y ago
bakk
bakkOP3y ago
ahaa
Want results from more Discord servers?
Add your server