没有模型和实体框架的ASP.NET MVC [英] ASP.NET MVC without Models and Entity Framework

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

问题描述

我打算将ASP.Net应用程序迁移到ASP.NET MVC,并且我在考虑避免使用Models和Entity Framework.相反,我将使用方法来直接访问数据库.

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?

谢谢.

推荐答案

我的问题是可能吗?

My question here is that is it possible?

当然可以.除了一个小小的例外:没有模型的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 :-))

两种方法之间的性能差异是什么?

What is the performance difference between both ways?

您无法获得比原始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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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