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
@section
s 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).
You are considering using some architecture that Microsoft has already defined, such as ASP.NET Membership or ASP.NET Identity?
– Leonel Sanches da Silva
I am considering a general environment where it can be used as a basis for projects that use or not Membership or Identity.
– Vinícius
@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
– Rod
That doesn’t work. The answer would be huge.
– Leonel Sanches da Silva
@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.
– Vinícius
@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...
– Vinícius
If the application is small, the separation of the MVC itself is enough, that is, depends on what you are quoting from "small"
– Rod
@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.
– Leonel Sanches da Silva
@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!!
– Andre Figueiredo
@Ciganomorrisonmendez, I edited the question for ASP.NET Identity, since as you mentioned, it is the most suitable to use.
– Vinícius
@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?
– Vinícius
@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.
– Leonel Sanches da Silva