views:

542

answers:

9

I am starting a new ASP.NET MVC project to learn with, and am wondering what's the optimal way to set up the project(s) to connect to a SQL server for the data. For example lets pretend we have a Product table and a product object I want to use to populate data in my view.

I know somewhere in here I should have an interface that gets implemented, etc but I can't wrap my mind around it today :-(

EDIT: Right now (ie: the current, poorly coded version of this app) I am just using plain old SQL server(2000 even) using only stored procedures for data access, but I would not be adverse to adding in an extra layer of flexability for using linq to sql or something.

EDIT #2: One thing I wanted to add was this: I will be writing this against a V1 of the database, and I will need to be able to let our DBA re-work the database and give me a V2 later, so it would be nice to only really have to change a few small things that are not provided via the database now that will be later. Rather than having to re-write a whole new DAL.

+1  A: 

In my site's solution, I have the MVC web application project and a "common" project that contains my POCOs (plain ol' C# objects), business managers and data access layers.

The DAL classes are tied to SQL Server (I didn't abstract them out) and return POCOs to the business managers that I call from my controllers in the MVC project.

muloh
I guess maybe some of the issue I'm trying to get an understanding on is what would the difference be between your objects in the common projects VS objects that should go in the model folder? Or does this common project replace stuffing things into models?
Ryan Skarin
I could be completely wrong (I'm new to MVC), but I'd use the Models folder to create mash-ups of my "common" objects for view-specific use.
muloh
So you'd basically extend your common objects in the models folder if they needed to do something not provided in your basic common object?
Ryan Skarin
If I need an object that's specific to the MVC app, I put it in the Models folder. However, I found this quote on asp.net which is making me think I'm wrong: "The model should contain all of your application business logic and database access logic." http://www.asp.net/learn/mvc/tutorial-02-cs.aspx
muloh
Maybe I am thinking too much about separating things into separate projects when separate cs files is really all that I should be doing.
Ryan Skarin
+4  A: 

It really depends on which data access technology you're using. If you're using Linq To Sql, you might want to abstract away the data access behind some sort of "repository" interface, such as an IProductRepository. The main appeal for this is that you can change out the specific data access implementation at any time (such as when writing unit tests).

I've tried to cover some of this here:

Haacked
Can't open that link....is your site down?
Ryan Skarin
+3  A: 

I would check out Rob Conery's videos on his creation of an MVC store front. The series can be found here: MVC Store Front Series

This series dives into all sorts of design related subjects as well as coding/testing practies to use with MVC and other projects.

JPrescottSanders
A: 

For our application I plan on using LINQ to Entities, but as it's new to me there is the possiblity that I will want to replace this in the future if it doesn't perform as I would like and use something else like LINQ to SQL or NHibernate, so I'll be abstracting the data access objects into an abstract factory so that the implementation is hidden from the applicaiton.

How you do it is up to you, as long as you choose a proven and well know design pattern for implementation I think your final product will be well supported and robust.

Odd
A: 

Check out the Code Camp Server for a good reference application that does this very thing and as @haacked stated abstract that goo away to keep'em separated (thx OffSpring).

MotoWilliams
+1  A: 

I think that Billy McCafferty's S#arp Architecture is a quite nice example of using ASP.NET MVC with a data access layer (using NHibernate as default), dependency injection (Ninject atm, but there are plans to support the CommonServiceLocator) and test-driven development. The framework is still in development, but I consider it quite good and stable. As of the current release, there should be few breaking changes until there is a final release, so coding against it should be okay.

hangy
A: 

Use LINQ. Create a LINQ to SQL file and drag and drop all the tables and views you need. Then when you call your model all of your CRUD level stuff is created for you automagically.

LINQ is the best thing I have seen in a long long time. Here are some simple samples for grabbing data from Scott Gu's blog.

LINQ Tutorial

Al Katawazi
A: 

I just did my first MVC project and I used a Service-Repository design pattern. There is a good bit of information about it on the net right now. It made my transition from Linq->Sql to Entity Framework effortless. If you think you're going to be changing a lot put in the little extra effort to use Interfaces.

I recommend Entity Framework for your DAL/Repository.

Bill
+1  A: 

I have done a few MVC applications and I have found a structure that works very nicely for me. It is based upon Rob Conery's MVC Storefront Series that JPrescottSanders mentioned (although the link he posted is wrong).

So here goes - I usually try to restrict my controllers to only contain view logic. This includes retrieving data to pass on to the views and mapping from data passed back from the view to the domain model. The key is to try and keep business logic out of this layer.

To this end I usually end up with 3 layers in my application. The first is the presentation layer - the controllers. The second is the service layer - this layer is responsible for executing complex queries as well as things like validation. The third layer is the repository layer - this layer is responsible for all access to the database.

So in your products example, this would mean that you would have a ProductRepository with methods such as GetProducts() and SaveProduct(Product product). You would also have a ProductService (which depends on the ProductRepository) with methods such as GetProductsForUser(User user), GetProductsWithCategory(Category category) and SaveProduct(Product product). Things like validation would also happen here. Finally your controller would depend on your service layer for retrieving and storing products.

You can get away with skipping the service layer but you will usually find that your controllers get very fat and tend to do too much. I have tried this architecture quite a few times and it tends to work quite nicely, especially since it supports TDD and automated testing very well.

Jaco Pretorius