Skip to content

Instantly share code, notes, and snippets.

@mythz
Created May 29, 2012 06:39
Show Gist options
  • Save mythz/2822977 to your computer and use it in GitHub Desktop.
Save mythz/2822977 to your computer and use it in GitHub Desktop.
StackOverflow Funq IOC code example
//Had to make these types from the example since the example didn't compile and they weren't supplied
public interface IFoo { }
public interface IFoo2 { }
public interface IBar { }
public class Foo : IFoo { }
public class Bar : IBar { }
public class Test
{
public IFoo Foo { get; set; }
public IFoo2 Foo2 { get; set; }
public IBar Bar { get; set; }
}
[Test]
public void Can_AutoWire()
{
//var test = new Test(); WTF? how can you expect anything to populate this instance?
var container = new Container();
container.Register<IFoo>(c => new Foo());
container.Register<IBar>(c => new Bar());
container.RegisterAutoWired<Test>(); //This registers an autowired version of the Test type and binds all above deps
//container.Register<int>(c => 10); IOC's are meant for dependencies not valuetypes which aren't supported.
var test = container.Resolve<Test>(); //This retrieves an autowired instance of Test
Assert.IsNotNull(test.Foo);
Assert.IsNotNull(test.Bar);
Assert.IsNull(test.Foo2 as Foo);
//Assert.IsNotNull(test.Names); //Where did these come from, and how can you expect them to not be null??
//Assert.Equals(test.Age, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment