Best practices for storing Server object between Controller calls

I am using a 3rd party SDK to connect to a server running the corresponding 3rd party database.

When a user accesses my ASP.NET MVC 4.0 website he will not have to provide credentials; the connection is anonymous and the Server object is instantiated at each call to the Controller.

This is slow as instantiating this Server object takes several seconds in itself. What I really want is to create the object once and reuse it. I know I can use session variables to achieve this, but it is not an elegant solution and presents a problem as session variables expire.

Is there a way better way to do this?

Upvotes: 0

Views: 80

Answers (1)

Mikolaytis
Mikolaytis

Reputation: 962

If you will create class with static variable with connection like that

class DB
{
    public static <Type> DB {get;set;}
}

You will have issues because all users will use single DB connection. So you need to create class with static list of objects, that will contain DB connection and UserID. And you need to create methods:

AddConnectionForUser(<Type> UserID)//on login
RemoveConnectionForUser(<Type> UserID)//on logoff
GetConnectionForUser(<Type> UserID)

Upvotes: 1

Related Questions