Home > Development > StructureMap Basic Debugging

StructureMap Basic Debugging


Following on from the last post. We will look at the example and see how we can get a print out of what has been registered. This is really useful if you are having issues and need to see what StructureMap is actually doing.

To see what has been registered check out the WhatDoIHave method on the ObjectFactory Class.

Here is the source code with the new functionality.

using System;
using StructureMap;

namespace StructureMapSamples
{
    public interface ILogger
    {
        void Log(string message);
    }

    public class RemoteLogger : ILogger
    {
        private readonly string _server;

        public int Port { get; set; }

        public RemoteLogger(string server)
        {
            _server = server;
            Port = 80;
        }

        public void Log(string message)
        {
            Console.WriteLine("Writing {0} to {1}:{2}", message,_server,Port);
        }
    }

    public class Program
    {
        

        static void Main(string[] args)
        {
            Program program = new Program();
            program.InitializeContainer();
            program.PrintWhatHasBeenRegisteredInStructureMap();
            program.RunExamplesWithCtorArgsPassedToTypeAndPropertyValuesSet();

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

        public void InitializeContainer()
        {
            ObjectFactory.Configure(
                    x => x.For<ILogger>()
                          .Use<RemoteLogger>()
                          .Ctor<string>("server").Is("www.google.com")
                          .SetProperty(y => y.Port = 8080)
                );
        }

        public void PrintWhatHasBeenRegisteredInStructureMap()
        {
            Console.WriteLine(ObjectFactory.WhatDoIHave());
        }

        private void RunExamplesWithCtorArgsPassedToTypeAndPropertyValuesSet()
        {
            ILogger logger = ObjectFactory.GetInstance<ILogger>();
            logger.Log("Hello Google");
        }
    }
}

Also here is what the output from this method looks like.
================================================================================
===========================
Configuration Sources:

================================================================================
================================================================================
======================================================
PluginType Name
Description

——————————————————————————–
——————————————————————————–
——————————————————
Func`1 (Func`1)

Scoped as: Transient

15469a1d-2e3d-45d8-a850-e2eb37ea9187

——————————————————————————–
——————————————————————————–
——————————————————
IContainer (StructureMap.IContainer) 8d3565d0-8c7e-4bb2-8bea-9febbaf37fcd
Object: StructureMap.Container

Scoped as: Transient

8d3565d0-8c7e-4bb2-8bea-9febbaf37fcd
Object: StructureMap.Container

——————————————————————————–
——————————————————————————–
——————————————————
ILogger (StructureMapSamples.ILogger) 1abf6438-9b9a-4519-8651-51bd5dda1c69
Configured Instance of StructureMapSamples.RemoteLogger, StructureMapSamples,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Scoped as: Transient

1abf6438-9b9a-4519-8651-51bd5dda1c69
Configured Instance of StructureMapSamples.RemoteLogger, StructureMapSamples,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
================================================================================
================================================================================
======================================================
Writing Hello Google to http://www.google.com:8080
Press any key to quit

Hope this helps.

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: