Last active
September 7, 2016 10:31
-
-
Save ramondeklein/ca9fa880364411a48ae220a138f16eba to your computer and use it in GitHub Desktop.
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 StructureMap; | |
namespace ConsoleApplication5 | |
{ | |
static class Program | |
{ | |
public interface ISayHello | |
{ | |
void SayHello(); | |
} | |
public class SayHelloImpl : ISayHello | |
{ | |
private readonly Guid _id = Guid.NewGuid(); | |
public void SayHello() => Console.WriteLine($"Hello {_id}"); | |
} | |
private static IContainer GetContainer() | |
{ | |
return new Container(c => | |
{ | |
c.For<ISayHello>().Use<SayHelloImpl>(); | |
c.For<ISayHello>().Add<SayHelloImpl>().Named("A"); | |
}); | |
} | |
public static void Main() | |
{ | |
using (var container = GetContainer()) | |
{ | |
var a = container.GetInstance<Func<ISayHello>>(); | |
a().SayHello(); // ID 1 | |
a().SayHello(); // ID 2 | |
// Via the factory it reuses the same instance | |
var a1 = container.GetInstance<Func<string, ISayHello>>(); | |
a1("A").SayHello(); // ID 3 | |
a1("A").SayHello(); // ID 3 (I would expect another one) | |
var a2 = container.GetInstance<Func<string, ISayHello>>(); | |
a2("A").SayHello(); // ID 4 | |
a2("A").SayHello(); // ID 4 (I would expect another one) | |
// Via explicit GetInstance calls it returns unique objects | |
container.GetInstance<ISayHello>("A").SayHello(); // ID 5 | |
container.GetInstance<ISayHello>("A").SayHello(); // ID 6 | |
Console.ReadLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment