Project Structure for Web, API, DAL, and ViewModels in ASP.NET MVC

asp.net-mvcobject-oriented-designweb-api

I have a ASP .NET WebApi and a Web Project. I'm about to create a DAL class library… Which will be my codefirst/POCO's or possibly edmx files.

I'm wondering do I need to have another class library for DTOs of ViewModels?…

I'll need to map db entities to some sort of ViewModel that will probably be needed by both the web and Api projects.

Does this sound right?

Best Answer

Surprisingly, it depends.

If we're talking about a small project with a small number of model entities, then there's no sense in introducing another dummy level of models.

But in bigger solutions, especially when DI (dependency inversion) will be introduced between DAL and BL, it's helpful to separate (or even necessary) to create a DTO objects. This separation will allow you to break dependency between Web projects and DAL layer and increase testability.

Some DTOs will be very similar to db entities, so using an Automapper may be handy.

Related Topic