Home > Development > Castle Windsor Manual Registration Arrays

Castle Windsor Manual Registration Arrays


In previous post I have used the Parameters method when registering components manually. This is actually now obsolete and to set static values you should be using the DependsOn method.

Looking at the code below you can see the from and to parameters in the Notifier class constructor. Look at how the to parameter is a string array.  The DependsOn method allows you to assign any value wheres as Properties method only allows string based properties to be set so arrays and hashtables will not work out of the box. It is recommended on the website to use DependsOn going forwards.

using Castle.MicroKernel.Registration;
using Castle.Windsor;

namespace CastleWindsorDemos
{

    public interface INotifier
    {
        void Notify();
    }

    public class Notifier : INotifier
    {
        private readonly string[] to;
        private readonly string from;

        public Notifier(string from, string[] to)
        {
            this.to = to;
            this.from = from;
        }

        public void Notify()
        {
            // Put code to send to people wishing to be notified here
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            WindsorContainer container = new WindsorContainer();
            container.Register(Component.For<INotifier>()
                                        .ImplementedBy<Notifier>()
                                        .Named("notifier.default")
                                        .DependsOn(
                                            Property.ForKey("from").Eq("system.predicate@gmail.com"),
                                            Property.ForKey("to").Eq(new[] { "tom@hotmail.com", "fred@hotmail.com" })
                                         )
                                        .LifeStyle
                                        .Transient
                               );

            INotifier n = container.Resolve<INotifier>();

        }
    }
}

Hope this helps.

Blair….

Advertisements
Categories: Development Tags:
  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: