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?


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.

  • In my DAL library would it make sense to keep the ef POCO's, ViewModels and Repositories? As long as the Web, and API projects can both use the DAL and have access to ViewModels I think it should be ok – raklos Mar 15 '16 at 22:58
  • How big it gonna be finally? – andrew.fox Mar 16 '16 at 6:53
  • not massive, just my own project at the moment – raklos Mar 16 '16 at 9:02
  • so you should be ok with option a), id est, using domain entities in web application with ad hoc viewModels where necessary. – andrew.fox Mar 16 '16 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.