的ASP.NET Web API架构建议/意见 [英] ASP.NET Web API Architecture Suggestion/Feedback

查看:107
本文介绍了的ASP.NET Web API架构建议/意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我面对的要求建立一个Web服务,可以让Java客户端检索并通过HTT prequest触发我们的数据的行为。我们是它出现这种情况的最新的解决方案是微软的MVC4的Web API一个.net店。我用你的标准分层架构我从API的拉数据,但是这将是我的第一个Web服务提供的数据。

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 shop it appears the latest solution for this is Microsoft's MVC4 Web API. I am used to your standard tiered architecture I have pulled data from API's but this will be my first web service supplying data.

从我的研究,我所看到的建议如下:

From my studies I have seen suggestions as follows:


  • 逻辑和数据访问分离到一个单独的项目类型类库。

  • 使用您的控件访问数据和执行逻辑。

  • 使用模型来访问数据和执行逻辑。

我找人谁拥有MVC4的Web API的经验谁能够提供一些线索的良好做法以这种方式构建Web服务。

I am looking for someone who has experience with MVC4 Web API who can shed some light on good practices for building a web service in this way.

在此先感谢。

推荐答案

首先,不要把你的ASP.NET Web API逻辑放到一个单独的项目。这会给你的灵活性为申办层(作为的ASP.NET Web API承载无关的),它只是使整个工程的清洁。比方说,你的项目的名称为MyProject的。可以命名API项目为 MyProject.API 键,您可以安装 Microsoft.AspNet.WebApi.Core 的NuGet包到这个项目中。

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.

我也建议你到您的域层以及(POCO实体,资源库,你的服务层件等)分开。让我们把这个 MyProject.Domain 。然后,您将引用这个 MyProject.Domain MyProject.API 工程项目。

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.

我不建议您将所有的POCO实体转储到您的API。所以,我肯定会与数据传输对象(DTO)工作。您可以使用第三方工具,如autoMapper你的实体类映射到您的DTO。但是,不要把你的DTO,请求命令,请求模型成一个单独的项目。你会引用 MyProject.API.Model MyProject.API 工程项目。我们为什么要为此创造一个单独的项目?因为,以后如果你决定建立你的HTTP API .NET客户端的包装,你可以很容易地引用该项目使用那些与你的.NET客户端也是如此。让我们把这个项目 MyProject.API.Model

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.

最后,我们需要为我们的API托管层。 Asumming,你想举办ASP.NET根据这个项目,你可以通过创建空的Web应用程序模板一个新的项目,让我们把这个项目 MyProject.API.WebHost 。然后,您可以安装 Microsoft.AspNet.WebApi 封装到这个项目中。从这个项目中,您将引用 MyProject.API MyProject.API.Model MyProject的。域项目。这个项目是你要部署到服务器的人。

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.

如果你想创建一个.NET包装你的HTTP API,你可以创建一个名为另一个项目 MyProject.API.Client 安装 Microsoft.AspNet.WebApi.Client 封装成这一个。您也将从这个引用 MyProject.API.Model 的项目,这样就可以将反序列化和强类型的对象序列化。

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:

希望这给你一点想法的。

Hope this gives you a bit of an idea.

这篇关于的ASP.NET Web API架构建议/意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆