tags:

views:

201

answers:

5

I am putting some heavy though into re-writing the data access layer in my software(If you could even call it that). This was really my first project that uses, and things were done in an improper manner.

In my project all of the data that is being pulled is being stored in an arraylist. some of the data is converted from the arraylist into an typed object, before being put backinto an arraylist.

Also, there is no central set of queries in the application. This means that some queries are copy and pasted, which I want to eliminate as well.This application has some custom objects that are very standard to the application, and some queries that are very standard to those objects.

I am really just not sure if I should create a layer between my objects and the class that reads and writes to the database. This layer would take the data that comes from the database, type it as the proper object, and if there is a case of multiple objects being returned, return a list of those object. Is this a good approach?

Also, if this is a good way of doing things, how should I return the data from the database? I am currently using SqlDataReader.read, and filling an array list. I am sure that this is not the best method to use here, i am just not real clear on how to improve this.

The Reason for all of this, is I want to centralize all of the database operations into a few classes, rather than have them spread out amongst all of the classes in the project

+2  A: 

It really depends on what you are doing. If it is a growing application with user interfaces and the like, you're right, there are better ways.

I am currently developing in ASP.NET MVC, and I find Linq to SQL really comfortable. Linq to SQL uses code generation to create a collection of code classes that model your data.

ScottGu has a really nice introduction to Linq to SQL on his blog:

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

Robert Harvey
Thanks for the link. I read the posting and it seems fairly easy to accomplish, looks much easier to work with too
Aaron M
A: 

"I am really just not sure if I should create a layer between my objects and the class that reads and writes to the database. This layer would take the data that comes from the database, type it as the proper object, and if there is a case of multiple objects being returned, return a list of those object. Is this a good approach?"

I'm a Java developer, but I believe that the language-agnostic answer is "yes".

Have a look at Martin Fowler's "Patterns Of Enterprise Application Architecture". I believe that technologies like LINQ were born for this.

duffymo
+4  A: 

One thing comes to mind right off the bat. Is there a reason you use ArrayLists instead of generics? If you're using .NET 1.1 I could understand, but it seems that one area where you could gain performance is to remove ArrayLists from the picture and stop converting and casting between types.

Another thing you might think about which can help a lot when designing data access layers is an ORM. NHibernate and LINQ to SQL do this very well. In general, the N-tier approach works well for what it seems like you're trying to accomplish. For example, performing data access in a class library with specific methods that can be reused is far better than "copy-pasting" the same queries all over the place.

I hope this helps.

Scott Anderson
Because I really had very little experience writing anything large than a hello world type application. I didnt even know what generics were until much further down the road, and I certainly didnt realize the benefit, nor the number of queries that I would eventually write
Aaron M
Okay. Fair enough. I'm glad that you know now, however :). Get rid of those sneaky ArrayLists!
Scott Anderson
+5  A: 

You should use an ORM. "Not doing so is stealing from your customers" - Ayende

Martin Murphy
Ha !I was just looking up his post as you answered this.http://ayende.com/Blog/archive/2008/11/21/stealing-from-your-client.aspx
Rob Scott
nice vonnegut reference as well.
Rob Scott
+1  A: 

I have over the past few projects used a base class which does all my ADO.NET work and that all other data access classes inherit. So my UserDB class will inherit the DataAccessBase class. I have it at the moment that my UserDB class actualy takes the data returned from the database and populates a User object which is then returned to the calling Business Object. If multiple objects are returned then these are then a Generic list ie List<Users> is returned.

There is a good article by Daemon Armstrong (search Google for Daemon Armstrong which demonstrates on how this can be achived.

""http://www.simple-talk.com/dotnet/.net-framework/.net-application-architecture-the-data-access-layer/""

However I have now started to move all of this over to use the entitty framework as its performs much better and saves on all those manual CRUD operations. Was going to use LINQ to SQL but as it seems to be going to be dead in the water very soon thought it would be best to invest my time in the next ORM.

Cragly