-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider way to control injection for one parameter of a constructor #67
Comments
We could add an attribute: public class ParameterFactoryAttribute
{
public ParameterFactoryAttribute(Type forType, int? parameterIndex = null, string? parameterName = null, bool useForAnyParametersOfSameTypeAsReturnType = true)
} Then it could be used as follows: public class MyService { public MyService(Dep1 dep, Dep2 dep2, Dep3 dep3, Dep4 dep4, string connectionString) }
[Register(typeof(MyService))]
[Register(typeof(Dep1))]
[Register(typeof(Dep2))]
[Register(typeof(Dep3))]
[Register(typeof(Dep4))]
public class Container : IContainer<MyService>
{
[ParameterFactory(typeof(MyService))] string GetConnectionString() => LoadConnectionStringFromConfig();
} |
This would be particularly useful for registrations of Unless there's a better workaround for |
@Stroniax I'm not sure I fully understand the issue. Would you be able to provide a code sample with what you are doing now, and explain what the issue is with that solution, and how this proposal would help? Thanks! |
https://gist.github.com/Stroniax/c6ffd55b9506d4bfb7feb4036bc895b5 The
This may be a contrived example, but it demonstrates the issue of configured parameters required for injection, especially when they are disposable. This also allows a container to override the client for a single service without overriding configuration everywhere. |
If GlobalStateHttpClient is disposable stronginject will dispose of it. If that isn't possible you can implement |
Sorry I misunderstood. Ignore my previous comment. |
My current advice would be to make GlobalStateHttpClient disposable and have it dispose of the httpclient. Alternatively you could make the Factory take as a parameter a However I agree that this proposal would be useful. I don't actively maintain stronginject at the moment, but if you would like to contribute a solution I would be very happy to review it and provide guidance. |
HttpClient instances obtained from HttpClientFactory do not need to be disposed at all. https://stackoverflow.com/a/54326424/521757 |
Is this project still maintained? |
I do not currently have time to maintain this project, but I am happy to review pull requests. |
If a constructor has multiple parameters, one of which needs special treatment, you need to create a factory method which takes all the other parameters as dependencies and manually calls the constructor.
This means if a new parameter is added to the constructor, we have to update the factory as well.
If there was some way to modify a single parameter of a constructor, that would avoid this issue.
The text was updated successfully, but these errors were encountered: