Home > Development > StructureMap Manual Registration

StructureMap Manual Registration


In an attempt to go and learn FubuMVC I decided to Learn the basics of StructureMap first. Being already fluent in Windsor this is a pain as now I am going to have to know how to use 2 containers.

I have put together a contrived example on how to manually register a concrete class against an interface and provided the basic code to do so.

I have other posts on IoC and containers if you want more information.

So heres the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StructureMap;

namespace StructureMapSamples
{

    interface ILogger
    {
        void Log(string message);
    }

    class Logger : ILogger
    {
        public void Log(string message)
        {
            Console.WriteLine(message);
        }
    }

    public static class BootStrapper
    {
        public static void WireUp()
        {
            ObjectFactory.Initialize(
                    x => x.For<ILogger>().Use<Logger>()
                );
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            BootStrapper.WireUp();

            ILogger logger = ObjectFactory.GetInstance<ILogger>();
            logger.Log("Hello World From StructureMap");

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

    }
}

As I get time between coding and learning Ruby I will add new posts with more advanced usages in the next few days.

Blair..

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: