我只想就使用SqlConnection物体的适当使用或适当设计发表意见。 以下2种最佳用途:
一种数据提供者类别,其方法(其中每一种方法)含有SqlConnection物体(并在使用时处理)。 和:
IList<Employee> GetAllEmployees()
{
using (SqlConnection connection = new SqlConnection(this.connectionString)) {
// Code goes here...
}
}
Employee GetEmployee(int id)
{
using (SqlConnection connection = new SqlConnection(this.connectionString)) {
// Code goes here...
}
}
或
SqlConnection connection; // initialized in construct或
IList<Employee> GetAllEmployees()
{
this.TryOpenConnection(); // tries to open member SqlConnection instance
// Code goes here...
this.CloseConnection();
// return
}
Employee GetEmployee(int id)
{
this.TryOpenConnection(); // tries to open member SqlConnection instance
// Code goes here...
this.CloseConnection();
// return
}
Or is there a better approach than this? I have a focused web crawler type of application and this application will crawl 50 或 m或e websites simultaneously (multithreaded) with each website contained in a crawler object and each crawler object has an instance of a data provider class (above).