-
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
Provide the consumer type in factories #109
Comments
Hi I can definitely see the use case here. Currently in StrongInject there's no way to see how a type is going to be used when you resolve it in order to modify how it's resolved. I don't know the best way to support this functionality. I think what I'll do is I'll mull over this for a few days, and see if I can think of anything elegant, and not overly complex. If I do, I'll post the design here for further discussion. |
Sorry it took me so long to get back to you - I've been distracted by other things and then forgot about this. What I'm thinking of doing is adding a type:
Then ONLY for InstancePerDependency dependencies you can have a parameter of type ResolutionInfo. If no registrations exist for that type, then StrongInject will create it with the correct type. So you could do: [Factory(Scope.InstancePerDependency)] ILogger CreateLogger(ResolutionInfo rInfo) => new Logger(rInfo.ResolvedAsDependencyOf); WDYT? |
For my usecase with Serilog I need to create a new logger from an existing one.
So would look something like:
But I worry that this would be circular. My generic logger is currently created as follows
I think I can solve this by creating a ILoggerResolver, which holds the generic ILogger, and then make a factory that looks like this:
So I think your design is good 👍 |
@trampster you could do that by making it a decorator instead. [Decorator] ILogger CreateLogger(ResolutionInfo rInfo, ILogger logger) => logger.ForContext(typeof(rInfo.ResolvedAsDependencyOf)); |
This is exactly my use case as well, using a decorator seems perfectly reasonable. |
I think there's some further design questions that need to be answered. If I have the following code: public class A{ public (IB b){} }
public class B : IB { public B(ILog log){} }
public class Log : ILog { public Log(ResolutionInfo info){} } What would you expect |
I would expect it to be |
Some further cases which I don't think are obvious what to do. If I have: public record A(ResolutionInfo info);
public record B(A[] as); Should ResolutionInfo contain public record A(ResolutionInfo info);
public record B(List<A> as);
[Register(typeof(A)]
[Register(typeof(B))]
public class Container : IContainer<B>
{
[Factory] List<T> CreateList<T>(T[] ts) => ts.ToArray();
} By the same logic, |
Note that you can work around this issue by using Microsoft's |
Sorry, I saw this and got stuck on an answer for it and forgot to come back to it. It's a really good question that I don't have an answer to because I've never actually encountered a situation where it was a problem. There are a couple of things that I would note that may make this harder or simpler, I'm not sure, and that's the case that I've never needed the concept that ResolutionInfo provides directly inside a dependancy. In general, I've always tried to keep my code unaware of the container as possible unless it's doing nested resolution, and as such I personally would only need it to be available for [Factory] methods (and probably decorators) I honestly wish I had a better answer, but I'm afraid I don't. |
First of all, love the project. I've been really excited for source generators and this project just uses then so well.
Now, I'm trying to inject a logging interface in my application.
The interface is:
And my implementation is:
As you can see, the consumer type is used in the implementation but not in the interface.
I know that default (at least for microsoft) is to use something like
ILogger<T>
, but all my projects usesILogger
(and I personally thinks that is better).I don't know if currently is possible to use StrongInject to resolve this.
One possible solution is to provide the consumer type (the type begin resolved) in the factory methods and in the
IFactory
interface.I'm thinking something like this:
Or maybe something like this:
I understand that this is probably a breaking change and if it's not something that you're not willing to do.
Thanks for the wonderful project 😄
The text was updated successfully, but these errors were encountered: