0

I have a ASP.NET WEB API application that is very "GET" intensive. Most of the methods accept variation of params where method signature varies

For example

[HttpGet]
public async Task<HttpResponseMessage> GetProducts(int year)
{
    //...
}

[HttpGet]
public async Task<HttpResponseMessage> GetProducts(int year, string category)
{
    //...
}

[HttpGet]
public async Task<HttpResponseMessage> GetProducts(int year, string category, int minPrice)
{
    //...
}

[HttpGet]
public async Task<HttpResponseMessage> GetProducts(int year, string category, decimal minPrice, decimal maxPrice)
{
    //...
}

I want to use data annotations from System.ComponentModel.DataAnnotations but I noticed that I must convert all simple url parameters to models? In another words, I have to create input model for every method variation (see example below).

[HttpGet]
public async Task<HttpResponseMessage> GetProducts([FromUri] yearModel)
{
    //...
}

public class YearModel
{
    [Required]
    [Range(1966, 2013)]
    public int Year { get; set; }
}

If I want to preserve my method variations and still use data annotations is there a way I could use them without creating all the input models (is there a way to use them "inline" somehow?)?

zam6ak
  • 7,229
  • 11
  • 46
  • 84
  • You can use ViewModels with your WebAPI. You don't have to only use POCOs for your webAPI. – Michael Dunlap Feb 18 '13 at 17:46
  • I was convinced that ViewModels *are* POCOs...:) – zam6ak Feb 18 '13 at 17:55
  • Isn't a ViewModel a POCO? – lintmouse Feb 18 '13 at 19:22
  • I meant vs POCOs that are persisted with EF or other method. – Michael Dunlap Feb 18 '13 at 21:54
  • POCOs are not specific to persistent frameworks by definition. – Dmitry S. Feb 19 '13 at 02:08
  • 1
    Usually the term POCO is used to indicate that the classes representing your domain are persistence-ignorant (they don't have any knowledge about the framework used to manage persistence = they do not know class like EntityObject), so usually you use the term in this context. ViewModels represent the "soul" of your view without knowing how to represent this information. In the web often this classes are also POCO, because they know only String, int, bool, etc., but this is not a requirement: i.e. in WPF ViewModels can have ObservableCollection, a concept tight to the presentation framework – Daniele Armanasco Feb 24 '13 at 10:45

0 Answers0