我刚刚开始使用并爱上了MVC的设计模式。
我唯一讨厌的宠物就是 它似乎产生了很多重复代码。例如。
我有一个标准的MVC App和我的DB(模型)在一个项目中,与我的控制器/观点/视觉模型在另一个项目中分离,又与我的测试方法在另一个项目中分离。所有工作都很好。
Models: Now, I have a bunch of nice EF4 classes in my DB project, which I have to use ViewModels for in my real project to access my data. No problem here.
Controllers: However, every controller I have, essentially does the very same thing. It gets and sets the data from the ViewModels so while each controller is different in that it gets only different data, they are all essentially doing the very same job, in the very same way. (I currently have 9, but this could easily explode to well over 50).
例如:
public class Dummy1Controller : Controller
{
private MyProj.Data.Entities _entities = new Data.Entities();
private MyProj.Data.Entities2 _coreEntities = new Data.Entities2();
//GET: /Customers/
public ActionResult Index()
{
if (_entities.table1.Count() == 0) return View();
var pastObj = _entities.table1.First();
return View(new Table1ViewModel()
{
Id = pastObj.Id,
FirstName = pastObj.FirstName,
LastName = pastObj.LastName,
.
.
.
.
});
}
}
public class Dummy2Controller : Controller
{
private MyProj.Data.Entities _entities = new Data.Entities();
private MyProj.Data.Entities2 _coreEntities = new Data.Entities2();
//GET: /Vehicles/
public ActionResult Index()
{
if (_entities.table2.Count() == 0) return View();
var pastObj = _entities.table2.First();
return View(new Table1ViewModel()
{
RegNo = pastObj.RegNo,
Make = pastObj.Make,
Model = pastObj.Model,
.
.
.
.
});
}
}
public class Dummy3Controller : Controller
{
private MyProj.Data.Entities _entities = new Data.Entities();
private MyProj.Data.Entities2 _coreEntities = new Data.Entities2();
//GET: /Invoices/
public ActionResult Index()
{
if (_entities.table3.Count() == 0) return View();
var pastObj = _entities.table3.First();
return View(new Table1ViewModel()
{
InvNo = pastObj.InvNo,
Amount = pastObj.Amount,
Tax = pastObj.Tax,
.
.
.
.
});
}
}
Views: Every view generated from the contollers work great. Execpt, that only thing that changes is the data (fields with labels and text boxes). Once again, they all do the very same job (but with different datasets).
@model MyProject.Web.ViewModels.Table1ViewModel
@{
ViewBag.Title = "Index";
}
<link href="@Url.Content("~/Content/CSS/GenericDetailStyles.css")" rel="stylesheet" type="text/css" />
<section id="content">
<div id="table">
<div>
<h2>Customer</h2>
</div>
<div class="row">
<div class="left">@Html.LabelFor(x=>x.Id)</div>
<div class="right">@Html.TextBoxFor(model => model.Id)</div>
</div>
<div class="row">
<div class="left">@Html.LabelFor(x=>x.FirstName)</div>
<div class="right">@Html.TextBoxFor(model => model.FirstName)</div>
</div>
<div class="row">
<div class="left">@Html.LabelFor(x=>x.LastName)</div>
<div class="right">@Html.TextBoxFor(model => model.LastName)</div>
</div>
.
.
.
.
</div>
</section>
@{Html.RenderAction("Index", "FooterPartial");}
--------------------------------------------------------------------------------------
@model MyProject.Web.ViewModels.Table2ViewModel
@{
ViewBag.Title = "Index";
}
<link href="@Url.Content("~/Content/CSS/GenericDetailStyles.css")" rel="stylesheet" type="text/css" />
<section id="content">
<div id="table">
<div>
<h2>Vehicle</h2>
</div>
<div class="row">
<div class="left">@Html.LabelFor(x=>x.RegNo)</div>
<div class="right">@Html.TextBoxFor(model => model.RegNo)</div>
</div>
<div class="row">
<div class="left">@Html.LabelFor(x=>x.Make)</div>
<div class="right">@Html.TextBoxFor(model => model.Make)</div>
</div>
<div class="row">
<div class="left">@Html.LabelFor(x=>x.PatientID)</div>
<div class="right">@Html.TextBoxFor(model => model.Model)</div>
</div>
.
.
.
.
</div>
</section>
@{Html.RenderAction("Index", "FooterPartial");}
--------------------------------------------------------------------------------------
@model MyProject.Web.ViewModels.Table3ViewModel
@{
ViewBag.Title = "Index";
}
<link href="@Url.Content("~/Content/CSS/GenericDetailStyles.css")" rel="stylesheet" type="text/css" />
<section id="content">
<div id="table">
<div>
<h2>Invoice</h2>
</div>
<div class="row">
<div class="left">@Html.LabelFor(x=>x.InvNo)</div>
<div class="right">@Html.TextBoxFor(model => model.InvNo)</div>
</div>
<div class="row">
<div class="left">@Html.LabelFor(x=>x.Amount)</div>
<div class="right">@Html.TextBoxFor(model => model.Amount)</div>
</div>
<div class="row">
<div class="left">@Html.LabelFor(x=>x.Tax)</div>
<div class="right">@Html.TextBoxFor(model => model.Tax)</div>
</div>
.
.
.
.
</div>
</section>
@{Html.RenderAction("Index", "FooterPartial");}
Problem: I want to make a single controller, and make it dynamic. So that it can read data from different view models. (Why have 9 or 50 controllers esentially doing the same job)
然后,我想对视图也这样做。这样,不同的字段就可以动态生成。 (为什么有9或50个视图都做同样的工作) 如果视图基于控制器, 则该视图应该能够根据其属性而改变 。
我所要做的基本上就是让控制器从 X 或 VM - Y 或 VM - Z 的视图模型中读取它, 它应该能够生成属性, 恢复相关数据, 并将其传递到视图中, 接收后, 它将生成带有标签和文字框的字段 。
我想我要知道是否有办法使用反射来做到这一点。 因为查看模型是基本类别, 具有简单的属性。 人们可能会创建一个基础控制器类, 该类可使用特定查看模型对象的读取方法, 获取其属性, 也可以在相关表格中读取, 并将该表格中的字段与该类属性匹配。 最后, 人们可以从表格中记录下来显示。 这样的话, 可以使用某种剃刀、 c# 或 javascript 来自动生成此视图 。
任何教书,如果有可能或不可以的话,都欢迎。