Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is there syntax to avoid calling operator new in lambda registrations #60

Unanswered
ekalchev asked this question in Q&A
Discussion options

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?

You must be logged in to vote

Replies: 2 comments

Comment options

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.

You must be logged in to vote
0 replies
Comment options

Here's issue. Supplement it please. And if you have any ideas on API, I would be grateful if you share.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /