且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC:HttpContext的和依赖注入

更新时间:2023-02-20 13:06:46

有接口抽象HttpContext.Current。只露出需要的方法。 GetUserName()将要求HttpContext.Current.User.Identity.Name在执行,例如。作出这样的尽可能地薄。

Have an interface abstract HttpContext.Current. Expose only the methods you need. GetUserName() would call HttpContext.Current.User.Identity.Name in the implementation, for example. Make that as thin as possible.

采取这一抽象和它注入到你的其他供应商类。这将允许你通过嘲讽HTTP上下文抽象测试供应商。作为一个附带的好处,你可以做其他时髦的东西与HttpContext的抽象,除了嘲笑它。重用它,一件事。添加泛型类型PARAMS到袋等。

Take that abstraction and inject it into your other provider class. This will allow you to test the provider by mocking the http context abstraction. As a side benefit, you can do other nifty things with that HttpContext abstraction besides mock it. Reuse it, for one thing. Add generic type params to bags, etc.