我有很多类具有与以下相同的结构,我想在其中实现一个接口。那么,如果有人有解决方案吗?
public static class ClientDAL
{
private static string connString = ConfigurationManager.ConnectionStrings["default"].ToString();
public static ClientEntity NewClient(ClientEntity client)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "InsertNewClient";
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@ClientID", client.ClientID);
cmd.Parameters.AddWithValue("@ClientName", client.ClientName);
cmd.Parameters.AddWithValue("@ClientNif", client.ClientNIF);
cmd.Parameters.AddWithValue("@ClientAddress", client.ClientAddress);
cmd.Parameters.AddWithValue("@ClientCountry", client.ClientCountry);
cmd.Parameters.AddWithValue("@ClientProvincy", client.ClientProvincy);
cmd.Parameters.AddWithValue("@ClientCity", client.ClientCity);
cmd.Parameters.AddWithValue("@ClientPhone", client.ClientPhone);
cmd.Parameters.AddWithValue("@ClientMobile", client.ClientMobile);
cmd.Parameters.AddWithValue("@ClientFax", client.ClientFax);
cmd.Parameters.AddWithValue("@ClientEmail", client.ClientEmail);
cmd.Parameters.AddWithValue("@ClientInfo", client.ClientInfo);
cmd.ExecuteNonQuery();
}
return client;
}
/// <summary>
///
/// </summary>
/// <param name="clientID"></param>
/// <returns></returns>
public static ClientEntity GetClientByID(Int64 clientID)
{
ClientEntity client=null;
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SelectClient";
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@ClientID", clientID);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
client=LoadClient(reader);
}
}
return client;
}
/// <summary>
///
/// </summary>
/// <param name="ClientID"></param>
/// <returns></returns>
public static bool Exists(Int64 ClientID)
{
int numberOfRecord = 0;
using (SqlConnection conn=new SqlConnection())
{
conn.ConnectionString = connString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SelectValidClient";
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@ClientID", ClientID);
numberOfRecord = Convert.ToInt32(cmd.ExecuteScalar());
}
return numberOfRecord > 0;
}
public static List<ClientEntity> GetAllClients()
{
List<ClientEntity> lista = new List<ClientEntity>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SelectAllClient";
cmd.Connection = conn;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
lista.Add(LoadClient(reader));
}
}
return lista;
}
public static ClientEntity UpdateClient(ClientEntity client)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connString;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "UpdateClient";
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@ClientID", client.ClientID);
cmd.Parameters.AddWithValue("@ClientName", client.ClientName);
cmd.Parameters.AddWithValue("@ClientNif", client.ClientNIF);
cmd.Parameters.AddWithValue("@ClientAddress", client.ClientAddress);
cmd.Parameters.AddWithValue("@ClientCountry", client.ClientCountry);
cmd.Parameters.AddWithValue("@ClientProvincy", client.ClientProvincy);
cmd.Parameters.AddWithValue("@ClientCity", client.ClientCity);
cmd.Parameters.AddWithValue("@ClientPhone", client.ClientPhone);
cmd.Parameters.AddWithValue("@ClientMobile", client.ClientMobile);
cmd.Parameters.AddWithValue("@ClientFax", client.ClientFax);
cmd.Parameters.AddWithValue("@ClientEmail", client.ClientEmail);
cmd.Parameters.AddWithValue("@ClientInfo", client.ClientInfo);
cmd.ExecuteNonQuery();
}
return client;
}
private static ClientEntity LoadClient(IDataReader reader)
{
ClientEntity list= new ClientEntity();
list.ClientID =Convert.ToInt64(reader["ClientID"]);
list.ClientName = Convert.ToString(reader["ClientName"]);
list.ClientNIF = Convert.ToInt64(reader["ClientNif"]);
list.ClientAddress = reader["ClientAddress"]==null?string.Empty:Convert.ToString(reader["ClientAddress"]);
list.ClientCountry = reader["ClientCountry"]==null?string.Empty:Convert.ToString(reader["ClientCountry"]);
list.ClientProvincy = reader["ClientProvincy"]==null?string.Empty:Convert.ToString(reader["ClientProvincy"]);
list.ClientCity = reader["ClientCity"] == null ? string.Empty : Convert.ToString(reader["ClientCity"]);
list.ClientPhone = reader["ClientPhone"] == null ? string.Empty : Convert.ToString(reader["ClientPhone"]);
list.ClientMobile = reader["ClientMobile"] == null ? String.Empty : Convert.ToString(reader["ClientMobile"]);
list.ClientFax = reader["ClientFax"] == null ? String.Empty : Convert.ToString(reader["ClientFax"]);
list.ClientEmail = reader["ClientEmail"] == null ? string.Empty : Convert.ToString(reader["ClientEmail"]);
list.ClientInfo = reader["ClientInfo"] == null ? string.Empty : Convert.ToString(reader["ClientInfo"]);
return list;
}
}
答案 0 :(得分:0)
您无法为静态类定义接口。开始重构您的代码,一旦完成,使用在所有类中重复的相同方法编写接口。像
这样的东西public interface IClientDAL
{
string ConnString { get; set; }
ClientEntity NewClient(ClientEntity client);
//...
}
最后在需要它的所有类中实现接口。