Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to set up data access for an ASP.NET MVC project?

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.

like image 831
Ryan Skarin Avatar asked Sep 22 '08 18:09

Ryan Skarin


People also ask

How does ASP MVC connect to database?

Step 1 − To install the Entity Framework, right-click on your project and select NuGet Package Manager → Manage NuGet Packages for Solution… It will open the NuGet Package Manager. Search for Entity framework in the search box. Select the Entity Framework and click 'Install' button.

How can store data in database in ASP.NET MVC?

Select “ Web application (Model-View-Controller)” template and press OK to create Asp.Net Core MVC project. Right-click on the solution and add a new folder named “Models”. Now, add a new class on the Models folder. Right-click on Models folder and select "Add a new class".

What is data access layer in MVC?

A data access layer (DAL) in computer software is a layer of a computer program which provides simplified access to data stored in persistent storage of some kind, such as an entity-relational database. This acronym is prevalently used in Microsoft environments.


5 Answers

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:

like image 134
Haacked Avatar answered Oct 01 '22 04:10

Haacked


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.

like image 27
JPrescottSanders Avatar answered Oct 01 '22 03:10

JPrescottSanders


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.

like image 37
brock.holum Avatar answered Oct 01 '22 03:10

brock.holum


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.

like image 31
hangy Avatar answered Oct 01 '22 05:10

hangy


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.

like image 32
Jaco Pretorius Avatar answered Oct 01 '22 03:10

Jaco Pretorius