Skip to content
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

Add DI container support for contextual runners #59

Closed
2 of 3 tasks
Suremaker opened this issue Oct 13, 2017 · 4 comments
Closed
2 of 3 tasks

Add DI container support for contextual runners #59

Suremaker opened this issue Oct 13, 2017 · 4 comments
Labels
enhancement An enhancement of the feature or new feature
Milestone

Comments

@Suremaker
Copy link
Collaborator

Suremaker commented Oct 13, 2017

Progress

  • Feature is implemented,
  • Ensured backward-compatibility,
  • Ensured good debugging experience.

Description

At this moment it is possible to specify Runner.WithContext<MyContext>() to use contextful runner with context having parameterless constructor. If context has dependencies, it has to be manually instantiated with code like Runner.WithContext(new MyContext(dependency)), and all dependencies has to be resolved manually in code. Finally, the contexts implementing IDisposable interfaces are not disposed after usage.

Initial Idea

  • The ˋRunner.WithContext()ˋ should be extended to allow instantiating types with any constructor, using DI container to instantiate the context.
  • The DI container should have sane defaults, such as:
    • working without any 3rd-party DI container integrations,
    • resolving dependencies to the widest constructor, without need of explicit registrations,
    • instantiate all types per test,
  • The DI container should be configurable with LightBddConfiguration, allowing to:
    • choose a 3rd-party implementation if desired (like Autofac),
    • configure choosen implementation,
  • The default DI container implementation should allow to:
    • customize registration of type (custom instantiation with ability to resolve dependency),
    • specify if instance is created per test or shared (optional requirement),
  • The runner should be updated to control lifespan of the contexts (dispose if needed)

Story definition

After spike, I have decided to add DI container support to LightBDD with focus on supporting proper, mature DIs but also provide a very basic DI in LightBDD itself.

Features:

  • LightBDDConfiguration.DependencyContainerConfiguration() configuration allowing to specify DI container type and configure it,
  • LightBDD.Autofac project offering integration with Autofac,
  • Basic DI container implementation (enabled by default), supporting:
    • container scoping,
    • instance registration,
    • constructor injections,
    • resource disposals,
  • Modify WithContext<TContext>() extension methods to use DI containers and lifting where TContext: new() constraint,
  • Add StepExecution.Current.GetScenarioDependencyResolver() extensions to allow retrieving DI resolver during scenario execution,
  • Add ResourcePool<T> type for handling resource pools (DI friendly),
  • Expose DI containers for scenario/step decorators.
@Suremaker Suremaker added the idea A new, high-level idea label Oct 13, 2017
@Suremaker Suremaker changed the title 0 Add DI container support for contextual runners Oct 13, 2017
@aidapsibr
Copy link

Hey, you might want to check out SpecFlows BoDi container for a default integrated DI container. I am using it in psibr/REstate successfully with external DI support. It is pretty limited, but also easy to modify as I did to support IEnumerable resolutions.

@Suremaker Suremaker added this to the 2.4.0 milestone Apr 16, 2018
@Suremaker Suremaker added enhancement An enhancement of the feature or new feature and removed idea A new, high-level idea labels Jun 10, 2018
@Suremaker
Copy link
Collaborator Author

Hello @psibernetic . I have just recently came to this ticket to think how to implement it.
Thanks for the hint - I have took a look at the BoDi implementation, however decided to build some abstractions in LightBDD itself to support container scoping (for contextual scenarios and steps) and integration with other DI containers.

I have got a basic DI working which will be shipped with LightBDD 2.4.0, and added LightBDD.Autofac project with sample integration with Autofac. Hopefully, over the time there will be more integrations with other people favourite DIs.

There is still a lot of docs and samples to be written before shipping 2.4.0, but it should appear soon :)

@aidapsibr
Copy link

Very good sir, looking forward to it.

Suremaker added a commit that referenced this issue Jun 11, 2018
* Implemented BasicDependencyContainer and wired it up to LightBDD
* Added LightBDD.Autofac
* Implemented WithContext<T>() to use DI
* Updated containers to register self
* Updated BasicDependencyContainer to use parameterized ctors for resolving items
* Extended FeatureCoordinator to dispose DI container upon disposal
* Implemented ResourcePool and ResourceProvider
* Exposed DependencyResolver and Context on IScenario and IStep interfaces
* Added EnableCurrentScenarioTracking() feature configuration.
* Added StepExecution.GetScenarioDependencyResolver() extension method
@Suremaker
Copy link
Collaborator Author

The feature has been implemented on develop branch and will be released with LightBDD 2.4.0

@Suremaker Suremaker mentioned this issue Jun 12, 2018
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement of the feature or new feature
Projects
None yet
Development

No branches or pull requests

2 participants