Good practices with .NET MVC

5

I have an ASP.NET MVC application and would like to know what are the best practices that microsoft indicates in the organizational question of the solution, using the latest technologies like ASP.NET Identity.

Assuming the following scenario:

  
  • Controller for user (UserController);
  •   
  • ViewModel for user actions;
  •   
  • Class representing the user entity;
  •   
  • Class representing the user repository;
  •   
  • Some User Views have images;
  •   
  • Some User Views have specific scripts and css's.
  •   

How can I organize the above scenario? That is, where should I write the code for all ViewModels? Should these be in the same location as the user entity? And the images from a specific View should be grouped in the Content folder?

    
asked by anonymous 21.11.2014 / 20:46

1 answer

8

First, I'm assuming the organization for ASP.NET Identity, so the answer does not turn up a textural wall.

Assuming the following scenario:

  
  • Controller for user (UserController);
  •   
  • ViewModel for user actions;
  •   
  • Class representing the user entity;
  •   
  • Class representing the user repository;
  •   
  • Some User Views have images;
  •   
  • Some User Views have specific scripts and css's.
  •   

How can I organize the above scenario?

In ASP.NET Identity this is already organized for you as follows:

  • Controller for User: Controllers/AccountController ;
  • ViewModels for User Actions: Models/AccountViewModel ;
  • Class representing the User entity: ApplicationUser , in Models/IdentityModels . This does not mean that additional entities should be in the IdentityModels source. It is best to leave one font per class;
  • Class representing the User repository: If you are using the Entity Framework, the Entity Framework is the repository not only of User, but of all system entities . In an ASP.NET MVC5 application, it is within ApplicationDbContext ( Models/IdentityModels ), derived from IdentityDbContext , which contains IDbSet<TUser> Users . This Users is the repository;
  • Some user views have images: You can save the user's images to the database in the Content/Images directory or use Gravatar .
  • Some User Views have specific scripts and CSS: You would have to mount @section s dynamically. Usually the project is configured with two: scripts and views . See your _Layout.cshtml file to see where those sections will be written. It's not worth putting this here. This part deserves a separate question.

Where should I write the code for all ViewModels? Should these be in the same location as the user entity?

In a directory ViewModels , separated from other directories ( Models , Controller , Views , ...).

And the images of a specific View should be grouped in the Content folder?

Yes. I usually use Content\Images\MinhaView\ . You can create as many subdirectories as you want from there. Just do not forget to set a index.html file for each level, to avoid problems with the publication (if the directories are empty, Web Deploy does not create the directories at the destination of the publication).

    
21.11.2014 / 21:37