views:

447

answers:

6

When I last worked in programming, we were trying to move away from DataReaders and the traditional ADO.NET API toward Object Relational Mapping (ORM).

To do this, we generated a DataContext of our DB via sqlmetal. There was then a thin data layer that made the DataContext private, and any code needing to access the database would have to use a public method in this thin data layer. These methods were basically stored procedures; they would perform queries on the database via LINQ to SQL.

Is this a common approach today? I mean, is everyone whose using the .NET 3.5 framework really running sqlmetal in their build process, or what? It almost seemed like a hack at the time.

Basically, I'd like to know if LINQ to SQL and sqlmetal is what to expect if I'm go to write a DAL today at a .NET 3.5 shop that doesn't employ a third-party, open-source ORM.

+2  A: 

I think it depends on your use but I'd say with such a thin data layer as you explained that would be your DAL. Most projects will build another layer on top of that mainly for edit/create logic and maybe some stitching logic for gets.

For most of my projects I design it like this.

Repository holds the instance of DataContext and exposes some basic add/delete methods
ProductRepository : Repository exposes general queries (IQueryable)
StoreService uses an instance of different repositories like ProductRepository, SalesRepository and handles all logic for creating something like a product.

So something like...

StoreService.CreateProduct(/* properites */)

This would return some sort of result class.

Chad Moran
I knew I wasn't the only person using this approach for the usual line of business app! Maintainability and separation of concerns has been the biggest benefit since I started developing with this approach.
Toran Billups
+4  A: 

It is still considered best practice to have some sort of data access layer. Whether this is best achieved with a ORM is a heavily debated issue. There is one faction that generally argues that ORM's are the way to go. Another faction argues that stored procedures and database centric is the best route.

Also, this may not be exactly the poster you meant, but it similar (and also the one in my cubicle)

http://download.microsoft.com/download/4/a/3/4a3c7c55-84ab-4588-84a4-f96424a7d82d/NET35_Namespaces_Poster_LORES.pdf

Jacob Adams
+1  A: 

This very site uses LINQ to SQL, so take that as you will.

Officially, Microsoft is supporting Entity Framework over LINQ to SQL in terms of new development. However, there's a vocal group of people who think EF is the wrong way to go. LINQ to SQL will still be around for some time, and is a very decent ORM, if somewhat limiting in terms of which DB backend you can use.

I would recommend LINQ as a great starting point for your ORM. If you need better, look into EF and/or NHibernate.

Randolpho
Do you know if there has been any official response to the concerns raised with that "vote of no confidence"?
Chris Dunaway
I don't have any links at the moment, nor do I feel like digging, but I'm pretty sure the official line was something along the lines "screw that, we're doing EF the way we want anyway". I'm sure it was more PC than that, however. :) I think they said that a lot of the issues would be fixed in later versions. Ship early/often, etc.
Randolpho
+2  A: 

Your approach is good. I currently use Astroria services (ADO.NET Data Services). There was a nice introduction in MSDN Magazine about this.

I also like the new PLINQO (requires CodeSmith Tools though). This is very slick in my opinion.

When I have such a DAL (service layer), I just consume this service from my client application (Silverlight or ASP.NET MVC).

David Pokluda
A: 

"Is this a common approach today? I mean, is everyone whose using the .NET 3.5 framework really running sqlmetal in their build process, or what?"

The people I know using the 3.5 Framework (and that's just about everyone) - the vast majority - are still using NHibernate. Version 2.0 is a very nice OR/M. I started using it on a recent project and it cut my data access code down significantly, to the point where I really don't want to use anything else in the future. And the Fluent NHibernate API is making some headway for folks who don't like the XML mapping.

Chris Holmes
+1  A: 

The best data layer is the one that is plain and simple and gets the job done without any bells any whistles. I have used the technologies you mentioned and written about them here: The Only Pattern for Data Access is - There Are No Patterns for Data Access

Ahmad