简体   繁体   English

消耗Web API和类信息

[英]Consuming Web API and class information

I currently have the following projects in a Visual Studio Solution 我目前在Visual Studio解决方案中有以下项目

  • MySiteAPI - Web API MySiteAPI-Web API
  • MySiteMVC - MVC 5 template MySiteMVC-MVC 5模板

MySiteAPI is correctly serving data on http:\\\\localhost\\api and I want to consume that in MySiteMVC . MySiteAPIhttp:\\\\localhost\\api上正确提供数据,我想在MySiteMVC中使用它。

MySiteAPI currently has Entity Framework Code First classes to handle the data side MySiteAPI当前具有Entity Framework Code First类来处理数据端

I'm confused on how to pull the data into the class definitions on MySiteMVC since the definitions are in MySiteAPI project and not in MySiteMVC . 我对如何将数据提取到MySiteMVC的类定义中感到困惑,因为这些定义在MySiteAPI项目中,而不在MySiteMVC中 It also appears that Web API can't be called as a Web Reference to get class information, which is where I'm stuck. 看来,Web API不能作为获取类信息的Web引用被调用,这就是我遇到的问题。

I believe this is an architectural problem and want to make sure I understand the proper way to do this before I restructure this project. 我认为这是一个体系结构问题,并希望在重组该项目之前确保我了解执行此操作的正确方法。

I don't want to reference MySiteAPI into MySiteMVC because that sort of defeats separating the two projects. 我不想将MySiteAPI引用到MySiteMVC中,因为这样会使两个项目分开。 I'm trying to be more robust about this. 我正在尝试对此进行增强。

Instead of placing the class definitions inside of MySiteAPI project, should a class library be created that handles the entity framework for the data which both projects reference? 不是将类定义放在MySiteAPI项目中,而是应该创建一个类库来处理两个项目引用的数据的实体框架?

If so, would the class library contain only the class definitions and MySiteAPI project would be the project that does the database operations via a DbContext ? 如果是这样,则类库将仅包含类定义,而MySiteAPI项目将是通过DbContext执行数据库操作的DbContext吗? This seems to be what makes most sense to me. 这似乎对我来说最有意义。

In this case there would be three projects 在这种情况下,将有三个项目

  • MySiteModels - Holds class definitions MySiteModels-包含类定义
  • MySiteAPI MySiteAPI
  • MySiteMVC MySiteMVC

Both MySiteAPI and MySiteMVC would reference MySiteModels MySiteAPIMySiteMVC都将引用MySiteModels

Is this a correct way to do this? 这是正确的方法吗? Is there a more appropriate way? 有没有更合适的方法?

Yes, your problem seems to be architectural. 是的,您的问题似乎与体系结构有关。 There are many different approaches to achieve better architecture for your problem (it also depends on the size of your project) but I will describe the one I think is the most suitable, flexible, testable for medium projects. 有多种方法可以针对您的问题实现更好的体系结构(这也取决于项目的规模),但是我将描述一种我认为最适合,中等,灵活,可测试的方法。

The models are defined in separated class library project for example MyProject.Models . 这些模型是在单独的类库项目中定义的,例如MyProject.Models In this project there are only POCO which doesn't know anything about WebApi, Mvc or entity framework projects. 在这个项目中,只有POCO对WebApi,Mvc或实体框架项目一无所知。 They should be completely decoupled and possible to use from any other projects. 它们应该完全分离,并可以与其他任何项目一起使用。

The 'entity framework project' or 'data' is defined also in separated class library project for example MyProject.Data . “实体框架项目”或“数据”也在单独的类库项目中定义,例如MyProject.Data This project references only entity framework and the Models project. 该项目仅引用实体框架和Models项目。 Here is defined the derived DbContext class. 这里定义了派生的DbContext类。 The project is responsible for connection and operating over the database. 该项目负责数据库的连接和操作。 If you use some patterns like Respoistory or Unit Of Work patterns (which I think is much better than initializing DbContext everywhere where you need it) they can be placed in this project too. 如果您使用某些模式(例如RespoistoryUnit Of Work Respoistory Unit Of Work模式)(我认为比在需要的地方初始化DbContext更好),它们也可以放在此项目中。

The WebApi and Mvc are two separated 'client' projects. WebApiMvc是两个单独的“客户端”项目。 Both reference Models project, the WebApi references the Data project and Entity Framework too. 两者都引用了Models项目, WebApi引用了Data项目和Entity Framework。 But both client projects don't know anything each other. 但是,两个客户项目彼此都不了解。 You can give them their thin (dll) dependencies and host them on different servers if you want. 您可以给它们提供瘦(dll)依赖关系,并根据需要将它们托管在不同的服务器上。 If the MVC project will have individual users authentication using the identity it should reference other similar structure of projects (data, models, entity framework, separated DbContext) or use the already defined one. 如果MVC项目将使用身份进行单个用户身份验证,则它应引用项目的其他类似结构(数据,模型,实体框架,分隔的DbContext)或使用已定义的项目。

If you plan to test your code (I recommend it) there will be one (or more) test projects called for example MyProject.Tests . 如果您打算测试代码(我建议这样做),将有一个(或多个)测试项目,例如MyProject.Tests The tests projects reference Models , Data (or fake implementation of Data ) and WebApi or Mvc project depends on what is testing. 试验项目参考ModelsData (或假执行Data )和WebApiMvc项目依赖的是什么测试。

For DbContext, Repository, Unit of Work and some other classes there should be few interfaces which can be placed in different class library for common interfaces. 对于DbContext,存储库,工作单元和其他一些类,应该没有几个接口可以放置在不同的类库中以用于公共接口。

Let's summarize: 让我们总结一下:

  1. MyProject.Models - The POCO data models. MyProject.Models-POCO数据模型。 No other references. 没有其他参考。
  2. MyProject.Data - The DbContext, Repository, Unit of Work. MyProject.Data-DbContext,存储库,工作单元。 Reference to Models and Entity Framework. 参考模型和实体框架。
  3. MyProject.WebApi - The WebApi project. MyProject.WebApi-WebApi项目。 References - Models, Data, Entity Framework. 参考-模型,数据,实体框架。
  4. MyProject.MVC - The MVC project. MyProject.MVC-MVC项目。 References - Models (optional Data and Entity Framework). 参考-模型(可选的数据和实体框架)。
  5. MyProject.Test - The test project. MyProject.Test-测试项目。 References - Models, Data (or Fake Data), the projects fro testing. 参考-模型,数据(或假数据),测试项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM