将 ASP.Net MVC 与经典 ADO.Net 结合使用 [英] Using ASP.Net MVC with Classic ADO.Net

查看:16
本文介绍了将 ASP.Net MVC 与经典 ADO.Net 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用 Classic ADO.Net 访问存储过程的方法,因为我是 ASP.Net MVC 的新手,我不知道如何去做.

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.

大部分示例展示了使用 ADO.Net Entity 框架的 CRUD 操作.

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

推荐答案

你可以有一个存储库:

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

然后实现它:

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);
    }
}

这样,控制器不再依赖于数据访问层的实现:无论您是使用普通的 ADO.NET、NHibernate、EF,还是其他一些 ORM,调用外部 Web 服务、XML,都可以.

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.

现在剩下的就是配置您最喜欢的 DI 框架,以将存储库的正确实现注入到控制器中.如果明天您决定更改数据访问技术,没问题,只需编写 IUsersRepository 接口的不同实现并重新配置您的 DI 框架以使用它.无需触摸您的控制器逻辑.

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.

您的 MVC 应用程序不再依赖于数据的存储方式.这也使得单独对控制器进行单元测试变得更加容易,因为它们不再与特定数据源紧密耦合.

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.

这篇关于将 ASP.Net MVC 与经典 ADO.Net 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆