Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ASP.Net MVC with Classic ADO.Net

I am looking out for a way to access stored procedures using Classic ADO.Net, since I am new to ASP.Net MVC I do not know how to go about it.

Majority of the examples show CRUD operations using ADO.Net Entity framework.

like image 710
Sandhurst Avatar asked Jul 14 '11 13:07

Sandhurst


People also ask

Can ADO.NET be used with MVC?

MVC (Model View Controller) is a web application design pattern that is widely used in application development. Here, we are creating an MVC application that connects to the SQL Server with the help of ADO.NET framework. This application contains a Model, a View and a Controller file.

What is difference between ADO.NET and MVC?

Summary. Just as ASP.NET MVC has made building web applications easier, the ADO.NET Entity Framework has made building data connected applications easier. The Entity Framework is not the only ORM tool out there, but it is Microsoft's data access strategy moving forward so it is something that you want to keep an eye on ...

Is ADO.NET outdated?

It isn't obsolete, it is the foundation for working with databases in . NET. It certainly is the archaic way of accessing a database though..


1 Answers

You could have a repository:

public interface IUsersRepository {     public User GetUser(int id); } 

then implement it:

public class UsersRepository: IUsersRepository {     private readonly string _connectionString;     public UsersRepository(string connectionString)     {         _connectionString = connectionString;     }      public User GetUser(int id)     {         // Here you are free to do whatever data access code you like         // You can invoke direct SQL queries, stored procedures, whatever           using (var conn = new SqlConnection(_connectionString))         using (var cmd = conn.CreateCommand())         {             conn.Open();             cmd.CommandText = "SELECT id, name FROM users WHERE id = @id";             cmd.Parameters.AddWithValue("@id", id);             using (var reader = cmd.ExecuteReader())             {                 if (!reader.Read())                 {                     return null;                 }                 return new User                 {                     Id = reader.GetInt32(reader.GetOrdinal("id")),                     Name = reader.GetString(reader.GetOrdinal("name")),                 }             }         }     } } 

and then your controller could use this repository:

public class UsersController: Controller {     private readonly IUsersRepository _repository;     public UsersController(IUsersRepository repository)     {         _repository = repository;     }      public ActionResult Index(int id)     {         var model = _repository.GetUser(id);         return View(model);     } } 

This way the controller is no longer depend on the implementation of your data access layer: whether you are using plain ADO.NET, NHibernate, EF, some other ORM, calling an external web service, XML, you name it.

Now all that's left is to configure your favorite DI framework to inject the proper implementation of the repository into the controller. If tomorrow you decide to change your data access technology, no problem, simply write a different implementation of the IUsersRepository interface and reconfigure your DI framework to use it. No need to touch your controller logic.

Your MVC application is no longer tied to the way data is stored. This makes it also easier to unit test your controllers in isolation as they are no longer tightly coupled to a particular data source.

like image 55
Darin Dimitrov Avatar answered Oct 04 '22 06:10

Darin Dimitrov