开发者

Castle Windsor unable to resolve classes which implement WPF's ICommand

I am seeing some strange behaviour whilst trying to register WPF ICommands using Castle Windsor.

When running the included code, I get the following error:

Can't create component 'ConsoleApplication1.TestParent' as it has dependencies to be satisfied.

ConsoleApplication1.TestParent is waiting for the following dependencies:

Keys (components with specific keys开发者_JAVA技巧)

testChild which was not registered.

However if I change the interface on TestChild from ICommand to ITest then the code works just fine.

Has anyone else witnessed this behaviour and/or know how to resolve it?

I am using VS2008, .NET 3.5 and Castle 2.5.2.

Thanks,

Stuart  

    using System; 
    using System.Windows.Input; 
     
    using Castle.MicroKernel.Registration; 
    using Castle.Windsor; 
 
    namespace ConsoleApplication1 
    { 
        class Program 
        { 
            static void Main(string[] args) 
            { 
                var container = new WindsorContainer(); 
                 
                container.Register( 
                    Component.For<TestParent>().ImplementedBy<TestParent>(), 
                    Component.For<TestChild>().ImplementedBy<TestChild>() 
                    ); 
 
                var parent = container.Resolve<TestParent>(); 
            } 
        } 
 
        public interface ITest {} 
 
        public class TestParent 
        { 
            public TestParent(TestChild testChild) { } 
        } 
 
        public class TestChild : ICommand 
        { 
            public event EventHandler CanExecuteChanged; 
            public bool CanExecute(object parameter) { return true; } 
            public void Execute(object parameter) {} 
        } 
    } 


I am seeing some strange behaviour whilst trying to register WPF ICommands using Castle Windsor.

When running the included code, I get the following error:

Can't create component 'ConsoleApplication1.TestParent' as it has dependencies to be satisfied.
ConsoleApplication1.TestParent is waiting for the following dependencies:

Keys (components with specific keys)
- testChild which was not registered.

However if I change the interface on TestChild from ICommand to ITest then the code works just fine.

Has anyone else witnessed this behaviour and/or know how to resolve it?

I am using VS2008, .NET 3.5 and Castle 2.5.2.

Thanks, Stuart

using System;
using System.Windows.Input;  
using Castle.MicroKernel.Registration;  
using Castle.Windsor; 
namespace ConsoleApplication1      
{          
    class Program          
    {              
        static void Main(string[] args)              
        {                  
            var container = new WindsorContainer();

            container.Register(                      
                Component.For().ImplementedBy(),                      
                Component.For().ImplementedBy()                      
            ); 

            var parent = container.Resolve();
        }
    }

    public interface ITest {}

    public class TestParent
    {              
        public TestParent(TestChild testChild) { }
    }  

    public class TestChild : ICommand          
    {              
        public event EventHandler CanExecuteChanged;              
        public bool CanExecute(object parameter) { return true; }              
        public void Execute(object parameter) {}          
    }      
}    


Ignoring the fact that your code sample does not compile due to incorrect calling of Windsor registration, could it be you could want something like this?

class Program          
{              
    static void Main(string[] args)              
    {                  
        var container = new WindsorContainer();

        container.Register(
            Component.For<TestParent>(),
            Component.For<ICommand>().ImplementedBy<TestChild>()
        );

        var parent = container.Resolve<TestParent>();
    }
}

public class TestParent
{              
    public TestParent(ICommand testChild) { }
}  

public class TestChild : ICommand          
{              
    public event EventHandler CanExecuteChanged;              
    public bool CanExecute(object parameter) { return true; }              
    public void Execute(object parameter) {}          
} 

}

Note that I removed the ITest interface definition, no clue what that was used for.


the TestParent class has only one constructor, which requires passing an object parameter. If it were a basic data type like int or string, you could specify its value in the App.config file. But you can't set objects there. In this case, I recommend you add a blank constructor (without parameters).


Thanks for the replies.

Since the corporate filter at work refuses to display the "Captcha" human verification I had to cut and paste from my iphone... anyway this somehow removed the generics from the code sample so apologies for the lack of compilation not to mention the fubar layout...

Should look like:

    private static void Main(string[] args)
    {
        var container = new WindsorContainer();

        container.Register(
            Component.For<TestParent>().ImplementedBy<TestParent>(),
            Component.For<TestChild>().ImplementedBy<TestChild>());

        var parent = container.Resolve<TestParent>();
    }

Registering ICommand does work, however it doesn't solve my problem since I have multiple commands in my real app and I need to Castle to distinguish between them.

I have managed to work around this by creating dummy interfaces for every one of the commands and registering against those - not ideal.

The ITest interface was to allow switching from ICommand and ITest which resolves the issue - i.e. the issue is specific to WPF ICommand.

Stuart

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜