- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图通过使用一些 ASP.NET MVC 对象,特别是 HttpContextWrapper,使现有的 ASP.NET Web 表单应用程序更易于单元测试。我看过它的用法示例,它们总是创建一个新对象。我用 Reflector 反汇编源代码,看到它所做的只是存储传递的 HttpContext。但是我很好奇始终创建 HttpContextWrapper 的新实例或以某种方式遵循单例模式是否安全?下面是我在我的应用程序中使用的类
public static class AppHttpContext {
public static HttpContextBase Current { get { return Getter(); } }
public static void SetContext(Func<HttpContextBase> getter) {
Getter = getter;
}
private static Func<HttpContextBase> Getter = () => new HttpContextWrapper(HttpContext.Current);
}
我使用它类似于 HttpContext.Current
AppHttpContext.Current.Session["blah"] = "something";
最佳答案
您的 AppHttpContext
类非常棒。它完美地抽象了 HttpContext 并允许进行单元测试。使用 HttpContextWrapper
是安全的。作为一项改进,您可以使此类成为非静态类,而不是使用静态 SetContext
方法,您可以将委托(delegate)注入(inject)构造函数。然后所有需要使用上下文的类(通常那些应该只限于您的网络表单页面)可以采用 AppHttpContext
的实例。
关于c# - 始终创建一个新的 HttpContextWrapper 是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3990879/
我试图通过使用一些 ASP.NET MVC 对象,特别是 HttpContextWrapper,使现有的 ASP.NET Web 表单应用程序更易于单元测试。我看过它的用法示例,它们总是创建一个新对象
我一直在清理我们的 Controller 代码以使每个操作都可测试。一般而言,这并不太难——在我们有机会使用固定对象(例如 FormsAuthentication)的情况下,我们通常会适本地引入某种形
我知道 HttpContext 之间的区别和 HttpContextWrapper在下面... HttpContext 这是老式的 asp.net 上下文。这样做的问题是它没有基类并且不是虚拟的,因此
我是一名优秀的程序员,十分优秀!