主题:[讨论]做一个通用的基类实现多个子类继承???高手!
在项目中有十个类都用到了create()函数,但是函数中的变量及参数的个数和类型是不一样的。如果我想编一个基类实现继承的话,只能够使用重载的方法,这样太繁琐。请问有没有一种方法实现一个通用的基类呢?
例如:其中一个Create()函数
private static readonly string PARM_TYPE_ID = "@typeId";
private static readonly string PARM_TYPE_CODE = "@typeCode";
private static readonly string PARM_TYPE_NAME = "@typeName";
private static readonly string PARM_TYPE_SPEC = "@typeSpec";
private static readonly string SQL_CREATE =
"INSERT INTO pm_tyreType (typeCode, typeName, typeSpec)" +
"VALUES(?, ?, ?)";
public int Create(
string typeCode,//变量的个数是不一样的
string typeName,
string typeSpec
)
{
//read the runtime setup
dataSettings settings = settingsManager.DataSettings;
SqlConnection conn = new SqlConnection(settings.ConnectionString);
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = [color=FF0000]SQL_CREATE[/color];
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter(PARM_TYPE_CODE,SqlDbType.VarChar,30),
new SqlParameter(PARM_TYPE_NAME,SqlDbType.VarChar,50),
new SqlParameter(PARM_TYPE_SPEC,SqlDbType.VarChar,100)
};
//asign values to the parameters
parms[0].Value = typeCode;
parms[1].Value = typeName;
parms[2].Value = typeSpec;
for (int i = 0; i < parms.Length; i ++)
{
command.Parameters.Add(parms[i]);
}
try
{
conn.Open();
command.ExecuteNonQuery();
return 1;
} //end try to execute sql
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
} //end finally
}//end create
例如:其中一个Create()函数
private static readonly string PARM_TYPE_ID = "@typeId";
private static readonly string PARM_TYPE_CODE = "@typeCode";
private static readonly string PARM_TYPE_NAME = "@typeName";
private static readonly string PARM_TYPE_SPEC = "@typeSpec";
private static readonly string SQL_CREATE =
"INSERT INTO pm_tyreType (typeCode, typeName, typeSpec)" +
"VALUES(?, ?, ?)";
public int Create(
string typeCode,//变量的个数是不一样的
string typeName,
string typeSpec
)
{
//read the runtime setup
dataSettings settings = settingsManager.DataSettings;
SqlConnection conn = new SqlConnection(settings.ConnectionString);
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = [color=FF0000]SQL_CREATE[/color];
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter(PARM_TYPE_CODE,SqlDbType.VarChar,30),
new SqlParameter(PARM_TYPE_NAME,SqlDbType.VarChar,50),
new SqlParameter(PARM_TYPE_SPEC,SqlDbType.VarChar,100)
};
//asign values to the parameters
parms[0].Value = typeCode;
parms[1].Value = typeName;
parms[2].Value = typeSpec;
for (int i = 0; i < parms.Length; i ++)
{
command.Parameters.Add(parms[i]);
}
try
{
conn.Open();
command.ExecuteNonQuery();
return 1;
} //end try to execute sql
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
} //end finally
}//end create