简体   繁体   中英

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:

  1. Console Application: read local files and update data to SQL Database in Azure
  2. ASP.NET MVC: dashboard site to interact with uploaded data to SQL Database
  3. Web API: handle business logic and data access.

I first thought of using Web API as the core to serving data and business logic. 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. This Web API should also have the same SMS object. Next, the ASP.NET will display SMS data, hence, should access the same object as well. But since this is an MVC app, it will have its own data object.

What would be the right approach to this? How should be the Solution tree in 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.

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 is not worth using anymore.

That way, everyone connects to the WebAPI, your console app, your website/dashboard, and even a future mobile application.

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. Using only a single API to interact with your database will effectively separate all your layers.

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. Including the console app part. It should only do its data reads and then connect to the 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.

Also, serving the dashboard files to a client (browser) will be really easy, since just need to dispatch HTML, js, and CSS files. Any server will do and also it won't be tied to .Net or any serverside code you like.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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