Created
January 24, 2016 10:57
-
-
Save anton-abyzov/89f0287a43c6bc73b301 to your computer and use it in GitHub Desktop.
Structuremap registration via IContainer vs ObjectFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Machine.Specifications; | |
using NUnit.Framework; | |
using StructureMap; | |
namespace StructuremapTests | |
{ | |
// structuremap version 3.1.6.186, Machine Specification 0.9.3, Machine.Specifications.Should 0.8.0 | |
class Program | |
{ | |
[Test] | |
public void get_the_default_instance() | |
{ | |
var container = new Container(x => { x.For<ITest>().Use<Test>(); }); | |
container.GetInstance<ITest>() | |
.ShouldBeOfExactType<Test>(); | |
container.GetInstance<ITest>() | |
.ShouldBeOfExactType<Test>(); | |
} | |
[Test] | |
public void should_throw_on_get_instance_from_ObjectFactory() | |
{ | |
var container = new Container(x => { x.For<ITest>().Use<Test>(); }); | |
Assert.Throws<StructureMapConfigurationException>(() => ObjectFactory.GetInstance<ITest>()); | |
} | |
[Test] | |
public void get_instance_when_registered_via_ObjectFactory() | |
{ | |
ObjectFactory.Initialize((x) => | |
{ | |
x.For<ITest>().Use<Test>(); | |
}); | |
var instance = ObjectFactory.GetInstance<ITest>(); | |
instance.ShouldBeOfExactType<Test>(); | |
} | |
public interface ITest | |
{ | |
int Id { get; set; } | |
} | |
public class Test : ITest | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment