本文介绍了没有模型和实体框架的 ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算将一个 ASP.Net 应用程序迁移到 ASP.NET MVC,我想避免使用模型和实体框架.相反,我将使用方法来直接访问数据库.

I am going to migrate an ASP.Net application to ASP.NET MVC, and I am thinking to avoid Models and Entity Framework. Instead I will use methods to direct access databases.

我的问题是这可能吗?两种方式的性能差异是什么?

My question here is that is it possible? What is the performance difference between both ways?

谢谢.

推荐答案

当然有可能.除了一个小例外:没有模型的 MVC 不是 MVC :-) 它是 VC,我个人从未听说过.既不作为设计模式,也不作为框架.听起来更像是 (WC :-))

Of course that it is possible. With one little exception: MVC without Models is not MVC :-) It's VC, which personally I never heard of. Neither as a design pattern nor as a framework. Sounds more like (WC :-))

两种方式的性能差异是什么?

您无法获得比原始 ADO.NET 更快的任何东西.所以,是的,与使用 ORM 相比,这会更快.

You can't get anything faster than the raw ADO.NET. So, yeah, that will be faster compared to using an ORM.

当然,您将不得不编写更多的代码,因为您仍然需要模型来映射查询的结果.不要认为您没有使用 EF 就可以免除使用模型的责任.也不要认为您会使用 DataTables.

Of course you will have to write far more code as you will still have models to map the results from your queries. Don't think that the fact that you are not using EF relieves you from the responsibility of using Models. Also don't think that you will be using DataTables.

所以基本上你会让你的数据层与这些模型一起工作.唯一的区别是实现.

So basically you will have your Data Layer work with those models. The only difference will be the implementation.

让我们举个例子.

定义一个模型来代表您打算在应用程序中使用的业务实体:

Define a Model that will represent your business entity that you intend to be working with in your application:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime Dob { get; set; }
}

然后定义您的数据访问合同(也就是您愿意对您的模型执行的操作):

then define your data access contract (a.k.a the operations you are willing to perform with your model):

public interface IPeopleRepository
{
    IEnumerable<Person> Get();

    ... other operations you want to perform with this model
}

然后你就可以实现了:

public class ADOPeopleRepository: IPeopleRepository
{
    public IEnumerable<Person> Get()
    {
        string connectionString = ...;
        using (var conn = new SqlConnection(connectionString))
        using (var cmd = conn.CreateCommand())
        {
            conn.Open();
            cmd.CommandText = "SELECT id, name, age, dob FROM people";
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    yield return new Person
                    {
                        Id = reader.GetInt32(reader.GetOrdinal("id")),
                        Name = reader.GetString(reader.GetOrdinal("name")),
                        Age = reader.GetInt32(reader.GetOrdinal("age")),
                        Dob = reader.GetDateTime(reader.GetOrdinal("dob")),
                    };
                }
            }
        }
    }

    ... implementation of the other operations you want to perform with this model
}

然后像往常一样你可能有一个控制器来处理这个存储库:

and then as usual you might have a controller to work with this repository:

public class PeopleController: Controller
{
    private readonly IPeopleRepository repository;
    public PeopleController(IPeopleRepository repository)
    {
        this.repository = repository;
    }

    public ActionResult Index()
    {
        var people = this.repository.Get().ToList();

        // Depending on the requirements of your view you might have
        // other method calls here and collect a couple of your domain models
        // that will get mapped and aggregated into a single view model
        // that will be passed to your view

        return View(people);
    }

    ...
}

现在剩下的就是将数据访问层的 ADOPeopleRepository 具体实现注册到您喜欢的容器中.

All that's left now is to register the ADOPeopleRepository concrete implementation of your Data Access Layer into your favorite container.

看看事情是如何分层的.现在,如果您正确编写了当前的 ASP.NET 应用程序,您可能已经拥有模型、接口和存储库实现.因此,将其迁移到 ASP.NET MVC 将是小菜一碟,您只需编写几个视图模型和视图即可.

See how things get layered. Now, if you wrote your current ASP.NET application properly, you probably already have the Models, the interface and the repository implementations. So migrating it to ASP.NET MVC will be a piece of cake where all you need to do is write a couple of view models and views.

这篇关于没有模型和实体框架的 ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 11:57