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

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

问题描述

我要寻找出一种方式来访问使用的是经典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 Framework的CRUD操作。

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

推荐答案

您可以有一个存储库:

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

通过这种方式,控制器不再依赖于数据访问层的实现:无论您使用的是普通的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天全站免登陆