Created
November 30, 2015 23:03
-
-
Save anton-abyzov/cef8a1a93f0e0c804c1c to your computer and use it in GitHub Desktop.
StructureMap 2.6.4.1 supports lazy initialization
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 System; | |
using NUnit.Framework; | |
using StructureMap; | |
namespace StructureMapTests | |
{ | |
public class SMTests | |
{ | |
[Test] | |
public void WillUseLazyInit() | |
{ | |
//arrange | |
ObjectFactory.Initialize(x => { x.For<IDependency1>().Use<Dependency1>(); }); | |
//act | |
var main = ObjectFactory.GetInstance<Main>(); | |
main.Write(); | |
//assert: the order of messages in Console should be | |
//Write method is called | |
//Dependency created | |
//Dependency property Name is called | |
} | |
} | |
public interface IDependency1 | |
{ | |
string Name { get; } | |
} | |
public class Dependency1 : IDependency1 | |
{ | |
public Dependency1() | |
{ | |
Console.WriteLine("Dependency created"); | |
} | |
public string Name | |
{ | |
get { return "Dependency property Name is called"; } | |
} | |
} | |
public class Main | |
{ | |
private readonly Func<IDependency1> _dep; | |
public Main(Func<IDependency1> dep) | |
{ | |
_dep = dep; | |
} | |
public void Write() | |
{ | |
Console.WriteLine("Write method is called"); | |
Console.WriteLine(_dep().Name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment