What is the right way for ASP.NET MVC 3 solution structure, data binding and Automapper integration?

I created an ASP.NET MVC 3 solution. It consists of three separate projects. I am going to use Automapper and Unity. Each view has its own ViewModel.

Projects:

  • DATA (the first project contains the first classes of models and storage)
  • BUSINESS (service classes are in this project). DATA project added as a reference.)
  • Project WEB (WEB UI). This is an MVC project. The BUSINESS project has been added as a link.)

Now I have 2 questions.

  • Is this project structure a good practice?
  • I am trying to use Automapper with my utility calls in controllers. However, if I use Automapper in the controller, I must add the DATA project as a reference in my web project in order to do the conversion from POCOs to ViewModels. Should the WEB project only interact with the BUSINESS project?

Need help.

Thanks.

+4
source share
3 answers

Question 1 This is subjective, and this is not the best place to ask. However, for what it's worth, the projects that I support usually have a Core, Web, and Services project in them.

Question 2 No, based on your example and my experience, your web project is suitable for links to your DATA and BUSINESS project. For us, our web projects are aware of our Core and Services projects. However, in our service there is no link to our web project. For this reason, our Automapper configuration knows how to map service model models, view models, and entity models to and from them.

Does it help? Let me know in the comments if you want me to clarify. A bit down on this blog you can find an image depicting a similar project setting for what we use.

+1
source

What you describe is the usual approach, and there is nothing wrong with that. Of course, there may be approaches that can be considered an alternative. But it depends on your actual needs and also depends on personal preferences.

Your POCOs are mainly your business objects. And even if they are in a DATA project, they are part of your domain. Often it’s better to just put the DATA link in the UI project until you need a more complex solution.

0
source

I know that you have already accepted shanabus answer, but I would like to add something to the discussion, which may be useful on the second question as well.

MVC really is an architectural model of the presentation layer, which can lead to another way of structuring your project: the presentation layer (your WEB project) should speak only from the point of view of View Model objects, because it is really important for it. Therefore, your business project (or service level) should be responsible for the transfer from model to ViewModel and vice versa, using, for example, AutoMapper, a link to the model level and repository (in this case, the Data project). Given that the Presentation layer is no longer responsible for translating from Model to ViewModel and vice versa, you can remove the link to the Data project and solve your problem.

You can find a much better and more detailed explanation at the following link: http://msdn.microsoft.com/en-us/magazine/hh456393.aspx

Hope this helps

0
source

Source: https://habr.com/ru/post/1395842/


All Articles