我想使用ASP.NET MVC没有EF和可能不LINQ。首先,我在VS2010中创建了基本的ASP.net mvc模板上的一个表/模型/控制器/视图(仅实现了索引页和索引方法)。我目前正在尝试/错误,因为网络上的所有教程都基于EF,并且我拥有嵌入式c背景。ASP.NET MVC 4项目没有任何ORM
我的问题是:
体系结构是否正确?
只见库模式在一些开源项目使用,如nearforums。它的优点是什么?它如何适应这个问题,是否取代DAL?
我应该通过数据表,以查看或对象表示的数据表?
我可以传递比一个模型剃刀页吗?我如何列出2个不同的表格?
是更好地把所有DAL代码在一个文件?
DAL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
namespace MvcApplication1.Models
{
public class DAL
{
public DAL()
{ }
public DataTable getDataTable(string tableName)
{
using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQLConn"].ConnectionString))
{
using (MySqlCommand command = conn.CreateCommand())
{
command.CommandText = "select * from " + tableName;
command.CommandType = CommandType.Text;
conn.Open();
var table = new DataTable();
table.Load(command.ExecuteReader());
return table;
}
}
}
}
}
,它表示代码产品表Product.cs类:
namespace MvcApplication1.Models
{
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public string Producer { get; set; }
public int UnitPrice { get; set; }
}
}
ProductController.cs
namespace MvcApplication1.Controllers
{
public class ProductController : Controller
{
//
// GET: /Product/
DAL dal = new DAL();
public ActionResult Index()
{
DataTable dt = dal.getDataTable("Product");
Product[] products = new Product[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
products[i] = new Product();
products[i].ProductId = (int)dt.Rows[i]["ProductId"];
products[i].Name = (string)dt.Rows[i]["Name"];
products[i].Producer = (string)dt.Rows[i]["Producer"];
products[i].UnitPrice = Decimal.ToInt32((decimal)dt.Rows[i]["UnitPrice"]);
}
return View(products);
}
........
........
}
Index.cshtml:
@model IEnumerable<MvcApplication1.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProductId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Producer)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnitPrice)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ProductId }) |
@Html.ActionLink("Details", "Details", new { id=item.ProductId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ProductId })
</td>
<br>
</tr>
}
不幸的是,你说你不使用OR/M,但是你使用的是DataTable,它是OR/M早期的.NET早期教学方法。我期待您使用DataReader。在这一点上,我相信你应该考虑不使用EF或者NHib或者其他的,但是使用沉重和过时的DataSet模型的优点。 –
我可以问一下不使用EF的原因是什么?您是否意识到将数据表转换为模型类型所需的所有样板代码都容易出现很多错误(投射,魔术字符串等)?此外,性能不会更高,我不明白为什么世界上有人不喜欢使用LinQ。这是人类能够想到的最好的事情。 –
我不使用EF/linq的主要原因是性能。我知道我需要编写样板代码,我更喜欢这种性能开销。 [链接](http://blogs.msdn.com/b/adonet/archive/2012/02/14/sneak-preview-entity-framework-5-0-performance-improvements.aspx) – bilgehan