Skip to content

A SpecFlow plugin that provides a generic way to implement a custom DI container

License

Notifications You must be signed in to change notification settings

maartenkools/SpecFlow.Contrib.GenericContainer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SpecFlow.GenericContainer

A SpecFlow plugin that provides a generic way to implement a custom DI container

Build status NuGet status

Usage

  1. Create a class that implements the IGenericContainer interface and wraps your DI container.
  2. Create a class that contains a static function with the ScenarioDependenciesAttribute that configures the container, and returns your IGenericContainer implementation.

Example

SimpleInjector

public class SimpleInjectorContainer : IGenericContainer
{
  private readonly Container _container;

    public SimpleInjectorContainer(Container container)
    {
      _container = container;
    }

    public void Register<TService>(Func<TService> instanceCreator)
      where TService : class
    {
      _container.Register(instanceCreator);
    }

    public object Resolve(Type bindingType)
    {
      return _container.GetInstance(bindingType);
    }

    public TService Resolve<TService>()
      where TService : class
    {
      return _container.GetInstance<TService>();
    }
}
public static class Dependencies
{
  [ScenarioDependencies]
  public static IGenericContainer CreateContainer()
  {
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
    container.Options.DefaultLifestyle = Lifestyle.Singleton;

    // Add your registrations here

    foreach (var type in typeof(Dependencies).Assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))))
    {
      container.Register(type);
    }

    return new SimpleInjectorContainer(container);
  }
}

About

A SpecFlow plugin that provides a generic way to implement a custom DI container

Resources

License

Stars

Watchers

Forks

Packages

No packages published