简体   繁体   English

如何在Nancy自托管项目中使用Castle Windsor实施DI

[英]How to implement DI by using Castle Windsor in Nancy Self Hosted Project

I'm developing web application with Nancy framework. 我正在使用Nancy框架开发Web应用程序。 In Console Application (background service/daemon) I use Castle Windsor DI container and use the next initialization code: 在控制台应用程序(后台服务/守护程序)中,我使用Castle Windsor DI容器并使用下一个初始化代码:

_container = new WindsorContainer ();
_container.Register (Component.For (typeof(IRepository<>)).ImplementedBy (typeof(Repository<>)).LifeStyle.Transient);
_container.Register (Component.For<ILog> ().ImplementedBy<FileConsoleLog> ());

But I didn't know how to rewrite this code with using TinyIOC. 但是我不知道如何使用TinyIOC重写此代码。 I try to use WindsorNancyBootstrapper DI implementation. 我尝试使用WindsorNancyBootstrapper DI实现。 In this way I catch next problem. 这样,我就遇到了下一个问题。 How i can rewrite this? 我该如何重写呢?

   protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        base.ConfigureApplicationContainer (container);

        container.Register<IEnumerable<ISuperSimpleViewEngineMatcher>> ((c, p) => {
            return new List<ISuperSimpleViewEngineMatcher> () 
            { 
                new StringTranslateTokenMatcher () 
            };
        });
    }

How I can rewrite first or second code, using one of the DI containers? 如何使用一个DI容器重写第一或第二代码? Thank you! 谢谢!

You install the Nancy Windsor Bootstrapper NuGet package: 您安装Nancy Windsor Bootstrapper NuGet软件包:

Install-Package Nancy.Bootstrappers.Windsor

Then you create a Bootstrapper which inherits from WindsorNancyBootstrapper , and do your Windsor configuration there: 然后,创建一个继承自WindsorNancyBootstrapperBootstrapper ,并在其中进行Windsor配置:

public class Bootstrapper : WindsorNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(IWindsorContainer container)
    {
        container = new WindsorContainer();
        container.Register(Component.For(typeof(IRepository<>)).ImplementedBy(typeof(Repository<>)).LifeStyle.Transient);
       _container.Register(Component.For<ILog>().ImplementedBy<FileConsoleLog>());
    }
}

See the docs for more info. 有关更多信息,请参阅文档

The next code was added. 添加了下一个代码。

protected override void ConfigureRequestContainer(
            Nancy.TinyIoc.TinyIoCContainer container, Nancy.NancyContext context)
        {
            base.ConfigureRequestContainer (container, context);
            container.Register<IUserMapper, SiteUserMapper> ();

            container.Register (typeof(IRepository<>), typeof(Repository<>)).AsMultiInstance ();
            container.Register (typeof(ILog), typeof(DatabaseLog));
            container.Register (typeof(IServiceGeo), typeof(ServiceGeo));
            container.Register (typeof(IServiceUser), typeof(ServiceUser));
        }

TinyIoc can register generic TinyIoc可以注册通用

 container.Register (typeof(IRepository<>), typeof(Repository<>)).AsMultiInstance ();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM