MVC3 - 的ViewModels和控制器functionalty:建议设计模式 [英] MVC3 – ViewModels and controller functionalty: suggested design patterns

查看:161
本文介绍了MVC3 - 的ViewModels和控制器functionalty:建议设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个简单的基于MVC3票进入现场为低于可用的呼叫中心应用,并在尝试重构我的原型,以便更好地坚持以设计模式部分,使其更易于维护的向前发展,但主要是作为一种学习行使。
面向用户的观点是,除了少数面板允许不同的资源类型选择包括基本用户信息的形式。每个资源类型(硬件,软件等)显示在相同的方式:使用双,过滤列表框与添加/删除按钮,一个可选的正当理由的textarea有条件地显示,如果一个请求的资源需要理由,和一般性意见。
我已经建立了以下视图模型的各个面板:

I have built a simple MVC3-based ticket entry site for a less-than-usable call center application and am attempting to refactor my prototype to better adhere to design patterns partly to make it more maintainable going forward but mostly as a learning exercise. The user-facing view is a form consisting of basic user information in addition to a few panels allowing selection of various resource types. Each resource type (hardware, software, etc) is displayed in the same way: using dual, filterable listboxes with add/remove buttons, an optional "justification" textarea that conditionally displays if a requested resource requires justification, and general comments. I have built the following ViewModel for the individual panels:

public class RequestableList
{
    // list of requestable items ids requiring justification
    private List<string> _restrictedItems = new List<string>();
    public List<string> RestrictedItems
    {
        get { return _restrictedItems; }
        set { _restrictedItems = value; }
    }

    // the key-value pairs from which to populate available items list
    private Dictionary<string, string> _availableItems = new Dictionary<string, string>();
    public Dictionary<string, string> AvailableItems
    {
        get { return _availableItems; }
        set { _availableItems = value; }
    }

    // item ids requested by user
    private List<string> _requestedItems = new List<string>();
    public List<string> RequestedItems
    {
        get { return _requestedItems; }
        set { _requestedItems = value; }
    }
}

主视图模型然后根据需要由多RequestableLists的

The main ViewModel is then comprised of multiple RequestableLists as necessary:

public class SimpleRequestViewModel
{
    public UserInfo userInfo { get; set; }
    public RequestableList Software {get;set;}
    public RequestableList Hardware {get;set;}
    public RequestableList Access {get;set;}
    public string SoftwareAdditionalInfo { get; set; }
    public string HardwareAdditionalInfo { get; set; }
    public string AccessFileMailShare { get; set; }
    public string AccessAdditionalInfo { get; set; }
    public string SoftwareJustification { get; set; }
    public string HardwareJustification { get; set; }
    public string AccessJustification { get; set; }
    public string Comment { get; set; }
}

我已经创造了SimpleRequestViewModel(及其变种)一个强类型的视图和一个强类型EditorTemplate为RequestableList的直径达双列表框,过滤和jQuery。所有呈现良好,工作正常但是code目前的气味。

I have created a strongly typed view for SimpleRequestViewModel (and its variant) and a strongly typed EditorTemplate for RequestableList that wires up the dual listboxes, filtering, and jquery. All renders well and is working but the code currently smells.

在发布到控制器,如果模型是有效的,我必须要想在呼叫中心的应用程序创建一个新的门票它翻译成可读的文本描述。试图设计其他类翻译的的ViewModels时感觉不对有执行该翻译成可读文本的控制器,但我碰到的障碍。

When posting to the controller, if the model is valid I must translate it into a readable text description in order to create a new ticket in in the call center app. It doesn’t feel right to have the controller performing that translation into readable text but I run into hurdles when trying to design another class to translate the viewmodels.


  1. 只有选定的项目值张贴所以翻译请求转换成文本之前,我必须先查找所提供的数值适当的文本(他们要求的描述)。该控制器是目前唯一有权访问该查找查询呼叫中心的数据模型的唯一对象。

  2. 有含RequestableLists的不同的组合,所以任何翻译者必须能够将各种组合的翻译是2相似的ViewModels。一个只有硬件和软件,另外可能有硬件软件,以及一些更RequestableLists。

我认为,直接覆盖在视图模型的ToString(),但不喜欢这样的业务逻辑(有条件的渲染)出现,并再次,一旦公布,该视图模型不包含在列表框中,因此所选项目的文本将需要访问的数据模型。
帐值的翻译文本,因为它是在控制器目前处理的气味,因为它在一个开关语句的处理。该控制器采用每次贴RequestableList并填充原来的可用领域它建立新的售票描述之前。

I considered overriding ToString() directly in the ViewModel but didn’t like that business logic (conditional rendering) there, and again, once posted, the ViewModel doesn’t contain the text for the selected items in the listbox so it would need access to the data model. The translation of posted values to text as it is currently handled in the controller smells as it’s handled in a switch statement. The controller takes each posted RequestableList and populates the original "Available" fields before it builds the new ticket description.

switch (requestCategory)
{
    case RequestableCategory.Software:
        itemList = sde.GetSoftware();
        break;
    case RequestableCategory.Hardware:
        itemList = sde.GetHardware();
        break;
    case RequestableCategory.Access:
        itemList = sde.GetAccess();
        break;
    case RequestableCategory.Telecom:
        itemList = sde.GetTelecom();
        break;
    default:
        throw new ArgumentException();
}

所以,我的问题(S):

So, my question(s):


  1. 什么模式是你会推荐进行发布视图模型来描述车票翻译?技术

  2. 你怎么通常处理与选择框唯一岗位价值的问题,当你需要的文本和价值?

  3. 有没有更好的办法,我会处理这个问题?

再次我希望这是我的学习经验,并很乐意提供,如果需要更多的信息或说明以上。

Again, I am hoping this is a learning experience for me and am more than willing to provide additional information or description if needed.

推荐答案

一些建议:


  1. 摘要,做呼叫中心提交到自己的类中的逻辑。 (从控制器)提供它需要访问呼叫中心DB任何依赖关系。有不同的方法来处理不同类型的使用重载视图模型。 presumably的描述来自于数据库,以便您可以提取基于这个类中的值从DB的描述。这个类也可以采取建立您的视图模型显示的行为以及责任。注:在这个模式的类可以直接与DB进行交互,通过一个存储库,甚至是通过Web服务/的API。

  1. Abstract the logic that does the call center submission into its own class. Provide (from the controller) whatever dependencies it needs to access the call center DB. Have different methods to handle the various types of view models using overloading. Presumably the descriptions come from the DB so you can extract the description from the DB based on the value in this class. This class could also take responsibility for building your view models for the display actions as well. Note that with this pattern the class can interact with the DB directly, through a repository, or even via web services/an API.

使用一个实现了高速缓存,如果性能是仰视从DB第二次说明一个问题,一个存储库模式。我怀疑这会不会,除非你的呼叫中心是非常大的,但是这将是优化查询逻辑的地方。存储库可以是控制器传递到提交类的东西。

Use a repository pattern that implements some caching if performance is an issue in looking up the description from the DB the second time. I suspect it won't be unless your call center is very large, but that would be the place to optimize the query logic. The repository can be the thing that the controller passes to the submission class.

如果您不需要直接访问数据库中的控制器,考虑通过券商类作为直接依赖关系。

If you don't need to access the DB directly in the controller, consider passing the broker class as a dependency directly.

这可能看起来像:

private ICallCenterBroker CallCenterBroker { get; set; }

public RequestController( ICallCenterBroker broker )
{
   this.CallCenterBroker = broker;
   // if not using DI, instantiate a new one
   // this.CallCenterBroker = broker ?? new CallCenterBroker( new CallCenterRepository() );
}

[HttpGet]
public ActionResult CreateSimple()
{
    var model = this.CallCenterBroker.CreateSimpleModel( this.User.Identity.Name );
    return View( model );
}


[HttpPost]
public ActionResult CreateSimple( SimpleRequestViewModel request )
{
    if (Model.IsValid)
    {
       var ticket = this.CallCenterBroker.CreateTicket( request );
       // do something with ticket, perhaps create a different model for display?
       this.CallCenterBroker.SubmitTicket( ticket );
       return RedirectToAction( "index" ); // list all requests?
    }
    return View();
}

这篇关于MVC3 - 的ViewModels和控制器functionalty:建议设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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