2

I work on a MVC 5 web application. 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.

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.

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? Should we do this?

3
  • if it's needed by both projects (mvc and api) then move it into another separate project to be consumed by both. Commented Dec 18, 2015 at 9:35
  • I think you can use the Web API project as your service layer and Don't expose unnecessary methods to the web... It'll do the job for you while not having two service layers for a single solution.... Commented Dec 18, 2015 at 9:40
  • If you duplicate code you're not respecting the DRY principle. Commented Sep 19, 2022 at 16:11

1 Answer 1

4

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? 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)

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.