question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to provide one of several constructor dependencies from a factory method?

See original GitHub issue

I’m sitting with service fabric, and are wondering how to apply dependency injection in a good way…

The reliable services has a CreateServiceInstanceListeners-method that you override to provide the different listeners for the service. You would typically do this by creating an instance of ServiceInstanceListener. This class takes a factory function as it’s first parameter:

public ServiceInstanceListener(Func<StatelessServiceContext, ICommunicationListener> createCommunicationListener, string name = "");

So this factory function should create an instance of your listener.

My listener has a constructor with multiple dependencies. One of them is the StatelessServiceContext-instance provided in the factory function.

Is there a way of resolving a service where one of the dependencies is provided?

e.g. container.GetInstance<MyServiceListener>(context)

so that the container will use the provided services for any matching constructor parameter and resolve the rest from the container?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
vegarcommented, Sep 14, 2018

I haven’t had the time. Just close the case, though. I’ve received good input 😃


Fra: Bernhard Richter notifications@github.com Sendt: fredag, september 14, 2018 8:16 pm Til: seesharper/LightInject Kopi: Vegar Vikan; Mention Emne: Re: [seesharper/LightInject] How to provide one of several constructor dependencies from a factory method? (#435)

@vegarhttps://github.com/vegar Did you manage to get this working?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/seesharper/LightInject/issues/435#issuecomment-421442124, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAuqZcQK6n50TfK3dp_UoSkhS8EajvJtks5ua_JigaJpZM4WBGbo.

1reaction
seesharpercommented, Sep 13, 2018

The general advice here is not to mix runtime data with services. In fact, this is probably one of the features I regret ever adding to LightInject. We are actually considering marking these register methods obsolete and remove them in the next major release. Instead of injecting runtime data, it is better to let this data flow through the methods exposed by the service.

In your case it would probably be better to inject some kind of IContextAccessor that provides the context. 👍

This topic is very well covered in this article by @dotnetjunkie

https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=99

Read more comments on GitHub >

github_iconTop Results From Across the Web

Factory method for objects with multiple complex constructors
You would basically have one big factory of factories, which would then ... First, having methods and constructors with so many (different!)
Read more >
ASP.NET Core Dependency Injection with Multiple ...
If you read the article completely, you'll see the answer is simple. In case a type you control: remove one constructor. If this...
Read more >
Dependency Injection anti-pattern: multiple constructors
When Dependency Injection is applied correctly and completely, it is important that each type only has one constructor—multiple constructors ...
Read more >
Dependency Injection with Factory | by Tan Chun Wei
First, the creation of objects is simple using constructor injection. · First, it will lead to a code smell called the long parameter...
Read more >
When Is Factory Class Better Than Calling Constructor?
However, there are cases when multiple constructors with different parameters are required. In those instances, a factory method works better as they don't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found