ASP.NET Web API Architecture Suggestion/Feedback

后端 未结 1 1065
逝去的感伤
逝去的感伤 2020-12-24 09:59

Currently I am faced with the requirement building an web service that will let a java client retrieve and trigger actions on our data through httprequest. We are a .Net sho

1条回答
  •  失恋的感觉
    2020-12-24 10:47

    First of all, do put your ASP.NET Web API logic into a separate project. This will give you flexibility over hosting layer (as ASP.NET Web API is hosting agnostic) and it just makes the whole project clean. Let's say your project's name is MyProject. You can name the API project as MyProject.API and you can install the Microsoft.AspNet.WebApi.Core NuGet package into this project.

    I also suggest you to separate your domain layer as well (POCO entities, repositories, your service layer pieces, etc.). Let's call this MyProject.Domain. Then, you would reference this MyProject.Domain project from the MyProject.APIproject.

    I wouldn't recommend you to dump all your POCO entities into your API. So, I would definately work with Data Transfer Objects (Dto). You can use a 3rd party tool like autoMapper to map your entity classes to your Dtos. However, do put your Dtos, Request Commands, Request Models into a separate project. You would reference MyProject.API.Model project from MyProject.API project. Why do we create a separate project for this? Because, later if you decide to build a .NET client wrapper for your HTTP API, you can easily reference this project to use those with your .NET client as well. Let's call this project MyProject.API.Model.

    Lastly, we need a hosting layer for our API. Asumming that you would like to host this project under ASP.NET, you can create a new project through the Empty Web Application template and let's call this project MyProject.API.WebHost. Then, you can install the Microsoft.AspNet.WebApi package into this project. From this project, you would reference MyProject.API, MyProject.API.Model and MyProject.Domain projects. This project is the one that you should deploy to your server.

    If you would like to create a .NET wrapper for your HTTP API, you can create another project called MyProject.API.Client and install Microsoft.AspNet.WebApi.Client package into this one. You would also reference the MyProject.API.Model project from this one so that you can deserialize into and serialize from strongly-typed objects.

    Here is the screenshot of the solution explorer for the project I have been working with:

    enter image description here

    Hope this gives you a bit of an idea.

    0 讨论(0)
提交回复
热议问题