In a comment on Derik Whittaker's post about using app.config with StructureMap, Brian Johnston asks for a simple "hello world" example. I figured I could re-create the app that I used when I was first learning StructureMap. This is a very simple demonstration of how to make an application use StructureMap. It does not get into the whys and whens for different scenarios. It is simply a piece of code that will let you see StructureMap "work" so that you can play with it. For more introductory details, I would highly recommend reading Chad Myers' posts that cover Basic and Medium-level usage scenarios for StructureMap.
using System; using StructureMap; namespace ConsoleApplication1 { class Program { private static void Main(string[] args) { ConfigureDependencies(); IAppEngine appEngine = ObjectFactory.GetInstance<IAppEngine>(); appEngine.Run(); } private static void ConfigureDependencies() { StructureMapConfiguration.ForRequestedType<IAppEngine>().TheDefaultIsConcreteType<AppEngine>(); StructureMapConfiguration.ForRequestedType<IGreeter>().TheDefaultIsConcreteType<EnglishGreeter>(); StructureMapConfiguration.ForRequestedType<IOutputDisplay>().TheDefaultIsConcreteType<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); } } }
Remember Me
Powered by: newtelligence dasBlog 2.1.8209.14743
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
Site theme based on the essence design by Jelle Druyts