我打算将一个ASP.Net应用程序迁移到ASP.NET MVC,并且我在考虑避免使用模型和实体框架。相反,我将使用方法来直接访问数据库。
我的问题是,这是可能的吗?这两种方式之间的性能差异是什么?
谢谢。
发布于 2013-12-31 17:51:24
,我的问题是,这是可能的吗?
当然,这是可能的。有一个小的例外:没有模型的MVC不是MVC :-)它是VC,我个人从来没有听说过。既不是设计模式,也不是框架。听起来更像(WC :-))
这两种方式之间的性能差异是什么?
你不能得到比原始ADO.NET更快的东西,所以,是的,这将比使用ORM更快。
当然,您将不得不编写更多的代码,因为您仍然有模型来映射查询的结果。不要认为你没有使用EF就解除了你使用模型的责任。另外,不要认为你会使用DataTables。
因此,基本上您将让您的数据层使用这些模型。唯一的区别将是实现。
让我们举个例子。
定义一个模型,该模型将表示您打算在应用程序中使用的业务实体:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public DateTime Dob { get; set; }
}然后定义您的数据访问契约(也称为您愿意使用模型执行的操作):
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
}然后,像往常一样,您可能有一个控制器来处理此存储库:
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具体实现注册到您最喜欢的容器中。
看看事情是如何分层的。现在,如果您正确地编写了当前的ASP.NET应用程序,那么您可能已经拥有了模型、接口和存储库实现。因此,将其迁移到ASP.NET MVC将是小菜一碟,您需要做的就是编写几个视图模型和视图。
发布于 2013-12-31 17:46:45
不使用实体框架很好,使用ADO.NET也可以。是的这是可能的。
如果小心,你可以使你的数据访问代码比EF生成的代码更有效率。不过,维护效率可能会较低,因为EF为您做了很多工作。请记住,维护成本非常昂贵。
但是,我不明白为什么要避免使用模型而使用MVC。我不推荐这种方法。
https://stackoverflow.com/questions/20855188
复制相似问题