检测修改记录与新记录,并使用 ASP.NET MVC 2、EF v4、POCO 和存储库模式更新修改记录
我正在使用 ASP.NET MVC 2、实体框架 v4、ADO.NET C# POCO 实体生成器和存储库模式。很多流行语!
设置:
我有一个存储库,IPartiesRepository。我有一个 Party 对象和一个 *Party_Identification* 对象。政党是个人或组织的通用术语。
在 SQL/物理模型级别,Party 表有一个主键 PartyId。 Party_Identifications 表有一个由三个字段组成的复合键:PartyId、IdentificationTypeCode 和IdentificationStartDate。
我有一个强类型(对于政党)视图,其中列出了政党信息和相关标识。用户可以编辑一方的身份;编辑链接是通过 ActionLink 生成的,如下所示:
<h3>Identification Data</h3>
<% foreach (var Identification in Model.Party_Identifications) { %>
<%: Identification.IdentificationTypeCode %>: <%: Identification.IdentificationValue %> - <%: Html.ActionLink("Edit", "EditIdentification", new { partyId = Identification.PartyId, idTypeCode = Identification.IdentificationTypeCode, startDate = Identification.PartyIdentificationStartDate })%><br />
<% } %>
PartyController 中的 EditIdentification() 方法如下所示:
public ActionResult EditIdentification(int partyId, string idTypeCode, string startDate)
{
var party = partiesRepository.GetPartyById(partyId);
var identification = party.Party_Identifications.Where(x => x.PartyId == partyId && x.IdentificationTypeCode == idTypeCode && x.PartyIdentificationStartDate == DateTime.Parse(startDate)).First();
return View("EditIdentification", identification);
}
EditIdentification 视图允许用户通过基本表单修改标识记录:
<% using (Html.BeginForm("EditIdentification", "Party")) { %>
<%: Html.EditorForModel()%>
<input type="submit" value="Save" />
<% } %>
问题:
我在 PartyController 中有另一个处理保存的 EditIdentification() 方法。我试图查看我们是否正在更新现有的标识记录,或创建一个新的标识记录(我在 PartyController 中还有一个 Create() 方法,它返回 < em>EditIdentification 视图,以 Party_Identification 对象作为模型)。
我知道有一种更好的方法可以完成我下面正在做的事情(具体来说,检测修改的记录与新记录,并更新修改的记录)——我该如何改进?
[HttpPost]
public ActionResult EditIdentification(Party_Identification partyidentification)
{
TryUpdateModel(partyidentification);
if (partyidentification.CreationDate == DateTime.Parse("01/01/0001"))
{
partyidentification.CreationDate = DateTime.Now;
partyidentification.CreatedByName = User.Identity.Name.ToString();
partyidentification.ModificationDate = DateTime.Now;
partyidentification.ModifiedByName = User.Identity.Name.ToString();
}
// TODO: Validation goes here...
if (ModelState.IsValid)
{
var party = partiesRepository.GetPartyById(partyidentification.PartyId) as Party;
int partyCount = party.Party_Identifications
.Where(x => x.PartyId == partyidentification.PartyId &&
x.IdentificationTypeCode == partyidentification.IdentificationTypeCode &&
x.PartyIdentificationStartDate == partyidentification.PartyIdentificationStartDate)
.Count();
if (partyCount == 0)
{
party.Party_Identifications.Add(partyidentification);
}
else
{
var idtoupdate = party.Party_Identifications
.Where(x => x.PartyId == partyidentification.PartyId &&
x.IdentificationTypeCode == partyidentification.IdentificationTypeCode &&
x.PartyIdentificationStartDate == partyidentification.PartyIdentificationStartDate)
.First();
idtoupdate.IdentificationValue = partyidentification.IdentificationValue;
idtoupdate.PartyIdentificationEndDate = partyidentification.PartyIdentificationEndDate;
idtoupdate.ModificationDate = DateTime.Now;
idtoupdate.ModifiedByName = User.Identity.Name.ToString();
}
partiesRepository.SaveParty(party);
return View("Edit", party);
}
else
{
return View("Edit", partyidentification);
}
}
I am using ASP.NET MVC 2, Entity Framework v4, the ADO.NET C# POCO Entity Generator, and the Repository pattern. Lots of buzzwords!
The Setup:
I have a repository, IPartiesRepository. I have a Party object and a *Party_Identification* object. Party being a generic term for a person or organization.
On the SQL/physical model level, the Party table has a single primary key, PartyId. The Party_Identifications table has a composite key made up of three fields -- PartyId, IdentificationTypeCode, and IdentificationStartDate.
I have a strongly typed (to Party) view which lists Party information and associated identifications. A user can edit a party's identification; the edit links are generated via an ActionLink that looks like so:
<h3>Identification Data</h3>
<% foreach (var Identification in Model.Party_Identifications) { %>
<%: Identification.IdentificationTypeCode %>: <%: Identification.IdentificationValue %> - <%: Html.ActionLink("Edit", "EditIdentification", new { partyId = Identification.PartyId, idTypeCode = Identification.IdentificationTypeCode, startDate = Identification.PartyIdentificationStartDate })%><br />
<% } %>
The EditIdentification() method in the PartyController looks like:
public ActionResult EditIdentification(int partyId, string idTypeCode, string startDate)
{
var party = partiesRepository.GetPartyById(partyId);
var identification = party.Party_Identifications.Where(x => x.PartyId == partyId && x.IdentificationTypeCode == idTypeCode && x.PartyIdentificationStartDate == DateTime.Parse(startDate)).First();
return View("EditIdentification", identification);
}
The EditIdentification view allows the user to modify the identification record via a basic form:
<% using (Html.BeginForm("EditIdentification", "Party")) { %>
<%: Html.EditorForModel()%>
<input type="submit" value="Save" />
<% } %>
The Problem:
I have another EditIdentification() method in the PartyController that handles the save. I am attempting to see if there we are updating an existing identification record, or creating a new one (I also have a Create() method in the PartyController that returns the EditIdentification view with a Party_Identification object as the model).
I know there's a better way to do what I'm doing below (specifically, detecting a modified vs new record, and updating a modified record) -- how can I improve this?
[HttpPost]
public ActionResult EditIdentification(Party_Identification partyidentification)
{
TryUpdateModel(partyidentification);
if (partyidentification.CreationDate == DateTime.Parse("01/01/0001"))
{
partyidentification.CreationDate = DateTime.Now;
partyidentification.CreatedByName = User.Identity.Name.ToString();
partyidentification.ModificationDate = DateTime.Now;
partyidentification.ModifiedByName = User.Identity.Name.ToString();
}
// TODO: Validation goes here...
if (ModelState.IsValid)
{
var party = partiesRepository.GetPartyById(partyidentification.PartyId) as Party;
int partyCount = party.Party_Identifications
.Where(x => x.PartyId == partyidentification.PartyId &&
x.IdentificationTypeCode == partyidentification.IdentificationTypeCode &&
x.PartyIdentificationStartDate == partyidentification.PartyIdentificationStartDate)
.Count();
if (partyCount == 0)
{
party.Party_Identifications.Add(partyidentification);
}
else
{
var idtoupdate = party.Party_Identifications
.Where(x => x.PartyId == partyidentification.PartyId &&
x.IdentificationTypeCode == partyidentification.IdentificationTypeCode &&
x.PartyIdentificationStartDate == partyidentification.PartyIdentificationStartDate)
.First();
idtoupdate.IdentificationValue = partyidentification.IdentificationValue;
idtoupdate.PartyIdentificationEndDate = partyidentification.PartyIdentificationEndDate;
idtoupdate.ModificationDate = DateTime.Now;
idtoupdate.ModifiedByName = User.Identity.Name.ToString();
}
partiesRepository.SaveParty(party);
return View("Edit", party);
}
else
{
return View("Edit", partyidentification);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎在重复不需要的代码:
You appear to be repeating your code which it not needed: