根据基类注册类型 [英] Register types based on base class

查看:75
本文介绍了根据基类注册类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将温莎作为IOC容器。
我现在面临的问题是立即注册我的所有视图模型。

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

其中 ViewModelBase 是我的基类。

还尝试了以下操作:

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

必需的依赖项可以解析,而视图模型不能解析。
我想我这里缺少明显的东西吗?

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

编辑

我的依赖项注册如下:

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

更新

UPDATE

由于建议不起作用,因此我计划在此处添加我的基类和viewmodel中的代码。
这样做时,我注意到我的viewmodel-class是内部密封的。当将其更改为公共密封时,以上代码确实起作用。

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.

有人可以解释为什么内部类不能在容器中注册?
我已经用完全相同的设置测试了其他IOC容器,他们没有抱怨。

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.

推荐答案

当我添加选择时,您的注册示例在我的应用程序中开始正常运行组件服务。例如。 .WithService.AllInterfaces()

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

更新:

为了注册内部类型,应使用 .IncludeNonPublicTypes()

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>
{
}

这篇关于根据基类注册类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆