2011-08-16 30 views
1

我有多個用於進行數據庫訪問的類,每個類都包含一組用於從數據庫提取信息並將其傳遞到另一個對象的方法。問題是大多數方法都會提取到不同的對象中。什麼將提取到的接口這幫助收拾我的代碼,減少數據庫連接的這樣的數字提取到接口

例如類

public class RequestDB 
    { 

    public RequestDB(string connectionStringName) 
    { 
     connectionString = connectionStringName; 
    } 


    public void UpdateRequest(AssignmentRequest request) 
    { 
     SqlConnection con = new SqlConnection(connectionString); 
     SqlCommand cmd = new SqlCommand("UpdateAssignmentRequest", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.Add(new SqlParameter("@assignmentRequestId", SqlDbType.Int)); 
     cmd.Parameters["@assignmentRequestId"].Value = request.RequestID; 
     cmd.Parameters.Add(new SqlParameter("@jobTitle", SqlDbType.VarChar)); 
     ... 
     try 
     { 
      // Move to a cache class library 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
     } 
     catch (SqlException ex) 
     { 
      Log.Write("Exception", "Unknown", "RequestDB", "UpdateRequest", request.ToString(), ex.Message); 

      throw new ApplicationException("Data source error."); 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 



    public int RequestAssignment(AssignmentRequest request) 
    { 
     int result = 0; 
     SqlConnection con = new SqlConnection(connectionString); 
     SqlCommand cmd = new SqlCommand("RequestAssignment", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.Add(new SqlParameter("@clientUserId", SqlDbType.Int)); 
     cmd.Parameters["@clientUserId"].Value = request.RequestingUserID; 
     ... 
     try 
     { 
      // Move to a cache class library 
      con.Open(); 
      var reader = cmd.ExecuteReader(); 
      reader.Read(); 
      result = reader.GetInt32(reader.GetOrdinal("id")); 
     } 
     catch (SqlException ex) 
     { 
      Log.Write("Exception", "Unknown", "RequestDB", "RequestAssignment", request.ToString(), ex.Message); 

      throw new ApplicationException("Data source error."); 
     } 
     finally 
     { 
      con.Close(); 
     } 
     return result; 
    } 

    public List<AssignmentRequest> GetAllAssignmentRequests() 
    { 
     var result = new List<AssignmentRequest>(); 
     SqlConnection con = new SqlConnection(connectionString); 
     SqlCommand cmd = new SqlCommand("GetAssignmentRequests", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     try 
     { 
      // Move to a cache class library 
      con.Open(); 
      var reader = cmd.ExecuteReader(); 
      while (reader.Read()) 
      { 
       result.Add(new AssignmentRequest() 
           { 
            RequestID = reader.GetInt32(reader.GetOrdinal("requestId")), 
            RequestingUserID = reader.GetInt32(reader.GetOrdinal("clientUserId")), 
            ... 
           }); 
      } 
     } 
     catch (SqlException ex) 
     { 
      Log.Write("Exception", "Unknown", "RequestDB", "GetAllAssignmentRequests", "", ex.Message); 

      throw new ApplicationException("Data source error."); 
     } 
     finally 
     { 
      con.Close(); 
     } 
     return result; 
    } 

    public AssignmentRequest GetAssignmentRequestById(int requestId) 
    { 
     var result = new AssignmentRequest(); 
     SqlConnection con = new SqlConnection(connectionString); 
     SqlCommand cmd = new SqlCommand("GetAssignmentRequestByID", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.Add(new SqlParameter("@requestId", SqlDbType.Int)); 
     cmd.Parameters["@requestId"].Value = requestId; 
     try 
     { 
      // Move to a cache class library 
      con.Open(); 
      var reader = cmd.ExecuteReader(); 
      reader.Read(); 
       result = new AssignmentRequest() 
       { 
        RequestID = reader.GetInt32(reader.GetOrdinal("requestId")), 
        RequestingUserID = reader.GetInt32(reader.GetOrdinal("clientUserId")), 
        JobTitle = reader.GetString(reader.GetOrdinal("jobTitle")), 
        Department = reader.GetString(reader.GetOrdinal("department")), 
        ... 
       }; 

     } 
     catch (SqlException ex) 
     { 
      Log.Write("Exception", "Unknown", "RequestDB", "GetAssignmentRequestById", requestId.ToString(), ex.Message); 

      throw new ApplicationException("Data source error."); 
     } 
     finally 
     { 
      con.Close(); 
     } 
     return result; 
    } 


    public List<AssignmentRequest> GetRequestHistoryByClientUserId(int clientUserId) 
    { 
     var result = new List<AssignmentRequest>(); 
     SqlConnection con = new SqlConnection(connectionString); 
     SqlCommand cmd = new SqlCommand("GetRequestHistoryByClientUserId", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@clientUserId", clientUserId); 
     try 
     { 
      // Move to a cache class library 
      con.Open(); 
      var reader = cmd.ExecuteReader(); 
      while (reader.Read()) 
      { 

       var request = new AssignmentRequest() 
            { 
             RequestID = reader.GetInt32(reader.GetOrdinal("requestId")), 
             RequestingUserID = reader.GetInt32(reader.GetOrdinal("clientUserId")), 
             JobTitle = reader.GetString(reader.GetOrdinal("jobTitle")), 
             ... 
       result.Add(request); 

      } 
     } 
     catch (SqlException ex) 
     { 
      Log.Write("Exception", "Unknown", "RequestDB", "GetRequestHistoryByClientUserId", "", ex.Message); 

      throw new ApplicationException("Data source error."); 
     } 
     finally 
     { 
      con.Close(); 
     } 
     return result; 
    } 

} 

回答

1

使用仿製藥,什麼最好的辦法:

public interface Repository<K, T> { 
    List<T> find(); 
    T find(K key); 
    K save(T value); 
    void update(T value); 
    void delete(T value); 
} 
0

我看到過幾個類似的問題。我的建議是不要急於進行界面設計。

這些類來自您自己的代碼?可以修改嗎?

我發現你的問題的一個很好的替代技術,而不是急於接口,是從共享公共行爲(方法,屬性)的類/對象中創建一個超類或一組超類。之後,他們可以遷移到一個界面。

步驟1(如何現在類):

public class MSSQLConnection 
{ 
    public void DoSomething() { ... } 
} 

public class XMLConnection 
{ 
    public void TrySomething() { ... } 
} 

public class OracleConnection 
{ 
    public void MakeSomething() { ... } 
} 

步驟2(重命名/重構編號相同。):

public class MSSQLConnection 
{ 
    public void DoSomething() { ... } 
} 

public class XMLConnection 
{ 
    public void DoSomething() { ... } 
} 

public class OracleConnection 
{ 
    public void DoSomething() { ... } 
} 

步驟3(請共同superclasss):

public class BaseConnection 
{ 
    public virtual void DoSomething() { ... } 
} 

public class MSSQLConnection: BaseConnection 
{ 
    public override void DoSomething() { ... } 
} 

public class XMLConnection: BaseConnection 
{ 
    public override void DoSomething() { ... } 
} 

public class OracleConnection: BaseConnection 
{ 
    public override void DoSomething() { ... } 
} 

乾杯。

0

使用泛型來創建duffymo指出的通用接口。 要共享各個不同的數據庫訪問類 使用構造函數注入數據庫連接:

public class ExampleRepository1 
{ 
    private SqlConnection _conn; 
    public ExampleRepository(SqlConnection conn) 
    { 
    _conn = oConnection; 
    } 
} 

public class ExampleRepository2 
{ 
    private SqlConnection _conn; 
    public ExampleRepository2(SqlConnection conn) 
    { 
    _conn = conn; 
    } 
} 

// Share the database connection: 

var conn = new SqlConnection(...); 

try 
{ 
    conn.Open(); 

    // You could also begin a transaction here 
    // Use conn.BeginTransation() 

    var repo1 = new ExampleRepository1(conn); 
    var repo2 = new ExampleRepository2(conn); 

    repo1.Find(...); 
    repo2.Delete(...) 
} 
finally 
{ 
    conn.Close(); 
}