为什么 Microsoft 警告不要使用 PerRequestLifetimeManager
Why does Microsoft warn from using PerRequestLifetimeManager
Although the PerRequestLifetimeManager lifetime manager works correctly and can help in working with stateful or thread-unsafe dependencies within the scope of an HTTP request, it is generally not a good idea to use it when it can be avoided, as it can often lead to bad practices or hard to find bugs in the end-user's application code when used incorrectly. It is recommended that the dependencies you register are stateless and if there is a need to share common state between several objects during the lifetime of an HTTP request, then you can have a stateless service that explicitly stores and retrieves this state using the Items collection of the Current object.
警告指的是哪种错误或不良做法?怎么会用错呢? - 不幸的是,警告不是很具体,因此很难应用于现实世界。此外,我不清楚在这种情况下有状态意味着什么。
恕我直言,使用 PerRequestLifetimeManager 的典型场景是某种数据库连接(例如 DbContext)或类似的。
其目的是为每个请求只实例化一个实例,这可以(例如)防止在单个请求过程中进行冗余操作和查找。
危险在于,如果有人假设创建的对象是在请求期间存储状态的好地方。依赖注入的想法是 class 接收一个依赖项(通常是一个接口)并且 "know" 除了实现该接口之外什么都不做。
但有人可能会推断,如果对象要在请求的整个生命周期中持续存在,那么它是在请求期间维护状态的好地方。所以他们创建了一个复杂的场景,其中一个 class 接收到依赖项,在其中存储一些信息(比如设置一个 属性),然后另一个 class 接收到相同的依赖项并期望读取它属性。
现在依赖注入(解耦)的目的已经失败,因为 classes 已经内置了关于该依赖的生命周期的假设,甚至可能包括关于其他 classes 已经或将要处理该对象的状态。这会造成混乱,其中 classes 之间的交互很难被察觉 - 甚至是隐藏的 - 因此很容易被破坏。
假设有人确定该依赖项的生活方式应该是短暂的,而不是每个网络请求。突然间,依赖它的 classes 的所有行为都按预期停止工作。所以开发人员查看那些 classes 并发现没有任何变化。发生了什么?这些 classes 之间的交互最初很难看到,因此当它中断时,问题将很难找到。如果有一些合理的理由可以改变这种依赖的生活方式,那么问题将更难解决。
如果我们需要在请求期间存储状态,那么我们应该把它放在 "normal" 的地方,比如 HttpContext
。那里仍然存在一些令人困惑的做法和错误,但至少我们知道 HttpContext
(根据定义)将与特定请求相关联。
Although the PerRequestLifetimeManager lifetime manager works correctly and can help in working with stateful or thread-unsafe dependencies within the scope of an HTTP request, it is generally not a good idea to use it when it can be avoided, as it can often lead to bad practices or hard to find bugs in the end-user's application code when used incorrectly. It is recommended that the dependencies you register are stateless and if there is a need to share common state between several objects during the lifetime of an HTTP request, then you can have a stateless service that explicitly stores and retrieves this state using the Items collection of the Current object.
警告指的是哪种错误或不良做法?怎么会用错呢? - 不幸的是,警告不是很具体,因此很难应用于现实世界。此外,我不清楚在这种情况下有状态意味着什么。
恕我直言,使用 PerRequestLifetimeManager 的典型场景是某种数据库连接(例如 DbContext)或类似的。
其目的是为每个请求只实例化一个实例,这可以(例如)防止在单个请求过程中进行冗余操作和查找。
危险在于,如果有人假设创建的对象是在请求期间存储状态的好地方。依赖注入的想法是 class 接收一个依赖项(通常是一个接口)并且 "know" 除了实现该接口之外什么都不做。
但有人可能会推断,如果对象要在请求的整个生命周期中持续存在,那么它是在请求期间维护状态的好地方。所以他们创建了一个复杂的场景,其中一个 class 接收到依赖项,在其中存储一些信息(比如设置一个 属性),然后另一个 class 接收到相同的依赖项并期望读取它属性。
现在依赖注入(解耦)的目的已经失败,因为 classes 已经内置了关于该依赖的生命周期的假设,甚至可能包括关于其他 classes 已经或将要处理该对象的状态。这会造成混乱,其中 classes 之间的交互很难被察觉 - 甚至是隐藏的 - 因此很容易被破坏。
假设有人确定该依赖项的生活方式应该是短暂的,而不是每个网络请求。突然间,依赖它的 classes 的所有行为都按预期停止工作。所以开发人员查看那些 classes 并发现没有任何变化。发生了什么?这些 classes 之间的交互最初很难看到,因此当它中断时,问题将很难找到。如果有一些合理的理由可以改变这种依赖的生活方式,那么问题将更难解决。
如果我们需要在请求期间存储状态,那么我们应该把它放在 "normal" 的地方,比如 HttpContext
。那里仍然存在一些令人困惑的做法和错误,但至少我们知道 HttpContext
(根据定义)将与特定请求相关联。