-
Notifications
You must be signed in to change notification settings - Fork 29
-
I found I have many lines of this kind that requires to construct the instance by using operator new
.Bind("PackageUpdaterServiceDescriptor").To(ctx => { ctx.Inject(out Func<PackageUpdaterService> packageUpdaterServiceFactory); return new ServiceDescriptor("PackageUpdaterService", packageUpdaterServiceFactory); })
is there a way to achieve that without the need to provide every argument for the constructor but only the arguments that I choose. For example
.Bind("PackageUpdaterServiceDescriptor").To(ctx => { ctx.Inject<ServiceDescriptor>(out ServiceDescriptor serviceDescriptor, DependencyOverride.With<string>("PackageUpdaterService")); return serviceDescriptor; })
This way if I add more arguments to ServiceDescriptor constructor I won't need to modify constructor call.
Another question. Why you choose inject to have out parameter instead of returning an instance?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments
-
I found I have many lines of this kind that requires to construct the instance by using operator new
Yes, I have seen a lot of such code in your examples too. Unfortunately I can't share with you examples in my private repositories with a lot of bindings, but for example I rarely use bindings with lambda functions. It looks much clearer and requires less support. I actually only use them when I want to add some sort of manual creation logic.
is there a way to achieve that without the need to provide every argument for the constructor but only the arguments that I choose
Unfortunately, there is no such way now. I will add a ticket about it. We need to come up with a convenient and simple API that eliminates ambiguities, for example - passing a parameter of the same type, dependence on parameter names.
Another question. Why do you choose inject to have out parameter instead of returning an instance?
This was done intentionally. Using a method that returns a parameter is much harder to replace when rewriting code using Roslyn's syntax tree. And it would lead to slower analysis and many potential errors, for example, when a lambda function contains conditional transitions and other non-trivial syntactic constructions. The current Inject() method call is very easy to replace with a block of statements of any size and type. You've certainly noticed that all statements like ctx.Inject<T>(...) are replaced by creating dependencies directly. This is very good for performance and for reducing memory consumption.
Beta Was this translation helpful? Give feedback.
All reactions
-
Here's issue. Supplement it please. And if you have any ideas on API, I would be grateful if you share.
Beta Was this translation helpful? Give feedback.