3

We've got a solution with multiple MVC web projects and now adding a client-facing WebApi project.

The API will be a much more scaled-down version of what is available through any of the web projects (though it will likely expand much more over time), so we've come to a decision-making point for how to handle the Models.

What are the best practices for handling Models across different projects?

It's my understanding that a Model in a WebApi project will, for example, use certain property attributes which are meaningless to an MVC web application. And also as an example, the Display attribute is meaningless to the WebApi, but very useful in a View.

This leads me to believe I should be creating a separate set of Models for the WebApi, but also wondering if I'm missing something.

I understand that this could be a question that may lead to a range of opinions, so I'm mostly looking for what are considered industry best practices.

4
  • Web API is usually used for web services. It is definitely a good practice to keep your service and your actual application separate, so you should use different projects for that. If you want to ensure they're being developed independently you should also split the solutions. Commented Dec 19, 2013 at 19:07
  • We have already separated the API into it's own project - my question is whether or not we should share a single Model among the various projects. Creating a single, shared Model library versus separate ones per project. Commented Dec 19, 2013 at 19:10
  • 1
    I would use several model projects, each for it's own purpose. That way you have cleaner separation between service and application (and perhaps database). But you could run into more code by creating mapping methods, to map one model to another, if necessary. Commented Dec 19, 2013 at 19:11
  • @McCee You can use the same domain models for various client apps(web api/ mvc web app). Expose it through a repositary pattern to the clients. Commented Dec 19, 2013 at 19:12

2 Answers 2

6

In my solution where i have Web API and MVC web app, I have the below structure

Models : My Entity/Business objects. These are created by Entity framework from my database. These are almost same as my db structure. My Repositary methods (for data access) returns either a single instance /collection of instances of this classes. My data access project is a separate class library which has been referred in other places like my web api project etc..

Web API ViewModels : The Viewmodels (POCO class) specific to the Web API interfaces/action methods.

MVC Web app ViewModels : The Viewmodels (POCO class) specific to my razor views. I had even inherited few of these from the Web API Viewmodels and added additional properties as needed.

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

4 Comments

The web api objects are usually not called ViewModels (since they're not tied to a view), but rather just models or DTO (domain transfer objects).
Viewmodel term is used as a generic term explaining that this object is specific to serve this particular calle (a razor view or an api call). But there could be a better term for it.
With the solution you describe, does that mean that you have a separate MappingExtensions class per project? (we're using AutoMapper)
@McCee : Yes because sometimes your web app viewmodel is different than what you want to return from your Web API.
1

I use a separate project for the DTOs, so that the projects that need to consume them do not get into issues with circular references.

I would have the WebApi project map your models into the DTOs. If your MVC project is consuming the WebAPI output, it just needs a reference to the DTO project. This keeps you from having to refer to the WebAPI project directly.

1 Comment

I agree that keeping DTOs in a separate assembly will be best in this scenario, also is how John Papa does it on the SPA course at pluralsight. Also MVC can inherit from DTOs for ModelViews purposes or make ViewModel Manipulation ex: ViewData.Model = new SomeViewModel(SomeDTO) and also keeps the API and MVC DTOs in sync.

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.