0

I came across this question when returning a model(Object1) with several dependencies via an IEnumerable<Object1> with REST.

I suddenly realized I may not want to return all this information, and was wondering if I should re-think Object1 itself, or make a ReturnObject1, which only contains the essential information I need.

password
  • 471
  • 3
  • 12
  • 4
    This is very similar to the use of a View Model instead of a Model when dealing with a View. Often times your underlying model may have chained objects that you don't need to return to either the View or in your case the API response. A View Model, or as you termed it "ReturnObject" is the ideal way to go to limit the information returned. You could use LINQ and select an anonymous object as a return result too if you wanted to. – Nick Bork Feb 19 '15 at 20:15
  • @NickBork, thanks, that cleared things up. I am familiar with MVVM, but didn't think about it until you mentioned it. This link [http://stackoverflow.com/questions/20323713/how-to-use-viewmodels-in-asp-net-mvc] also helped to distinguish when to use ViewModels – password Feb 19 '15 at 20:30

1 Answers1

2

Yes, it's better to keep all request/response objects in 'contract' DLL for you REST. And yes - you have to do mapping between 'contract' model and 'internal' model in your controller. Is this case all changes for 'internal' model will be hidden from applications calling your REST.

khablander
  • 159
  • 1
  • 5