I'm building a web application with a mvc project with angularjs on one side and a Webapi on the other.
I will have a lot a views and don't want to create as many viewmodel variants on the webApi side.
Some view are list of items, some view are detail of one item.
I could create 2 viewmodels(light/summary and full/detail) for each business entity in my application but I would have many classes and it could be tricky for maintenance or evolution.
My idea was to create less viewmodels and have attributes to specify the properties I want. Light result json or a complete one.
Ok, It's obviously not clear. Please stay with me
public class BookVM
{
[WebApiRef]
public long Id { get; set; }
[WebApiRef]
public String Title { get; set; }
public DateTime Date { get; set; }
public float Price { get; set; }
public AuthorVM Author { get; set; }
}
public class AuthorVM
{
[WebApiRef]
public long Id { get; set; }
[WebApiRef]
public String Name { get; set; }
public int Age { get; set; }
public String Country { get; set; }
public List<BookVM> Books { get; set; }
[...]
}
For a page listing books I expect a list of serialized BookMV in json like so :
[
{
"Id": 1,
"Title": "The Lord of the Rings",
"Date": "29\/07\/1954",
"Price": "23",
"Author": {
"Id": 854,
"Name": "J. R. R. Tolkien"
}
},
{
"Id": 2,
"Title": "The Hobbit",
"Date": "21\/08\/1937",
"Price": "12",
"Author": {
"Id": 854,
"Name": "J. R. R. Tolkien"
}
},
{
"Id": 3,
"Title": "A Game of Thrones",
"Date": "06\/08\/1996",
"Price": "17",
"Author": {
"Id": 157,
"Name": "George R. R. Martin"
}
}
]
for a page listing authors :
[
{
"Id": 854,
"Name": "J. R. R. Tolkien",
"books": [
{
"Id": 1,
"Title": "The Lord of the Rings"
},
{
"Id": 2,
"Title": "The Hobit"
}
]
},
{
"Id": 157,
"Name": "George R. R. Martin",
"books": [
{
"Id": 3,
"Title": "A Game of Thrones"
}
]
}
]
for a page of an author :
{
"Id": 157,
"Name": "George R. R. Martin",
"books": [
{
"Id": 3,
"Title": "A Game of Thrones"
}
]
}
The attribute [WebApiRef] would define the properties to include in the attached entity but would not be used on the main type.
So depending on the type being serialized/deserialized, the attribute would be use to define what to keep in the attached entity inside.
I hope this make sense...
And my question is what I should do to acheive this ?
I read about Formatters, ContractResolvers, I'm not sure where or what to overloads.