I'm having a little problem with property injection when the expected 
dependency uses DynamicProperties to fill constructor parameters.
In the attached sample, Foo has a property of type IConnector (which 
obviously connects somewhere to do some stuff; completely optional tho, 
like some remote logging or whatever it may be).

When using the DynamicParameters way of initializing the component 
providing the IConnector service, the container tells me that the instance 
might not resolve properly. Probably as a result, foo.Connector is null, 
even tho container.Resolve<IConnector>() will return a usable component.
Internally, it seems that its CurrentState is still WaitingDependency and 
therefore not returned.
This only happens when using property injection. If I change this to use 
constructor injection, I do get my IConnector initialized (but then it 
becomes required, while I still want it optional).

Changing this to use UsingFactoryMethod, the diagnostic goes away and 
foo.Connector is initialized correctly.

Can I somehow ask the container to try and resolve foo.Container anyways, 
even tho its State is still WaitingDependency?
I'd like to avoid using UsingFactoryMethod (because the component may 
require other services and not just simple parameters), with the hope that 
DynamicParameters/DependsOn will soon allow me to specify another component 
as source for those simple parameters.

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to castle-project-users+unsubscr...@googlegroups.com.
To post to this group, send email to castle-project-users@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


?//#define USING_FACTORY_METHOD
using System;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

namespace ConsoleApplication1
{
    public interface IConnector { }
    public class ServerConnector : IConnector
    {
        public ServerConnector(string serverName, int port) { }
    }
    public interface IConfiguration
    {
        string ServerName { get; }
        int Port { get; }
    }
    class ConfigWatcher : IConfiguration
    {
        public ConfigWatcher(string configFileName)
        {
            //watch configFileName, update settings etc.
        }
        public string ServerName { get; set; }
        public int Port { get; set; }
    }
    public class Foo
    {
        public IConnector Connector { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var container = new WindsorContainer()
                .Register(Component.For<IConfiguration>().Instance(new ConfigWatcher("some.config")))
                .Register(Component.For<IConnector>().ImplementedBy<ServerConnector>().LifestyleTransient()
#if USING_FACTORY_METHOD
                    .UsingFactoryMethod((k, d) => new ServerConnector(k.Resolve<IConfiguration>().ServerName, k.Resolve<IConfiguration>().Port)))
#else
                    .DynamicParameters((k, d) =>
                    {
                        d["serverName"] = k.Resolve<IConfiguration>().ServerName;
                        d["port"] = k.Resolve<IConfiguration>().Port;
                    }))
#endif
                .Register(Component.For<Foo>().LifestyleTransient());

            var foo = container.Resolve<Foo>();
            Console.WriteLine("foo.Connector is {0}null", foo.Connector == null ? "" : "not ");
        }
    }
}

Reply via email to