Home > Development > StructureMap Manual Registration – Setting Properties

StructureMap Manual Registration – Setting Properties

Hi all back again with another example of using structure map manually. Were going to follow on from the last example.

If you have a concrete type that need a property set then you use the SetProperty Method and use an action lambda statement like below to set it up. EASY.

Pretty easy hey.

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();

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

        public void InitializeContainer()
                    x => x.For<ILogger>()
                          .SetProperty(y => y.Port = 8080)

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

Hope this helps.


  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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: