开发者

The difference between context and scope

开发者 https://www.devze.com 2023-03-17 04:55 出处:网络
Studying JSR-299, I read section 5.1 of the Weld reference which explains开发者_开发知识库 how scopes work in CDI. Apparently, context is a concept closely related to scope. The difference is not clea

Studying JSR-299, I read section 5.1 of the Weld reference which explains开发者_开发知识库 how scopes work in CDI. Apparently, context is a concept closely related to scope. The difference is not clear in my mind and I feel tempted to even use the words interchangeably.

What is the difference between scope and context? What is the relationship between the two concepts?


Each scope has a separate context.

The context is a container for beans which have been defined with the respective scope.

That's why context implementations carry the name of the scope - ApplicationContext, DependentContext, RequestContext, etc.

This is an implementation detail actually - as a user of CDI you should only know about scopes (because you define it for your beans), and the implementation decides where to place those beans and how to manage their lifecycle.


My understanding is that scope refers to where an object may be accessed from, while context enumerates the objects that can be accessed from some particular point in program execution. (That is, we talk about the scope of an object, and the context at some particular point in program execution.)

Mathematically speaking, both describe the can-access relation, but look at it in different directions.


First, we have concepts in our heads like applications, sessions, requests. Let's use the session concept in the following examples.

If we consider that a piece of execution is serving for a particular session, we'll say the session is part of the context of the execution; or, it is the session context of the execution.

A session has some variables, e.g. userName; we'll say the session is the scope of these variables.

Since both are pointing to the same session, it can get confusing. For example,

get the userName from the session context
get the userName from the session scope

both sound fine, because we are talking about an execution on a variable.

The following example is intelligible per the definition of scope

the scope of the injected bean is Session

but we don't have problem understanding what's really going on. If we want to, we can expand it till it's based on basic usages of words; we don't do that because it will be very verbose.

An author faces the difficult task of packing the words succinctly yet expecting readers somehow understand the complex meaning. Texts about context and scope usually appear to be gibberish to those who haven't understood the concepts.

API names are even more difficult to come up with, because codes are not English sentences. Context or Scope are pretty much interchangeable. If there's only one object representing a session, the class probably should be named just Session. If we split the part about variable manipulation, that part can be called SessionScope. However, the meaning of SessionContext is too elusive, the best we can tell, from the name alone, is that it is about something of a session - "context" here is pretty much an expletive.

0

精彩评论

暂无评论...
验证码 换一张
取 消