Josh
Josh

Reputation: 367

Visual Studio project(s) REST api

Using Microsoft's asp.net web api stuff.

What is the best way to structure Visual Studio project(s) for a REST api?

Should I make a new project for each type of object and have them in one solution?
Customer
Address
Bank

Or have them all in the same project with just different classes?

Is there any benefits to either method?

This API is going to at some point encompass every data source I have.

Upvotes: 2

Views: 1630

Answers (4)

Davide Icardi
Davide Icardi

Reputation: 12209

I think that you should have a single Web Project otherwise you will have multiple HTTP endpoints. So I will assume that your question is where to put all the Web API controllers (class derived from ApiController, see MSDN).

Usually we have all the Web API Controllers in the web project so that they are separated from the other application layers. Controllers are very easy, they just forward the call to a repository or another service.

In one project we have also separated the controllers primary because they can be plugged without recompile the project (sort of addon).

If you use some sort of IoC + Dependency Injection library (that I strongly suggest) then the controllers are created by the container so they can be located in any referenced assembly, you should only register all the controllers inside the container.

Here a tutorial on how to use Castle Windsor with ASP.NET API: Dependency Injection in ASP.NET Web API with Castle Windsor by Mark Seemann.

Here another tutorial for Ninject: Adding Ninject to Web API.

Another suggestion is to use Attribute Routing because I think it is more powerful and easy to use. For more information see this article. Basically each controller/action define its own route, for example:

public class OrdersController : ApiController
{
    [Route("customers/{customerId}/orders")]
    [HttpGet]
    public IEnumerable<Order> FindOrdersByCustomer(int customerId) { ... }
}

Finally just pay attention because the controller name (class name) should be unique across all the solution.

Upvotes: 2

Gomes
Gomes

Reputation: 3330

Keep the webapi module(dll) as thin(just routing and formatting result) as possible, so that unit test will be feasible to achieve 80%.

Next, keep the other dependency as separate module(dlls) as and when required according to your business use case.

if you stuff everything in your webapi, then reusable will be trouble. webapi should do what it should do(facade interface).

Upvotes: 1

Yishai Galatzer
Yishai Galatzer

Reputation: 8862

I would keep the all in one project.

By default controllers are what you use to separate resources.

If your app grows and you want to separate the concerns, you can use different assemblies, but honestly you will have to grow very very large and at that point you typically already have separate services.

So in short, build an app use controllers and folders to separate concerns.

Upvotes: 2

Raja Rajendraprasath
Raja Rajendraprasath

Reputation: 222

I would Suggest you to use MVC/MVVM architecture with the following technology

    AngularJS/KnocoutJS
    ASP.Net MVC 4
    WebAPI-2
    Entity Framework 6 

Click here for sample project architechture actually now days single page application is more emerging thing.

Upvotes: 1

Related Questions