Return object list as DataTable or some sort of object array?

R

Ronald S. Cook

I think I've been doing something wrong (i.e. not best practice)

From my class, I've been returning properties and methods ok.. in OO fashion
to the client.

However, I've been returning a list of that class as a Dataset. Shouldn't I
be sending it as an array of that object or similar somehow? What would
that syntax look like?

Here is what I currently have:


public class Merchant
{
private DataTable _MerchantList;
public DataTable MerchantList
{
get { return _MerchantList; }
set { _MerchantList = value; }
}

private Guid _MerchantId;
public Guid MerchantId
{
get { return _MerchantId; }
set { _MerchantId = value; }
}

private String _MerchantName;
public String MerchantName
{
get { return _MerchantName; }
set { _MerchantName = value; }
}
etc...


Thanks,
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ronald,

I wouldn't necessarily say it is wrong, after all, I don't know the
requirements of your class, or how it will be used.

But you do put yourself at a disadvantage, as the data table will be
untyped, and you might run into runtime errors if someone spells a field
incorrectly when trying to get a value from the data table.

More preferably, you would define an object that exposes the properties
of a row in the table (assuming that there is a general mapping between a
row and an entity you are trying to represent) and then return an IList<T>
of that. I recommend IList<T> because you can add rows to the DataTable,
and IList<T> will allow the user to add rows to the collection, which the
hosting object (Merchant) actually has.
 
R

Ronald S. Cook

Thanks Nicholas. Could you show me even a little what that syntax would
look like in my Merchant class?

I greatly appreciate it.



Nicholas Paldino said:
Ronald,

I wouldn't necessarily say it is wrong, after all, I don't know the
requirements of your class, or how it will be used.

But you do put yourself at a disadvantage, as the data table will be
untyped, and you might run into runtime errors if someone spells a field
incorrectly when trying to get a value from the data table.

More preferably, you would define an object that exposes the properties
of a row in the table (assuming that there is a general mapping between a
row and an entity you are trying to represent) and then return an IList<T>
of that. I recommend IList<T> because you can add rows to the DataTable,
and IList<T> will allow the user to add rows to the collection, which the
hosting object (Merchant) actually has.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
I think I've been doing something wrong (i.e. not best practice)

From my class, I've been returning properties and methods ok.. in OO
fashion to the client.

However, I've been returning a list of that class as a Dataset.
Shouldn't I be sending it as an array of that object or similar somehow?
What would that syntax look like?

Here is what I currently have:


public class Merchant
{
private DataTable _MerchantList;
public DataTable MerchantList
{
get { return _MerchantList; }
set { _MerchantList = value; }
}

private Guid _MerchantId;
public Guid MerchantId
{
get { return _MerchantId; }
set { _MerchantId = value; }
}

private String _MerchantName;
public String MerchantName
{
get { return _MerchantName; }
set { _MerchantName = value; }
}
etc...


Thanks,
Ron
 
M

Mr. Arnold

Ronald S. Cook said:
Thanks Nicholas. Could you show me even a little what that syntax would
look like in my Merchant class?

You want to see what's going on then view.

MODEL-VIEW-PRESENTER

http://www.polymorphicpodcast.com/

click 'Shows'

click 'Design Patterns Bootcamp: Model View * Patterns*

view parts 1-5

You can view the rest of the shows too.
 

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

Top