Good practice with . NET MVC

Asked

Viewed 795 times

5

I have an ASP.NET MVC application and would like to know what good practices microsoft indicates in the organizational aspect of the solution, using the latest technologies such as ASP.NET Identity.

Assuming the following scenario:

  • Controller for user (Usuariocontroller);
  • 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 place as the user entity? And the images of a specific View, should be grouped in the Content folder?

  • 1

    You are considering using some architecture that Microsoft has already defined, such as ASP.NET Membership or ASP.NET Identity?

  • I am considering a general environment where it can be used as a basis for projects that use or not Membership or Identity.

  • 1

    @Vinícius, Viewmodel is for you to make a better bind and typing in your Views, besides not leaving ugly with all those Annotations, "images" ? well, today in the current scenario, neither stores images in folders, if your app is cloud, but if it is not, you put in the structure you want, there is no "rule" for it

  • 1

    That doesn’t work. The answer would be huge.

  • @Rod, questioning would be more about where to implement this code. As I currently do, my entity and the viewmodels of my controller (Usuariocontroller) context are inside a file called Usuariomodel inside the Models folder. However, I do not know if this is the most appropriate way to implement.

  • @Ciganomorrisonmendez, isn’t there a more suitable mode or convention for small applications using only ASP.NET MVC? No use of business libraries using more complex architectures like N-Layers or Onion...

  • If the application is small, the separation of the MVC itself is enough, that is, depends on what you are quoting from "small"

  • @Vinicius From the MVC5 the indicated is to use the ASP.NET Identity. I need you to delimit that answer so I can answer something objectively.

  • @Vinicius For Models, Controllers and Views, follow the pattern of MS itself. Create a template and see, is what MS recommends. The rest you follow from one folder to each. And the most important: ONE FILE for each!! No Entity and Viewmodel in the same file!!

  • @Ciganomorrisonmendez, I edited the question for ASP.NET Identity, since as you mentioned, it is the most suitable to use.

  • @Andrefigueiredo, the template for an ASP.NET MVC 4 project has viewmodels within the same file. You could answer because it is not indicated to use?

  • 2

    @Vinicius is not indicated by the organization and navigation within the sources. Not separating tends to turn mess after a while. The right thing would be for Microsoft to have separated.

Show 7 more comments

1 answer

8


First, I am taking over the organization for ASP.NET Identity, so that the answer does not become a text wall.

Assuming the following scenario:

  • Controller for user (Usuariocontroller);
  • 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 the User: Controllers/AccountController;
  • Viewmodels for the User Actions: Models/AccountViewModel;
  • Class representing the entity User: ApplicationUser, in Models/IdentityModels. This does not mean that additional entities should remain at the source IdentityModels. Best to leave one font per class;
  • Class representing the User repository: If you are using Entity Framework, the Entity Framework is the repository not only of User, but of all entities of the system. In an ASP.NET MVC5 application, it is within ApplicationDbContext (Models/IdentityModels), derivative of IdentityDbContext, containing a IDbSet<TUser> Users. This Users is the repository;
  • Some Views user has images: You can save user images in database, in application directory Content/Images or else use the Engrave.
  • Some user views have specific scripts and CSS’s: You would have to mount @sections dynamically. Normally the project comes configured with two: scripts and views. See your file _Layout.cshtml to see where these sections will be written. It is not worth putting this here. This part deserves a separate question.

Where should I write the code of all Viewmodels? These must be in the same location as the user entity?

In a directory ViewModels, separate 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 this. Just be sure to set a file index.html for each level, to avoid issues with publishing (if directories are empty, Web Deploy does not create directories in the publication destination).

  • Other entities of the system must also remain in Models/Identitymodels?

  • 2

    No way. Separated by file. I will edit the answer as soon as the internet connection leaves.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.