views:

810

answers:

3

I am just starting porting an application to ASP.net MVC and I have an object holding application state (it keeps track of certain processes running on the machine, starting and stopping as necessary and sending/receiving MSMQ message).

Where should I keep this object? In my current application (based on HttpListener) it is a singleton, however I know singletons make testing difficult. It would be difficult to mock or test this object, at least in the context of the MVC application itself, and it has it's own set of tests outside the application anyway. However it may need to be replaced by a stub for testing.

The object needs to be made available to a number of controllers. Where should I store this object and how should I make it available to the controllers? I've never seen a case like this described in any ASP.net MVC examples I've seen.

UPDATE:

I guess I need to explain why I can't store this data in a database. First I must explain what the application does:

The application serves images that are generated dynamically by a number of "engines", which are processes running on the server, communicated to via MSMQ. Lets call the object I'm asking the question about the EngineManager. The process goes something like this:

  1. The client POSTs an XML request to the server, giving the name of "engine" to be used, as well as a number of parameters describing the image.
  2. The application checks the EngineManager to see if that engine is running. If not, it starts it.
  3. The application posts an MSMQ message to the engine and waits for the response.
  4. The application sends the generated image back to the client.
  5. If at any point the engine shuts down or crashes, the application must be aware of that so that it can be restarted on the next request to that engine.
  6. When the application shuts down, all engines are also shut down.

There are several controllers that handle these requests, each doing a slightly different job. All of them need to communicate with the same EngineManager, as it also needs to, in certain situations synchronise access to other resources.

As you can see, it's not your typical database-backed webserver.

A: 

Keep your application data in database and access this by model layer. Client keep session id only.

Soul_Master
I cannot keep this application data in a database. This is not a traditional database backed server - it communicates with a number of processes on the machine using MSMQ, and needs to start and stop these processes etc. I explained this in the question.
Groky
Sorry if I wasn't clear, I will add an explanation to the question.
Groky
Thanks. for help me.(L)
Soul_Master
+1  A: 

You should pass the object to the constructor of each Controller instance, and the controller action methods should all use the object instance passed in to the Controller instance constructor.

The default ControllerFactory which ships with ASP.NET MVC will not allow you to do this. However, there are free addon frameworks (the one I like is Autofac) which do permit this style of programming.

Justice
+2  A: 

If you want this object to be available to all users, i.e. it is not session specific, you could look at storing it in application state:

http://msdn.microsoft.com/en-us/library/bf9xhdz4(VS.71).aspx

However, application state has several disadvantages, as listed on the page linked above, so make sure these issues don't affect you before you go down that route. In general I steer clear from Applciation state and store application data in a backend DB. As you don't want to go down this route application state may be OK for you.

Steve Haigh