Skip to content

Scopes and Contexts

hendryluk edited this page Feb 11, 2015 · 8 revisions

Ref: https://docs.jboss.org/weld/reference/latest/en-US/html/scopescontexts.html

There are a number of built-in scopes defined in the CDI specs. Cormo has implemented just a handful of them:

  • Pseudo-scopes:
    • [Dependent]
    • [Singleton]
  • Normal-scopes
    • [RequestScoped]. Currently only 1 implementation: Cormo.Web attaches this scope against Http request context. In near future Cormo.Messaging will also provide another implementation which attaches this scope against each message.
    • [SessionScoped]. Currently only 1 implementation: Cormo.Web attaches this scope against Http session. In near future Como.Messaging will also provide an implementation which attaches this scope within the context of an EIP flow (e.g. with ESB's session-variable or claim-check)
    • Custom scopes: developers can define and implement their own scopes and contexts. {TODO: tutorial)

Example:

public interface IMyComponent {}

[RequestScoped]
public class MyComponent: IMyComponent
{
}

[Singleton]
public class MyService
{
  [Inject] IMyComponent _myComponent;
}

Notice that unlike most IoC frameworks in .NET, CDI spec (and therefore Cormo) specifically allows the dependencies between different scoped components, in this case a request-scoped MyComponent into a singleton MyService. At runtime, MyService will only receive a proxy of IMyComponent. Whenever MyService communicates with the IMyComponent proxy, Cormo will direct the call to the correct underlying reference of MyComponent depending on the current http-request in which the call is made.

Clone this wiki locally