简体   繁体   English

Asp.Net Mvc服务层

[英]Asp.Net Mvc service layer

I work on a MVC 5 web application. 我在MVC 5 Web应用程序上工作。 We have two projects, in one project there is a domain model, and everything else is in the other (service layer, controllers, views, etc). 我们有两个项目,在一个项目中有一个领域模型,而其他所有东西都在另一个中(服务层,控制器,视图等)。

That worked fine until we made a new WebApi project to expose some functionalities. 在我们创建一个新的WebApi项目以暴露一些功能之前,这种方法一直很好。

Now when we need some services from main project in our WebApi we need to copy paste service from main project to new web api project so we have duplicated code. 现在,当我们需要WebApi中的主项目中的一些服务时,我们需要将粘贴服务从主项目中复制到新的Web api项目中,以便我们复制了代码。

What I want to know is, is it a good practice to move whole service layer to the new project(dll) and then include it in main application and web api? 我想知道的是,将整个服务层移至新的project(dll),然后将其包含在主应用程序和Web API中是一种好习惯吗? Should we do this? 我们应该这样做吗?

Avoid copying/past code, it will not help you in the future if you need maintain this project. 避免复制/粘贴代码,如果将来需要维护此项目,它将对您没有帮助。

What I want to know is, is it a good practice to move whole service layer to the new project(dll) and then include it in main application and web api? 我想知道的是,将整个服务层移至新的project(dll),然后将其包含在主应用程序和Web API中是一种好习惯吗? Should we do this? 我们应该这样做吗?

Yes, it is a good pratice and you could create a new assembly (a class library project) in your solution and reference and use it on the web and web api, or more projects you need it (windows, wpf, windows service ... for sample) 是的,这是一个很好的实践,您可以在解决方案中创建一个新程序集(一个类库项目)并进行引用,并在Web和Web api上使用它,或者您需要更多项目(Windows,WPF,Windows Service .. (用于样本)

Look the image bellow, you could do something like this, separating you Domain Model, the Data Access Layer, Busines Layes (probably your service layer) and UI: 看下面的图像,您可以执行以下操作,将域模型,数据访问层,业务层(可能是服务层)和UI分开:

在此处输入图片说明

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

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