1

I am building a mvc based website with ef6 code first type db. Right now i have a web api controller named Categories Controller that works nicely and shells out json data to my js requests.

    // GET: api/Categories
    public IQueryable<Category> GetCategories()
    {
        return db.Categories;
    }

Now I need to wire up same for products, materials and some other entities. What I would like is something like : //GET: api/Data/Categories //GET: api/Data/Products

etc. all wired into one DataController. Is there a way to accomplish this?

For example one Data Controller, with separate region of code for all category specific api actions, product specific api actions and so on. Then I could do /api/Data/Categories/Create or api/Data/Products/Create

codetantra
  • 258
  • 2
  • 11
  • Why you can't return all of them in a class or anonymous object? – adt Jun 01 '15 at 12:42
  • See [Generic WebApi Controller](http://stackoverflow.com/questions/12077361/generic-webapi-controller). Do you mind having a controller class per entity class? If you do, you could [implement your own `IHttpControllerSelector`](http://stackoverflow.com/questions/11886197/what-is-the-equivalent-of-defaultcontrollerfactory-in-asp-net-web-api) that returns `new GenericCRUDController` where `T` is resolved according to the requested controller, mapping to an entity type. – CodeCaster Jun 01 '15 at 12:44

2 Answers2

0

The easiest way to do what you want is to implement an OData controller, or a Breeze controller. They will do all the heavylifting to expose your EF model to in Web APi endpoints:

They're wuite easy to setup and OData is a recognized standard for this kind of task. They both had prebuilt support for oldering, filtering, paging, including related conllections and so. There are Nuget packages to use both of them.

Breeze also has feature-rich clients for JS and C#, and a lot of extra functionality.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
0

Instead of bringing in a new technology, you could handle this by creating a new class that contains both. I am assuming there is no relation between them, such as a Product has Categories, but rather you want both exclusively.

// GET: api/Data/ProductCategories
public IQueryable<ProductsCategory> GetProductsCategories()
{
    return GetProductsCategories();
}

...

public class ProductsCategory
{
    public IEnumerable<Category> Categories { get; set;}
    public IEnumerable<Product> Products{ get; set;}
}

...
public ProductsCategory GetProductsCategories()
{
    var products = db.Products.ToList();
    var categories = db.Categories.ToList();

    var productCategories = new ProductsCategory()
    {  
        Products = products,
        Categories = categories
    };

    return productCategories;
}

Or something to that degree.

Also don't return IQueryable directly, it's redundant and ill advised unless the caller of that API is going to somehow be executing some Query against what has been returned which is unlikey seeing as its WebAPI and not some method. Instead return a List or IEnumerable.

And if you are looking to improve what you have a little bit as well, be sure to wrap that repository that you have in some sort of service, so you can say something along the lines of:

productsService.GetProductsCategories()

Rather than accessing the context directly in your API.

Extended Reading for Repositories and Service Layers

https://codereview.stackexchange.com/questions/33109/repository-service-design-pattern

Difference between Repository and Service Layer?

Community
  • 1
  • 1
JEV
  • 2,494
  • 4
  • 33
  • 47
  • Is it possible to validate multiple entities? For example if I want to update a batch of entities I need to check if they are valid? – Adrian Dec 10 '15 at 20:33