简体   繁体   English

如何将扁平化的ViewModel用于Web API方法

[英]How to use flattened ViewModel for a Web API method

I am creating a Web API service that acts as a facade for my clients to a more complex messaging API on the backend. 我正在创建一个Web API服务,该服务充当客户端到后端上较复杂的消息传递API的外观。 The .XSD that represents the calls I need to make to the backend API is obviously not something I want them to understand. 表示我需要对后端API进行调用的.XSD显然不是我希望他们理解的东西 My goal is to flatten out the required elements in a ViewModel class that can be used by the client. 我的目标是弄平客户端可以使用的ViewModel类中的必需元素。 My POST might be something like below: 我的POST可能如下所示:

public HttpResponseMessage Post(FlattenedViewModel flattenedViewModel)
{

}

The idea of the flattened view model is to prevent my clients from having to understand any complex structuring of data to call my API. 平面视图模型的思想是防止我的客户不得不理解任何复杂的数据结构来调用我的API。 It's a lot easier to submit this (could be JSON or XML): 提交此代码(可以是JSON或XML)要容易得多:

<PersonFirstName>John</PersonFirstName>
<PersonLastName>Smith</PersonLastName>
<PersonPhone>123-456-7890</PersonPhone>

than this: 比这个:

<Person>
  <Name>
    <FirstName>John</FirstName>
    <LastName>Smith</LastName>
  </Name>
  <Communication>
     <Type>
       <Phone>123-456-7890</Phone>
     </Type>
  </Communication>
</Person>

I understand creating the class structure to represent the 2nd example is not difficult and easy for all of us to understand. 我知道创建代表第二个示例的类结构并不容易,而且对我们所有人来说都很容易。 However, my real .XSD is about 50x this example. 但是,在此示例中,我真正的.XSD约为50倍。 My goal is to provide an easier interface and ability to have a flattened view, so please use that as a constraint of this question. 我的目标是提供一个更简单的界面并具有扁平化视图的功能,因此请使用它作为此问题的约束。 Imagine it like a user was entering data on a form and pressed submit; 想象一下,就像用户在表单上输入数据并按提交一样。 a form is like a flattened view of data to be entered. 表单就像要输入的数据的拼合视图。

The hurdles I am encountering are the following: 我遇到的障碍如下:

  1. Having a node that can repeat a finite set of times is solvable. 具有可以重复有限时间集的节点是可解决的。 However, nodes with the following constraint on the .xsd: maxOccurs="unbounded" do not appear to be initially doable with a flattened view. 但是,在.xsd上具有以下约束的节点: maxOccurs="unbounded"最初在平面视图下似乎不可行。 Is there another way of doing this so I don't have to introduce a collection? 还有另一种方法可以使我不必引入收藏吗? Or can I introduce a collection but still allow the user to not have to understand a complex structure (like my 1st example)? 还是可以介绍一个集合,但仍然允许用户不必理解复杂的结构(例如我的第一个示例)? Please provide an example of what that would look like if possible. 请提供一个示例,说明可能的情况。

  2. I have node names that are repeated among different parts of the .xsd but are unrelated. 我的节点名称在.xsd的不同部分之间重复,但是不相关。 For example the node ID or Date . 例如,节点IDDate My solution is to append the parent node name to the value to create a property like SubmitDate or PersonID . 我的解决方案是将父节点名称附加到该值,以创建一个属性,如SubmitDatePersonID The issue I now have is my ViewModel class property names don't match the ones of my entities that must be mapped to in the domain model. 我现在遇到的问题是我的ViewModel类属性名称与必须在域模型中映射到的实体名称不匹配。 I'm using ValueInjecter, so is there any type of streamlined way I can still map properties to other classes that have different names (ie annotation or something)? 我正在使用ValueInjecter,所以有什么简化的方式可以将属性映射到其他具有不同名称(例如注释或其他名称)的类?

Any help is appreciated, thank you! 任何帮助表示赞赏,谢谢!

I believe the answer lies in creating custom injections for ValueInjector to use and then simply making a call to 'InjectFrom' to invoke them... 我相信答案在于为ValueInjector创建自定义注入以供使用,然后简单地调用“ InjectFrom”来调用它们...

_person.InjectFrom<CustomPersonInjection>(flattenedViewModel);

I had a quick look around for some specific examples that might help you but could find anything within a reasonable time frame (they're out there though, google 'valueinjecter custom injections'). 我快速浏览了一些特定的示例,这些示例可能会为您提供帮助,但可以在合理的时间范围内找到任何内容(不过,谷歌提供了“ valueinjecter自定义注入”功能)。

Here are some links to get you started: 以下是一些帮助您入门的链接:

Deep Cloning example: http://valueinjecter.codeplex.com/wikipage?title=Deep%20Cloning&referringTitle=Home 深层克隆示例: http : //valueinjecter.codeplex.com/wikipage?title= Deep%20Cloning& referringTitle=Home

Custom Convention Injection: Using ValueInjecter to map between objects with different property names 自定义约定注入: 使用ValueInjecter在具有不同属性名称的对象之间进行映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM