469,589 Members | 2,147 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,589 developers. It's quick & easy.

Question on using .NET Generics with C#

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<stringoAgingReport;
oAgingReport = (IAgingReport<string>)oReturnObject;
string cXMLResults =oAgingReport.GetAgingReport(dAsOfDate);
MyDataSet.ReadXml(new StringReader(cXMLResults));
}
else
{
IAgingReport<DataSetoAgingReport;
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


Aug 3 '06 #1
1 1511
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<Datasetand IAgingReport<string>.
Hope this helps,
Jono

Kevin S. Goff wrote:
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<stringoAgingReport;
oAgingReport = (IAgingReport<string>)oReturnObject;
string cXMLResults =oAgingReport.GetAgingReport(dAsOfDate);
MyDataSet.ReadXml(new StringReader(cXMLResults));
}
else
{
IAgingReport<DataSetoAgingReport;
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
Aug 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Matthew W. Jackson | last post: by
1 post views Thread by Peter Kirk | last post: by
10 posts views Thread by Lloyd Dupont | last post: by
11 posts views Thread by hammad.awan_nospam | last post: by
14 posts views Thread by cwineman | last post: by
4 posts views Thread by DeveloperX | last post: by
11 posts views Thread by Olaf Krumnow | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.