views:

68

answers:

3
+1  Q: 

.NET Object Design

I have a series of objects I have created:

Item

Order

Song

etc.

Each object has a reasonable number of properties, and I use a datareader where I pass it "SELECT * FROM .objectname." and then I fill a collection of objects, and return the collection. This works as: GetOrdersCollection(), GetSongsCollection(), etc.

I understand SELECT * to be a performance problem, and additionally, sometimes I prefer to include additional columns in the select statement which do not exist in the object, and have those all returned as well.

So my question is, what is the best way to approach this problem?

  1. Should I create a new object for every query type?

  2. I tried performing a check to see if column is in datareader before storing it, but this presents perf. issues. Is there a negligible perf. way to avoid IndexOutOfRange?

  3. Should I just use Datatable and read right from the table?

+1  A: 

Performance-wise reading from a forward only data structure like DataReader is going to net you the best performance and resource conservation.

On the other hand populating object (like a OR/M does) can be negligible so long as you are not returning more than a handful of objects.

Your first step should be to profile your database and ensure that you have proper indexes. Write some tests to see where your largest time expense is in the process and optimize the target areas that cost you the most.

Ikarii Warrior
Speed of data returned from the database is not a problem for me. I can return 20,000 records in less than a second. My concern comes from trying to load up objects with joined data which only may be needed for a certain query. If I include some column from another related column, this breaks the object design. I understand this is a coupling issue, but I'm not aware of a better way than just using a datatable.
Kyle B.
p.s. I up-voted your suggestion... thank you.
Kyle B.
This is why OR/Ms were invented - as you are finding objects do not necessarily map 1 to 1 to database tables (nor should they!) I think your best option is to find a happy medium of performance and maintainability - don't worry so much about making your app enterprise so much as making your app maintainable. Does that make sense?
Ikarii Warrior
+1  A: 

Are there any reasons you can't use a simple ORM generator like SubSonic? This will allow you to very easily access these types of collections, and they'll be strongly typed. You also won't have to worry about the SQL since the queries will be built by SubSonic.

John Rasch
I am dealing with a project which already has class files generated and I'm not sure my voice will be heard to migrate to an OR/M like LINQ or SubSonic. What we are using currently works well, I'm just looking for an elegant way to include related data or partial data in my object collections.
Kyle B.
Just as a heads up if OR/M becomes a topic of discussion. Don't spend too much time investing in Linq2Sql as an OR/M if you have a lot of many-to-many relationships. L2S doesn't support those directly.
Ikarii Warrior
+2  A: 

I understand SELECT * to be a performance problem,

It's not a performance problem if there are only a few columns, or you need all of the columns anyway.

1.Should I create a new object for every query type?

You should create a new object for each table, and a new method for each query type.

2.I tried performing a check to see if column is in datareader before storing it, but this presents perf. issues. Is there a negligible perf. way to avoid IndexOutOfRange?

If you are referring to your fields by name rather than index, there shouldn't be any IndexOutOfRange problems. If you are referring to your fields by index, you can loop thru them where your index is less than the column Count(), and there shouldn't be any IndexOutOfRange problems.

3.Should I just use Datatable and read right from the table?

That's a perfectly good approach to start out with. Consider spending some time to learn a simple ORM as others have suggested. Subsonic is a good "first" ORM.

Robert Harvey