ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

首页> C#> NancyFx的ConfigureRequestContainer

2019-10-28 11:07:04  阅读:187  来源: 互联网

标签:nancy tinyioc c


我试图弄清楚NancyFx请求容器是如何工作的,所以我创建了一个小测试项目.

我创建了这个界面

public interface INancyContextWrapper
{
    NancyContext Context { get; }
}

通过此实现

public class NancyContextWrapper : INancyContextWrapper
{
    public NancyContext Context { get; private set; }

    public NancyContextWrapper(NancyContext context)
    {
        Context = context;
    }
}

然后在bootstrapper中我这样注册

protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
{
    base.ConfigureRequestContainer(container, context);
    container.Register<INancyContextWrapper>(new NancyContextWrapper(context));
}

我在不执行任何操作但只将请求url作为字符串返回的类中使用此上下文包装器.

public interface IUrlString
{
    string Resolve();
}

public class UrlString : IUrlString
{
    private readonly INancyContextWrapper _context;

    public UrlString(INancyContextWrapper context)
    {
        _context = context;
    }

    public string Resolve()
    {
        return _context.Context.Request.Url.ToString();
    }
}

最后在模块中使用它

public class RootModule : NancyModule
{
    public RootModule(IUrlString urlString)
    {
        Get["/"] = _ => urlString.Resolve();
    }
}

当我这样做时,请求始终为null.现在,我或多或少地发现,由于未在请求容器配置中配置IUrlString,因此TinyIoc在应用程序启动时已在任何请求发出之前解析了INancyContextWrapper,并且TinyIoc不会重新注册依赖项,因为依赖项图依赖于已配置的内容在请求容器配置中.

我的问题是使用ConfigureRequestContainer的最佳实践是什么?我是否必须在请求容器配置中显式注册所有以任何方式依赖NancyContext的内容?那会很快变得肿并且难以维护.我喜欢TinyIoc如何进行程序集扫描,因此必须这样做有点挫折.

解决方法:

假设上面的示例只是您实际想要的简化,即出于某种目的在请求生存期中随身携带的nancy上下文,您最好不要使用引导程序,因为它取决于使用过的IoC容器.

意见建议:

将包装器的实现更改为不使用ctor,而是使用属性设置器(您始终可以编码,以便该属性只能设置一次):

public interface INancyContextWrapper
{
    NancyContext Context { get; set; }
}

public class NancyContextWrapper : INancyContextWrapper
{
    private NancyContext _context;
    public NancyContext Context 
    { 
           get {return _context;} 
           set {_context = value;} //do something here if you want to prevent repeated sets
    }
}

与其直接使用容器和引导程序,不如使用IRegistration实现(它们由nancy使用,并且与容器无关)

public class NancyContextWrapperRegistrations : IRegistrations
{
    public IEnumerable<TypeRegistration> TypeRegistrations 
    {
        get 
        { 
            return new[]
            {
                new TypeRegistration(typeof(INancyContextWrapper), typeof(NancyContextWrapper), Lifetime.PerRequest),
                new TypeRegistration(typeof(IUrlString .... per request
            };
               // or you can use AssemblyTypeScanner, etc here to find
        }    

        //make the other 2 interface properties to return null
    }
}

使用IRequestStartup任务(nancy也会自动发现它们)来设置上下文

public class PrepareNancyContextWrapper : IRequestStartup
{
    private readonly INancyContextWrapper _nancyContext;
    public PrepareNancyContextWrapper(INancyContextWrapper nancyContext)
    {
        _nancyContext = nancyContext;
    }

    public void Initialize(IPipelines piepeLinse, NancyContext context)
    {
         _nancyContext.Context = context;
    }
}

尽管以上内容看起来有些过头,但是这是一种以IoC独立的方式组织类型注册的好方法(即,如果您用其他东西代替TinyIoC,则无需接触引导程序等)

同样,这是一种很好的方式来控制在请求(或如果需要的话,应用程序)启动期间发生的情况,而不会覆盖引导程序中的任何内容,并且它将与您决定使用的任何引导程序/容器一起工作.

标签:nancy,tinyioc,c
来源: https://codeday.me/bug/20191028/1952025.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有