简体   繁体   English

ASP.NET MVC 和 Web API 共享类库项目

[英]ASP.NET MVC and Web API sharing Class Library Project

I am going to build the following apps in .NET and I have a few questions regarding the correct architecture:我将在 .NET 中构建以下应用程序,我对正确的架构有几个问题:

  1. Console Application: read local files and update data to SQL Database in Azure控制台应用程序:读取本地文件并将数据更新到 Azure 中的 SQL 数据库
  2. ASP.NET MVC: dashboard site to interact with uploaded data to SQL Database ASP.NET MVC:仪表板站点与上传到 SQL 数据库的数据进行交互
  3. Web API: handle business logic and data access. Web API:处理业务逻辑和数据访问。

I first thought of using Web API as the core to serving data and business logic.我首先想到使用 Web API 作为提供数据和业务逻辑的核心。 However, a Class Library would be needed accross the projects so that all of them "speak the same language" in terms of business objects.但是,跨项目需要一个类库,以便所有项目在业务对象方面“使用相同的语言”。 Example: I have a SMS object in the Console Application that will submit data to the Web API.示例:我在控制台应用程序中有一个 SMS 对象,它将向 Web API 提交数据。 This Web API should also have the same SMS object.此 Web API 也应具有相同的 SMS 对象。 Next, the ASP.NET will display SMS data, hence, should access the same object as well.接下来,ASP.NET 将显示 SMS 数据,因此也应该访问相同的对象。 But since this is an MVC app, it will have its own data object.但由于这是一个 MVC 应用程序,它将拥有自己的数据对象。

What would be the right approach to this?什么是正确的方法? How should be the Solution tree in Visual Studio? Visual Studio 中的解决方案树应该如何?

Thanks for your help.谢谢你的帮助。

It doesn't mean that because it's an MVC application it needs to have its own data object.What I've done in the past was to create a class library and add references to the MVC related DLLs to this project.Then I would decorate the objects with all the required MVC attributes and finally I would add a reference to this class library to all the projects in my application that require them like Business Layer,Web api layer and the MVC(UI) layer.这并不意味着因为它是一个MVC应用程序它需要有自己的数据对象。我过去所做的是创建一个类库并将对MVC相关DLL的引用添加到这个项目中。然后我会装饰具有所有必需 MVC 属性的对象,最后我将向我的应用程序中需要它们的所有项目添加对此类库的引用,如业务层、Web api 层和 MVC(UI) 层。

In your scenario it could be something like this:在您的场景中,它可能是这样的:

通用对象库

Don't use MVC, make a WebApi service, and choose a Web UI framework like react or angular.不要使用MVC,做一个WebApi服务,选择react或angular之类的Web UI框架。 MVC is not worth using anymore. MVC 不再值得使用。

That way, everyone connects to the WebAPI, your console app, your website/dashboard, and even a future mobile application.这样,每个人都可以连接到 WebAPI、您的控制台应用程序、您的网站/仪表板,甚至是未来的移动应用程序。

IF you ever change the database from SQL server to, let's say, txt files (xD) then your console app will have to change also.如果您曾经将数据库从 SQL 服务器更改为 txt 文件 (xD),那么您的控制台应用程序也必须更改。 Using only a single API to interact with your database will effectively separate all your layers.仅使用一个 API 与您的数据库交互将有效地分离您的所有层。

However, a Class Library would be needed across the projects so that all of them但是,跨项目需要一个类库,以便所有项目

Yes, but you can achieve this with a shared library or even a shared project.是的,但您可以使用共享库甚至共享项目来实现这一点。 It's just a bunch of classes...这只是一堆课程......

All your logic should be in the web API.您的所有逻辑都应该在 Web API 中。 Including the console app part.包括控制台应用程序部分。 It should only do its data reads and then connect to the API.它应该只读取数据,然后连接到 API。

So in this case, you won't need to have the same model in all projects since the model for web API business model might be more complicated than the model to interact with it.因此,在这种情况下,您不需要在所有项目中都使用相同的模型,因为 Web API 业务模型的模型可能比与之交互的模型更复杂。

Also, serving the dashboard files to a client (browser) will be really easy, since just need to dispatch HTML, js, and CSS files.此外,将仪表板文件提供给客户端(浏览器)将非常容易,因为只需要分派 HTML、js 和 CSS 文件。 Any server will do and also it won't be tied to .Net or any serverside code you like.任何服务器都可以,而且它不会绑定到 .Net 或您喜欢的任何服务器端代码。

在此处输入图片说明

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

相关问题 在ASP.NET MVC 5,ASP.NET Web Api 2和Mobile(Xamarin)之间共享代码 - Sharing code between ASP.NET MVC 5, ASP.NET Web Api 2 and Mobile (Xamarin) 在 ASP.NET next MVC 项目中使用类库 - Using a class library in a ASP.NET next MVC project 类库参考在Asp.net 5 MVC 6 Web应用程序中不起作用 - Class library reference is not working in Asp.net 5 mvc 6 web application 在 ASP.NET (MVC5) web api 项目中授权用户 - Authorising users in an ASP.NET (MVC5) web api project 同一项目中的ASP.NET MVC和Web API - ASP.NET MVC and Web API in same project 在 ASP.NET MVC (5) 项目中使用 Web API 有什么好处? - What is the advantage of using Web API in a ASP.NET MVC (5) project? 在现有的 Asp.Net MVC 项目中包含 Web API - Include Web API in existing Asp.Net MVC project 如何在同一项目中的ASP.NET MVC控制器中从ASP.NET Web API获取数据 - How get data from asp.net web api in asp.net mvc controller in the same project ASP.Net Web API - 从 ASP.NET MVC 项目生成不记名令牌 - ASP.Net Web API - Generate Bearer Token from ASP.NET MVC Project 如何在 ASP.Net MVC4 Web API 项目中使用 Microsoft OCR 库 (Microsoft.Windows.Ocr)? - How to use Microsoft OCR Library ( Microsoft.Windows.Ocr ) in an ASP.Net MVC4 Web API Project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM