Bring a data module to work with --- pass-Store - use a connection inside a reusable data module
I want to split a site data module into an assembly (single dll file), What is the best way to get-save and pass the site's ConnectionString when working with a web application. Inside the data assembly, I created a static class called ConnectionManager. It has a property called DatabaseConnectionName that I want to pass and save the connection which is inside the Web.Config file. In this strategy, I decided to get the name and connect while loading the website in the Global.asax file and work on the property I mentioned earlier (DatabaseConnectionName). But, this is just the strategy I used, I don't know what the general pattern is to accomplish this task.
Pieces of code: ======================================= = = -------- Global.asax ------------]
the code in the site that makes the Data module accessible for the site
void Application_Start(object sender, EventArgs e)
{
OurCompany.Data.ConnectionManager.DatabaseConnectionName = "MasterConnection";
}
[------------ Class ConnectionManager ------------] this is in the data module besides the site
public static class ConnectionManager
{
public static SqlConnection GetMasterConnection()
{
string connectionString = ConfigurationManager.ConnectionStrings[**DatabaseConnectionName**].ConnectionString;
SqlConnection conn;
//conn.Open();
conn = new SqlConnection(connectionString);
return conn;
}
private static string **databaseConnectionName**;
public static string DatabaseConnectionName
{
get
{
return databaseConnectionName;
}
set
{
databaseConnectionName = value;
}
}
== END ============================================== =========================================== ======= ========
--- Questions: ---
-
Where to store the connection? (there was a property inside the ConnectionManager class, theCompany.Data.ConnectionManager.DatabaseConnectionName)
-
When creating this connection? (here was the load time of the Global.asax app)
-
Which method is best for storing this information: SessionState or ViewState or a simple property
-
Is this strategy a good one? Do you know a better way or general pattern for this?
Thanks for any information - MHM -
source to share
A few thoughts ...
-
Can't store and connect to an open database connection. Open the connection, perform database operations, then close immediately. Apply the receive rule later, release early.
-
See point 1.
-
Do not store database connections in session state. See point 1 again. If you mean the connection string, then just read it from the config manager when you need it. It's already cached for you, don't reinvent and wind the wheel.
-
I suggest you take a look at patterns and use an enterprise library that abstracts many common patterns of data access control:
source to share