简体   繁体   中英

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. The .XSD that represents the calls I need to make to the backend API is obviously not something I want them to understand. My goal is to flatten out the required elements in a ViewModel class that can be used by the client. My POST might be something like below:

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. It's a lot easier to submit this (could be JSON or 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. 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. 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. For example the node ID or Date . My solution is to append the parent node name to the value to create a property like SubmitDate or PersonID . 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. 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)?

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...

_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').

Here are some links to get you started:

Deep Cloning example: http://valueinjecter.codeplex.com/wikipage?title=Deep%20Cloning&referringTitle=Home

Custom Convention Injection: Using ValueInjecter to map between objects with different property names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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