I'd like to provide some custom model binding for my queryable web api, namely for the following interfaces:
IQueryOptions`1
ISortingOptions`2
implementingIQueryOptions`1
IPagingOptions`2
implementingISortingOptions`2
Depending on the parameter specified on the controller action, I want to provide the correct model binder.
public void Test([ModelBinding] IPagingOptions<MyEntity, int> pagingOptions)
// -> PagingOptionsModelBinder
My question is: Should there be a model binder provider for each model binder or should the model binder work as service locator and provide a model binder for each type to be bound?
e.g..
public QueryOptionsModelBinderProvider : ModelBinderProvider {
public override IModelBinder GetBinder(HttpConf...
if(modelType.IsOfGeneric((typeof(ISortingOptions<,>))
return new SortingOptionsModelBinder();
if(modelType.IsOfGeneric((typeof(IPagingOptions<,>))
return new PagingOptionsModelBinder();
...
or...
public SortingOptionsModelBinderProvider
public PagingOptionsModelBinderProvider
// etc.
I'm asking this because on one hand, service locating is a (horrible, IMHO) anti pattern since for every new type to be bound, I need to alter the model binder provider, but on the other if it isn't being done here, the responsibility of finding the right provider is just delegated to ASP.NET WebApi, where I need to register a new provider everytime.
Aucun commentaire:
Enregistrer un commentaire