HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

Properly create and customise a web app using visual studio 2015 C# MVC template

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
customise2015templatemvccreateappproperlyvisualusingweb

Problem

I'm new to C# web, I want to know how to properly create a web app using
visual studio 2015 MVC templates, and being able to customise some of its code. I want to both practice C# web and clean code...
So I have 2 projects, one for Domain and another which is the Web App.

For example: In my Domain I have a class named User, with the following code (It is aimed at users who speak Spanish so error messages are in Spanish):

```
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Domain
{
public class User
{
public int Id { get; set; }

[DisplayName("Nombre de Usuario")]
[Required(ErrorMessage = "El Nombre de Usuario es obligatorio.")]
[StringLength(30)]
[Index(IsUnique = true)]
public string UserName { get; set; }

[DisplayName("Correo Electrónico")]
[Required(ErrorMessage = "El Correo Electrónico es obligatorio.")]
[StringLength(45)]
[EmailAddress]
[Index(IsUnique = true)]
public string Email { get; set; }

[DisplayName("Contraseña")]
[Required(ErrorMessage = "La Contraseña es obligatoria.")]
[StringLength(100, ErrorMessage = "El número de caracteres de {0} debe ser al menos {2}.", MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password { get; set; }

[Display(Name = "Confirmar Contraseña")]
[DataType(DataType.Password)]
[NotMapped]
[Compare("Password", ErrorMessage = "La contraseña y la contraseña de confirmación no coinciden.")]
public string ConfirmPassword { get; set; }

public bool Locked { get; set; } = false;

public bool ConfirmedAccount { get; set; } = false;

[DisplayName("Perfil")]
[Required(ErrorMessage = "El Perfil es obligatorio.")]
public Profile Profile { get; set; }

private User() { }

public User(string userNam

Solution

The short answer to what you are trying to do is; let the Identity classes handle Authentication and User management. You can put your User into a Model class of it's own. Then reference that from the ApplicationUser class that comes with MVC.

Another approach would be to extend the ApplicationUser class itself by adding the additional properties for your profile.

As for Business Logic and N-Tier architecture; put all that aside for the time being. The MVC architecture Semantic approach and Entity Framework kind of help to handle that stuff organically.

I would suggest going through the free training at the Microsoft Video Academy website. That should clear things up for you.
There's also a class specifically for Entity Framework.

As for the class you submitted. It looks fine. You'll probably want to revisit all the annotations learning more about Entity Framework, depending on your use case..

MVC

Identity Resources

Context

StackExchange Code Review Q#138380, answer score: 4

Revisions (0)

No revisions yet.