Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

castle windsor - Register types based on base class

I'm trying to figure out Windsor as an IOC container. The problem I'm facing right now is to register all of my viewmodels at once.

I've taken a look at the docs and thought that the following code should work. However, when I check the container afterwards, nothing is registered.

container.Register(Classes.FromThisAssembly()
                          .BasedOn<ViewModelBase>()
                          .LifestyleTransient());

where ViewModelBase is my baseclass.

Also tried the following:

container.Register(Classes.FromThisAssembly()
         .InSameNamespaceAs<MainWindowViewModel>()
         .LifestyleTransient()); 

The necessary dependencies can be resolved, the viewmodels not. I suppose I'm missing something obvious here?

Edit

My dependencies are registered as follows:

this.container.Register(Component.For<IDALHandler>().ImplementedBy<DALHandler>());
this.container.Register(Component.For<IBLHandler>().ImplementedBy<BLHandler>());

UPDATE

Since the suggestions didn't work, I was planning on adding the code from my baseclass and viewmodel here. While doing so I noticed that my viewmodel-class was internal sealed. When changing it to public sealed, the above code did work.

Can someone explain why internal classes can't be registered in the container? I've already tested other IOC containers with the exact same setup and they didn't complain about it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your example of registration started working well in my application when I added selection of the service for component. E.g. .WithService.AllInterfaces()

container.Register(Classes.FromThisAssembly()
    .BasedOn(typeof(MyBaseClass<>))
    .WithService.AllInterfaces()
    .LifestylePerWebRequest()
);

container.Register(Classes.FromThisAssembly()
    .InSameNamespaceAs<MyBaseClass>()
    .WithService.AllInterfaces()
    .LifestylePerWebRequest()
);

UPDATE:

In order to register internal types, .IncludeNonPublicTypes() should be used.

public class ExampleTest
{
    [Test]
    public void MyBaseClass_Base()
    {
        var target = new WindsorContainer();

        target.Register(Classes.FromThisAssembly()
            .IncludeNonPublicTypes()
            .BasedOn(typeof(MyBaseClass<>))
            .WithService.Base()
            //.LifestylePerWebRequest()
        );

        //assert
        target.Resolve<MyBaseClass<int>>().Should().BeOfType<A>();
        target.Resolve<MyBaseClass<string>>().Should().BeOfType<B>();
    }

    [Test]
    public void MyBaseClass_Self()
    {
        var target = new WindsorContainer();

        target.Register(Classes.FromThisAssembly()
            .IncludeNonPublicTypes()
            .BasedOn(typeof(MyBaseClass<>))
            .WithService.Self()
            //.LifestylePerWebRequest()
        );

        //assert
        target.Resolve<MyBaseClass<int>>().Should().BeOfType<MyBaseClass<int>>();
        target.Resolve<MyBaseClass<string>>().Should().BeOfType<MyBaseClass<string>>();
        target.Resolve<A>().Should().BeOfType<A>();
        target.Resolve<B>().Should().BeOfType<B>();
    }
}

internal class MyBaseClass<T>
{
}

internal class A : MyBaseClass<int>
{
}

internal class B : MyBaseClass<string>
{
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...