3

I have a question about MVC architecture.

I have a class that collects data from a WCF service, lets call it 'WCFDataAccess'. This class exposes many different methods to get or set data.

Inside of the Home Controller in MVC I often need to use this 'WCFDataAccess' class to collect data for each of the routes.

Index route would potentially create an instance of the WCFDataAccess object and collect data. A different route, say 'IndexDetails' also needs to create an instance of the WCFDataAccess object to collect data.

Can I share an instance of the 'WCFDataAccess' class by making it a private variable inside of the Home controller without negative performance impact? If two requests come in at the same time to the controller class how does using the private variable work?

An additional thought is to use the HttpContext.Current.Cache class.

Any suggestions?

Thanks!

4

1 回答 1

5

Making your object private within the controller does not make it shared across requests. A new instance of the object would be created for each request.

Is there a heavy operation when constructing this object that you want to make it shared? If it's simply instantiating an web service client that should be fairly lightweight and re-creating it per request should't cause any performance degradation.

于 2013-06-12T21:44:17.897 回答