Question on using .NET Generics with C#

  • Thread starter Thread starter Kevin S. Goff
  • Start date Start date
K

Kevin S. Goff

Hi, all,

Hopefully this will make sense:

I have 2 classes that implement the same generic interface.

public interface IAgingReport<T>
{
T GetAgingReport(DateTime dAsOfDate);
}


1 of the two classes that implements the interface, returns a dataset:

public class bzAgingReport : IAgingReport<Dataset>
{
public Dataset GetAgingReport(DateTime dAsOfDate);
{
// get data for aging report, return dataset
}
}


The 2nd class also implements the interface, but returns an XML
string:

public class wAgingReport : WebService, IAgingReport<string>
{
[WebMethod]
public string GetAgingReport(DateTime dAsOfDate)
{
// return XML string

}

The reason I'm using one interface is because the client piece might
be using web services, or it might be using remoting. I want to use
an interface-based approach on the client-side, and use a common code
base, regardless of which communication approach is being used.

On the client side, I can create a regular .NET object (which is
either a web reference object or a remoting object), but I can't
figure out how to write "generic" code to deal with the different data
types that might be coming back (XML string or dataset).

So in my client piece, I have (and this is pseudocode)

object oReturnObject = either web service object or remoting
proxy object


if (using web services)
{
IAgingReport<string> oAgingReport;
oAgingReport = (IAgingReport<string>)oReturnObject;
string cXMLResults =oAgingReport.GetAgingReport(dAsOfDate);
MyDataSet.ReadXml(new StringReader(cXMLResults));
}
else
{
IAgingReport<DataSet> oAgingReport;
oAgingReport = (IAgingReport<DataSet>)oReturnObject;
MyDataSet = oAgingReport.GetAgingReport(dAsOfDate);
}
// at this point, either way, I have my results in a dataset

Essentially, in the first example, I'm utilizing the interface
function that returns a string (and then convert to a dataset). In
the second example, I'm using the interface function that returns a
dataset.

here's my question - this all "works", but I've been trying to figure
out how to avoid the 'IF' block above. I'm wondering if I need a 2nd
usage of generics, but I can't figure out which one. I've tried
different combinations of specifying a variable type, but nothing I
try seems to work.



Thanks in advance,
Kevin
 
Hi Kevin,
I'm not sure what you're trying to acheive by using generics in this
situation, as I'd probably have chosen (given the information in your
post) to skip generics and just create one object that returns a
DataSet (assuming the XML string conforms to some DataSet schema).
Since DataSet instances are serializable (as XML) you'd be able to
reference the same server-side object over both transport layers. Using
generics only seems to compound the issues you're seeing, by not being
able to cast between IAgingReport<Dataset> and IAgingReport<string>.
Hope this helps,
Jono
Hi, all,

Hopefully this will make sense:

I have 2 classes that implement the same generic interface.

public interface IAgingReport<T>
{
T GetAgingReport(DateTime dAsOfDate);
}


1 of the two classes that implements the interface, returns a dataset:

public class bzAgingReport : IAgingReport<Dataset>
{
public Dataset GetAgingReport(DateTime dAsOfDate);
{
// get data for aging report, return dataset
}
}


The 2nd class also implements the interface, but returns an XML
string:

public class wAgingReport : WebService, IAgingReport<string>
{
[WebMethod]
public string GetAgingReport(DateTime dAsOfDate)
{
// return XML string

}

The reason I'm using one interface is because the client piece might
be using web services, or it might be using remoting. I want to use
an interface-based approach on the client-side, and use a common code
base, regardless of which communication approach is being used.

On the client side, I can create a regular .NET object (which is
either a web reference object or a remoting object), but I can't
figure out how to write "generic" code to deal with the different data
types that might be coming back (XML string or dataset).

So in my client piece, I have (and this is pseudocode)

object oReturnObject = either web service object or remoting
proxy object


if (using web services)
{
IAgingReport<string> oAgingReport;
oAgingReport = (IAgingReport<string>)oReturnObject;
string cXMLResults =oAgingReport.GetAgingReport(dAsOfDate);
MyDataSet.ReadXml(new StringReader(cXMLResults));
}
else
{
IAgingReport<DataSet> oAgingReport;
oAgingReport = (IAgingReport<DataSet>)oReturnObject;
MyDataSet = oAgingReport.GetAgingReport(dAsOfDate);
}
// at this point, either way, I have my results in a dataset

Essentially, in the first example, I'm utilizing the interface
function that returns a string (and then convert to a dataset). In
the second example, I'm using the interface function that returns a
dataset.

here's my question - this all "works", but I've been trying to figure
out how to avoid the 'IF' block above. I'm wondering if I need a 2nd
usage of generics, but I can't figure out which one. I've tried
different combinations of specifying a variable type, but nothing I
try seems to work.



Thanks in advance,
Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top