流利的CRUD与NHibernate ASP.NET MVC [英] Fluent CRUD with NHibernate ASP.NET MVC

查看:137
本文介绍了流利的CRUD与NHibernate ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC使得它可以很容易地创建简单,平面物体编辑模板。但是,管理与使用类似功能NHibernate当几个迂回复杂的对象CRUD,就不那么明显了。

ASP.NET MVC makes it really easy to create editing templates for simple, flat objects. But managing CRUD for complex objects with several indirections when using something like Fluent NHibernate, is not so obvious.

为了说明一个简单的发票管理我的问题,的发票的实体拥有的项目的属性:

To illustrate my question with a simple Invoice manager, Invoice entities have a Project property:

public class Invoice
{
    ...
    Project Project { get; set; } // where Project class has an Id and a Name
}

这是在通过功能NHibernate映射到项目

...我的 InvoiceMap

...which is mapped to the Projects table via Fluent NHibernate in my InvoiceMap:

References(x => x.Project).Inverse();

在过去,我的发票实体将有引用我的数据库的一个项目,这使得它更容易渲染项目ID的选择列表中的专案编号属性,但更难输出的意见,例如:

In the past, my Invoice entities would have a ProjectId property that referenced a project in my database, which made it easier to render a select list with Project IDs, but harder to output views, e.g.:

public class Invoice
{
    ...
    Guid ProjectID { get; set; }
}

但自从我开始使用功能NHibernate,我不想弄脏了我更多的ID的控制器。

But since I started using Fluent NHibernate, I don't want to dirty up my controller with more IDs.

[HttpGet]
public ActionResult Edit(Guid id)
{
    var invoice = _unitOfWork.CurrentSession.Get<Invoice>(id);
    return View(invoice);
}

[HttpPost]
public ActionResult Edit(Invoice invoice)
{
    /* How to deal with invoice.Project mapping here without introducing a second
       action parameter, e.g. Guid projectId ? */

    _unitOfWork.CurrentSession.SaveOrUpdate(invoice);
    _unitOfWork.Commit();

    return RedirectToAction("Details", new { id = invoice.Id });
}

所引用的项目的不应该是从我的发票/ Edit.cshtml 的视图编辑的,但我希望能够选择哪些项目发票应该属于

The referenced Project should not be editable from my Invoice/Edit.cshtml view, but I want to be able to choose which Project the Invoice should belong to.

如何设计我的视图和控制器,使CRUD方便不会搞乱我的不平整实体字段引用的ID控制器动作?

How should I design my views and controllers to make CRUD easy without cluttering up my controller action with reference IDs that are not flat entity fields?

推荐答案

我建议你创建你的ProjectController一个动作(假设你有一个),其具有这简直是项目列表的局部视图。

I'd suggest you create an action on your ProjectController (assuming you have one) which has a partial view which is simply a list of the projects.

例如

 public class ProjectController : Controller
 {
      public ActionResult ProjectsList()
      {   
          var projects = GetAllProjects();
          return View(projects);
      }
 }

项目/ ProjectsList.cshtml

Project/ProjectsList.cshtml

@model IEnumerable<MyProject.Data.Models.Project>
@{ Layout = null }
<select name="project">
 @foreach(var project in Model)
 {
   <option value="@project.Id">@project.Name</option>
 } 
</select>

发票/ Edit.cshtml

Invoice/Edit.cshtml

<label>Title</label>
<input name="title" />

@Html.Action("ProjectsList","Project")

然后,我会改变你编辑的发票采取的输入模式,而不是直的发票和标识在行动查找项目。

I would then change you Edit invoice to take an input model rather than the straight invoice and look up the project by Id in that action.

心连心

这篇关于流利的CRUD与NHibernate ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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