Skip to main content
  • I am not sure if this is the recommended way to insert the session into the controller — Use constructor injection, as long as session is required for controller. Your "composition root" should be able to do this. And you had provided an example.

  • I was thinking about using DI but i am not sure how to inject the session that is opened and binded in the BeginRequest delegate into the Controllers constructor to get this — usually DI containers are configured with "scope" abstraction. There are several default scopes for most of DI containers, among them are Signleton, Transient, Thread and Request (for HTTP requests). Last one will create one instance of dependency per HTTP request, and injects it into all objects that handle this request. As soon as HTTP Request will be done, DI should dispose all dependencies also. So, there is no need to explicitely create, handle and dispose session yourself.

I am not sure if this is the recommended way to insert the session into the controller

Use constructor injection, as long as session is required for controller. Your "composition root" should be able to do this. And you had provided an example.

I was thinking about using DI but i am not sure how to inject the session that is opened and binded in the BeginRequest delegate into the Controllers constructor to get this

Usually DI containers are configured with "scope" abstraction. There are several default scopes for most of DI containers, among them are Signleton, Transient, Thread and Request (for HTTP requests). Last one will create one instance of dependency per HTTP request, and injects it into all objects that handle this request. As soon as HTTP Request will be done, DI should dispose all dependencies also. So, there is no need to explicitely create, handle and dispose session yourself.

  • I am not sure if this is the recommended way to insert the session into the controller — Use constructor injection, as long as session is required for controller. Your "composition root" should be able to do this. And you had provided an example.

  • I was thinking about using DI but i am not sure how to inject the session that is opened and binded in the BeginRequest delegate into the Controllers constructor to get this — usually DI containers are configured with "scope" abstraction. There are several default scopes for most of DI containers, among them are Signleton, Transient, Thread and Request (for HTTP requests). Last one will create one instance of dependency per HTTP request, and injects it into all objects that handle this request. As soon as HTTP Request will be done, DI should dispose all dependencies also. So, there is no need to explicitely create, handle and dispose session yourself.

I am not sure if this is the recommended way to insert the session into the controller

Use constructor injection, as long as session is required for controller. Your "composition root" should be able to do this. And you had provided an example.

I was thinking about using DI but i am not sure how to inject the session that is opened and binded in the BeginRequest delegate into the Controllers constructor to get this

Usually DI containers are configured with "scope" abstraction. There are several default scopes for most of DI containers, among them are Signleton, Transient, Thread and Request (for HTTP requests). Last one will create one instance of dependency per HTTP request, and injects it into all objects that handle this request. As soon as HTTP Request will be done, DI should dispose all dependencies also. So, there is no need to explicitely create, handle and dispose session yourself.

Source Link
Akim
  • 1k
  • 8
  • 16

  • I am not sure if this is the recommended way to insert the session into the controller — Use constructor injection, as long as session is required for controller. Your "composition root" should be able to do this. And you had provided an example.

  • I was thinking about using DI but i am not sure how to inject the session that is opened and binded in the BeginRequest delegate into the Controllers constructor to get this — usually DI containers are configured with "scope" abstraction. There are several default scopes for most of DI containers, among them are Signleton, Transient, Thread and Request (for HTTP requests). Last one will create one instance of dependency per HTTP request, and injects it into all objects that handle this request. As soon as HTTP Request will be done, DI should dispose all dependencies also. So, there is no need to explicitely create, handle and dispose session yourself.