views:

107

answers:

6

I'm writing a .NET application and the thought of implementing a data layer from scratch is icky to me. (By data layer I'm referring to the code that talks to the database, not the layer which abstracts the database access into domain objects [sometimes called the data access layer and used interchangeably with data layer].)

I'd like to find an existing generic data layer implementation which provides standard crud functionality, error handling, connection management - the works. I'll be talking to SQL Server only.

It doesn't matter to me if the library is in C# or VB.NET and I don't care if it's LINQ or ADO.NET. As long as it works.

** I want to emphasize that I'm not looking for data access technologies or mechanisms (e.g. LINQ, ORM tools, etc.) but rather existing libraries.)

+5  A: 

If you are talking to only SQL Server the Linq to SQL is your best option. It is pretty easy to get up and running. You will get both the Data Layer and the Abstraction. All you have to do is provide a connection string to Linq to SQL and it will handle the rest.

If you are going to connect to other database than SQL you would want to with NHibernate.

NHibernate takes a little more work than Linq to SQL to get up and running. MS provided in Visual Studio a nice tool that can get you reading from a SQL database pretty quick.

David Basarab
+1 Although I would've included Entity Framework alongside nHibernate. I know, however, that that would be controversial for many people.
Andras Zoltan
Hi David - I'm looking for something already written. Something which includes error handling and other things that require some effort to code, e.g. handling various different return codes. I certainly don't need the library to meet all my requirements, but I want to cover as much as possible.
Howiecamp
+1  A: 

You can use easyobjects has a very small learning curve, and is very extensible. From their web:

EasyObjects.NET is a powerful data-access architecture for the .NET Framework. When used in combination with code generation, you can create, from scratch, a complete data layer for your application in minutes.

Kristian Damian
+1  A: 

I'd like to find an existing generic data layer implementation which provides standard crud functionality, error handling, connection management - the works. I'll be talking to SQL Server only.

Might want to check out Subsonic. Though I personally find it quite limited, it's certainly not an ORM, but a "query tool." It will make CRUD operations easy and straightforward, and it generates partial POCO classes for every table in your database, rather than trying to map from a database to a domain layer.

qstarin
Hi qstarin - I'd love to have a pre-made layer. Please see my comment under David Basarab's answer. Thanks.
Howiecamp
+1  A: 

Microsoft's Entity Framework might be what you are looking for to releave you from writing "the code that talks to the database".

The best things are that it already ships with Visual Studio and - depending on your requirements - you can use most functionality out-of-the box or manually adjust it to your custom business logic via T4 templates.

You can use it for forward and reverse engeneering and being a microsoft technology it integrates well with other MS products like SQL server.

I started using it 3 months ago in my current project at work which is composed of several windows and WCF services to convert third party data into our own database scheme. From the experiences we made with it, we'll be using the EF in future project a lot more.

Sensei76
A: 

What would you expect this framework to do with your exceptions? If it can't connect to your database, what should it do - crash the application, show an error message (winforms or WPF or ASP)... the questions are endless.

An ORM such as those suggested elsewhere in these answers is likely to be the closest you're going to get. Expecting a third party framework to provide all your exception handling isn't realistic - how would a third party know how your application is supposed to behave?

The direct answer to your question asking for "an existing generic data layer implementation which provides standard crud functionality, error handling, connection management - the works" is simple: use ADO.NET. The answers everyone else have provided actually go beyond that functionality, but your responses suggest that you think that there's something even further beyond - something that implements your data layer for you. My suggestion is that what you're looking for probably doesn't exist.

Dan Puzey
Hi Dan - Application developers have implemented zillions of data access layers over time. There have to be existing ones. These do exist - they have been written again and again.Using ADO.NET doesn't give me pre-written functionality. It gives me the base technology. It gives me the bricks rather than a completed building. Not sure what you mean about the answers going beyond the functionality I'm looking for. If I want a house and someone gives me wood, I've got to build the house.Unless I'm not being clear on my request? Would love your thoughts. Thanks.
Howiecamp
Exactly what pre-written functionality are you looking for that, say, Linq-to-SQL or NHibernate doesn't provide?
Dan Puzey
@Dan - Linq to SQL and NHibernate are ORMs - they provide you with a mapping system. You still need to create CRUD functs, still need to implement exception handling, still need to handle database-specific return codes. Obviously any existing data access library would need to be aware of the underlying schema, but the crud operations are essentially the same. Back to my point about database response code handling for example - this could be implemented as a common base class so all crud operations get the benefit. Using an ORM doesn't automatically solve this.
Howiecamp
@Dan - You said "What would you expect this framework to do with your exceptions? If it can't connect to your database, what should it do - crash the application, show an error message (winforms or WPF or ASP)... the questions are endless." I'm not looking for it to make the decision about what to do - clearly that's app-specific. I'm looking the information about why a particular exception is caused (eg locking issue, whatever) and I'll handle it as appropriate. There's a lot of database conditions that can occur. If I can use a library that already is aware of this, it makes my job easier.
Howiecamp
So what information would you be looking for that you don't get from a standard `SqlConnection` exception? You get cause of error as you described with basic ADO.NET. You also say "the CRUD operations are essentially the same [between systems]" but the are blatantly not: beyond the `SELECT` keyword there is very little common between queries on different schemas! You can't abstract these queries much more than ADO.NET or similar already do - that is, to automatically build the statement for you based on schema, using classes like `DataSet`/`DataAdapter`.
Dan Puzey
+2  A: 

Honestly as much of a fan as I've always been with NHibernate. With the latest release of Enterprise Library 5 Data Access Block that they added in the dynamic mapping support natively. I would have to strongly consider not using NHibernate on a project and instead use a forward database generation tool from my domain objects to create my database (perhaps even use NHibernate solely for the scheme export) or something like CodeSmith and use EntLib.

Chris Marisic
Hi Chris - Love the answer. I'd forgotten about this. Do you know if it includes error handling? Not just exception handling but handling of SQL Server-specific error codes.
Howiecamp
As far as I know those would just bubble up as standard sql exceptions.
Chris Marisic