R
rbjorkquist
I'm looking for help and understanding of generics; specifically, the
use of generic methods. I'm new to using generics and haven't found a
good example that applies to what I'm trying to do.
I have a web server, feeding data through web methods, to a windows
application. This is not a problem and all works. However, I am
trying to refactor my code and thought I could use generic methods.
I have two main types of web methods. One type always retrieves is
data from the database. This type does not do anything with the
application cache. The other first tries to retrieve its data from
the application cache. If this fails, it tries to retrieve its data
from an associated XML file. If that fails, it then retrieves the
data from the database, creates the associated XML file, and then
loads it into the application cache.
For all the methods that use the second type, the main difference is
the return type (a dataset), the cache name, the SQL command text, and
the source table. But the main syntax and code if the same. This is
why I thought I could use a generic method. But I get compile errors.
1 - Cannot create an instance of the variable type 'T' because it does
not have the new() constraint
2 - 'T' does not contain a definition for 'ReadXml'
3 - The best overloaded method match for
'System.Data.Common.DbDataAdapter.Fill(System.Data.DataSet, string)'
has some invalid arguments
4 - Argument '1': cannot convert from 'T' to 'System.Data.DataSet'
5 - 'T' does not contain a definition for 'WriteXml'
Even if I add "where T : new()" to the generic methods definition, I
still get errors 2, 3, 4, and 5. I just don't get what I'm missing.
Any help would be greatly appreciated.
//-------------------------------------------------------------------------------------------
[WebMethod]
public PasteViscosities GetPasteViscosities(){
StringBuilder CommandText = new StringBuilder(250);
CommandText.Append("SELECT");
CommandText.Append(" [PasteViscosityID]");
CommandText.Append(",[PasteViscosity]");
CommandText.Append(",[PasteViscosityMinimumValue]");
CommandText.Append(",[PasteViscosityMaximumValue] ");
CommandText.Append("FROM");
CommandText.Append(" [ccivw_PasteViscosities] ");
CommandText.Append("ORDER BY");
CommandText.Append(" [PasteViscosity] ASC");
PasteViscosities PasteViscosityList = null;
GetData<PasteViscosities>(ref PasteViscosityList,
"PasteViscosities",
"PasteViscosities.xml",
CommandText.ToString(),
"ccivw_PasteViscosities");
return(PasteViscosityList);
}
//-------------------------------------------------------------------------------------------
private void GetData<T>(ref T MyDataList, string CacheName, string
XMLFile, string SQLStatement, string SourceTable){
ConnectionStringSettings Cs =
ConfigurationManager.ConnectionStrings["EfficiencyConnectionString"];
MyDataList = (T)HttpRuntime.Cache[CacheName];
if(MyDataList == null){
//----------------------------------------------------
//ERROR # 1
//----------------------------------------------------
MyDataList = new T();
//----------------------------------------------------
SqlDataAdapter SqlDA = new SqlDataAdapter();
SqlConnection SqlCon = new SqlConnection();
SqlCommand SqlCmd = new SqlCommand();
try{
//----------------------------------------------------
//ERROR # 2
//----------------------------------------------------
MyDataList.ReadXml(Server.MapPath(XMLFile),
XmlReadMode.InferTypedSchema);
//----------------------------------------------------
}//END TRY-BLOCK
catch(System.IO.FileNotFoundException){
SqlCmd.CommandText = SQLStatement;
SqlCmd.Connection = SqlCon;
SqlCon.ConnectionString = (string)Cs.ConnectionString;
SqlCon.Open();
SqlDA.SelectCommand = SqlCmd;
//----------------------------------------------------
//ERROR # 3
//ERROR # 4
//----------------------------------------------------
SqlDA.Fill(MyDataList, SourceTable);
//----------------------------------------------------
//----------------------------------------------------
//ERROR # 5
//----------------------------------------------------
MyDataList.WriteXml(Server.MapPath(XMLFile));
//----------------------------------------------------
HttpRuntime.Cache.Insert(CacheName,
MyDataList,
new
CacheDependency(Server.MapPath(XMLFile)),
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.Low,
null);
}//END CATCH-BLOCK
catch(Exception E){
//Writes error message to the event log
HandleWebException(E);
}//END CATCH-BLOCK
finally{
SqlCon.Close();
}//END FINALLY-BLOCK
}//END IF-STATEMENT "if(MyDataList == null)"
}
use of generic methods. I'm new to using generics and haven't found a
good example that applies to what I'm trying to do.
I have a web server, feeding data through web methods, to a windows
application. This is not a problem and all works. However, I am
trying to refactor my code and thought I could use generic methods.
I have two main types of web methods. One type always retrieves is
data from the database. This type does not do anything with the
application cache. The other first tries to retrieve its data from
the application cache. If this fails, it tries to retrieve its data
from an associated XML file. If that fails, it then retrieves the
data from the database, creates the associated XML file, and then
loads it into the application cache.
For all the methods that use the second type, the main difference is
the return type (a dataset), the cache name, the SQL command text, and
the source table. But the main syntax and code if the same. This is
why I thought I could use a generic method. But I get compile errors.
1 - Cannot create an instance of the variable type 'T' because it does
not have the new() constraint
2 - 'T' does not contain a definition for 'ReadXml'
3 - The best overloaded method match for
'System.Data.Common.DbDataAdapter.Fill(System.Data.DataSet, string)'
has some invalid arguments
4 - Argument '1': cannot convert from 'T' to 'System.Data.DataSet'
5 - 'T' does not contain a definition for 'WriteXml'
Even if I add "where T : new()" to the generic methods definition, I
still get errors 2, 3, 4, and 5. I just don't get what I'm missing.
Any help would be greatly appreciated.
//-------------------------------------------------------------------------------------------
[WebMethod]
public PasteViscosities GetPasteViscosities(){
StringBuilder CommandText = new StringBuilder(250);
CommandText.Append("SELECT");
CommandText.Append(" [PasteViscosityID]");
CommandText.Append(",[PasteViscosity]");
CommandText.Append(",[PasteViscosityMinimumValue]");
CommandText.Append(",[PasteViscosityMaximumValue] ");
CommandText.Append("FROM");
CommandText.Append(" [ccivw_PasteViscosities] ");
CommandText.Append("ORDER BY");
CommandText.Append(" [PasteViscosity] ASC");
PasteViscosities PasteViscosityList = null;
GetData<PasteViscosities>(ref PasteViscosityList,
"PasteViscosities",
"PasteViscosities.xml",
CommandText.ToString(),
"ccivw_PasteViscosities");
return(PasteViscosityList);
}
//-------------------------------------------------------------------------------------------
private void GetData<T>(ref T MyDataList, string CacheName, string
XMLFile, string SQLStatement, string SourceTable){
ConnectionStringSettings Cs =
ConfigurationManager.ConnectionStrings["EfficiencyConnectionString"];
MyDataList = (T)HttpRuntime.Cache[CacheName];
if(MyDataList == null){
//----------------------------------------------------
//ERROR # 1
//----------------------------------------------------
MyDataList = new T();
//----------------------------------------------------
SqlDataAdapter SqlDA = new SqlDataAdapter();
SqlConnection SqlCon = new SqlConnection();
SqlCommand SqlCmd = new SqlCommand();
try{
//----------------------------------------------------
//ERROR # 2
//----------------------------------------------------
MyDataList.ReadXml(Server.MapPath(XMLFile),
XmlReadMode.InferTypedSchema);
//----------------------------------------------------
}//END TRY-BLOCK
catch(System.IO.FileNotFoundException){
SqlCmd.CommandText = SQLStatement;
SqlCmd.Connection = SqlCon;
SqlCon.ConnectionString = (string)Cs.ConnectionString;
SqlCon.Open();
SqlDA.SelectCommand = SqlCmd;
//----------------------------------------------------
//ERROR # 3
//ERROR # 4
//----------------------------------------------------
SqlDA.Fill(MyDataList, SourceTable);
//----------------------------------------------------
//----------------------------------------------------
//ERROR # 5
//----------------------------------------------------
MyDataList.WriteXml(Server.MapPath(XMLFile));
//----------------------------------------------------
HttpRuntime.Cache.Insert(CacheName,
MyDataList,
new
CacheDependency(Server.MapPath(XMLFile)),
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.Low,
null);
}//END CATCH-BLOCK
catch(Exception E){
//Writes error message to the event log
HandleWebException(E);
}//END CATCH-BLOCK
finally{
SqlCon.Close();
}//END FINALLY-BLOCK
}//END IF-STATEMENT "if(MyDataList == null)"
}