public
Created

  • Download Gist
hello_structuremap.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
using System;
using StructureMap;
 
namespace ConsoleApplication1
{
class Program {
private static void Main(string[] args) {
IContainer container = ConfigureDependencies();
IAppEngine appEngine = container.GetInstance<IAppEngine>();
appEngine.Run();
}
 
private static IContainer ConfigureDependencies() {
return new Container(x =>{
x.For<IAppEngine>().Use<AppEngine>();
x.For<IGreeter>().Use<EnglishGreeter>();
x.For<IOutputDisplay>().Use<ConsoleOutputDisplay>();
});
}
}
 
public class AppEngine : IAppEngine {
private readonly IGreeter greeter;
private readonly IOutputDisplay outputDisplay;
 
public AppEngine(IGreeter greeter, IOutputDisplay outputDisplay) {
this.greeter = greeter;
this.outputDisplay = outputDisplay;
}
 
public void Run() {
outputDisplay.Show(greeter.GetGreeting());
}
}
 
public interface IAppEngine {
void Run();
}
 
public interface IGreeter {
string GetGreeting();
}
 
public class EnglishGreeter : IGreeter {
public string GetGreeting() {
return "Hello";
}
}
 
public class FrenchGreeter : IGreeter {
public string GetGreeting() {
return "Bonjour";
}
}
 
public interface IOutputDisplay {
void Show(string message);
}
 
public class ConsoleOutputDisplay : IOutputDisplay {
public void Show(string message) {
Console.WriteLine(message);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.