Skip to content

Instantly share code, notes, and snippets.

@Pondidum
Created May 12, 2016 13:17
Show Gist options
  • Select an option

  • Save Pondidum/e3bb80340382ed83ce767cd18efc19fd to your computer and use it in GitHub Desktop.

Select an option

Save Pondidum/e3bb80340382ed83ce767cd18efc19fd to your computer and use it in GitHub Desktop.
public class Scratchpad
{
[Fact]
public void When_testing_something()
{
var container = new Container(c =>
{
c.Scan(a =>
{
a.TheCallingAssembly();
a.WithDefaultConventions();
a.AddAllTypesOf<ISource>();
});
c.For<ISource>().Use(context => context.GetInstance<SourceFactory>().GetSource("first"));
});
container.GetInstance<ISource>().ShouldBeOfType<FirstSource>();
}
}
public class SourceFactory
{
private readonly List<ISource> _sources;
public SourceFactory(IEnumerable<ISource> sources)
{
_sources = sources.ToList();
}
public ISource GetSource(string name)
{
return _sources.Single(s => s.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
}
}
public interface ISource
{
string Name { get; }
}
public class FirstSource : ISource
{
public string Name { get { return "First Source"; } }
}
public class Second : ISource
{
public string Name { get { return "Second Source"; } }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment