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.

Improve Dependency injection scopes

See original GitHub issue

Hi,

just trying to get one thing done and having some difficulties. According to this each scenario has it’s own scope but additional scopes get created for composite steps. How do I properly share a dependency between my top-level steps and steps which are in a composite step and use their own context? Top-level steps are using StepExecution.Current.GetScenarioDependencyResolver().Resolve<T>() to inject a dependency and context uses constructor injection. Unfortunately I get two different instances while I’d like to share session state throughout entire scenario. The docs say Finally, container scopes are hierarchical, which means that they are aware of their parent and depending on configuration can use dependencies specified in higher scopes for injections. Can it be achieved using default DI container or only by applying a custom one which can handle scopes other than transient or singleton?

Maybe there is also a different way to share state between contexts for instance? I haven’t found a proper example for that. I can’t use neither of the methods described here because they only solve state sharing inside one “scope” (either context or a fixture class).

I come more from SpecFlow world and there was something like ScenarioContext which is basically a bag for anything that you want to share between steps no matter where the steps are defined. I can’t find a good way to achieve something similar in LightBDD. Well… I can imagine reimplementing a concept of ScenarioContext using AutoFac but don’t want to reinvent the wheel. Maybe it can be done without 3rd party DI?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
ipatalascommented, Mar 10, 2021

Works like a charm! Thanks again.

1reaction
Suremakercommented, Jan 23, 2021

I have also update the wiki to provide more details about sharing state with composite steps: https://github.com/LightBDD/LightBDD/wiki/Scenario-State-Management#sharing-state-with-composite-steps

Read more comments on GitHub >

github_iconTop Results From Across the Web

Effective Dependency Injection Scoping - AndroidPub
Specifically, avoid creating new instances of objects within your classes, and opt to have the instance provided through the class constructor.
Read more >
Dependency injection in ASP.NET Core
ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) ...
Read more >
How to control the scope of Dependency Injection in .Net ...
Net 2.0 Core. We use dependency injection straight out of the box. We have some services that we want to resolve from the...
Read more >
Dependency Injection in Android: Scopes and Lifecycles
Learn how to manage scopes and lifecycles with dependency injection in Android using Dagger, Hilt, Koin, or other frameworks or libraries.
Read more >
Improved Dependency Injection with the new providedIn ...
A detailed look into the new scopes for providedIn with Angular 9.
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