6
\$\begingroup\$

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 userName, string email, string password, string confirmPassword, Profile profile)
        {
            UserName = userName;
            Email = email;
            Password = password;
            ConfirmPassword = confirmPassword;
            Profile = profile;
        }
    }
}

Before continuing, any suggestions about this code will be very welcome!

I want to use ResetPasswordViewModel, ForgotPasswordViewModel, ExternalLoginConfirmationViewModel, and many other functionalities that come with MVC templates (you can see them inside AccountViewModels.cs) without programming them myself (that is the idea of using a template isn't it?), but I also want to have my own User class...

Reading the template code, I've found that ApplicationUser class inside IdentityModels.cs would have the logic for User the template applies, It also has a commentary telling me that if I want any other User properties added, they should be added here. But I don't think it would be a good idea to have Business bound code inside the web app, what if (hypothetically speaking) tomorrow I want to make the same application on another platform? (and by the way, why does the template encourage to have the business code inside the web app in the first place??)

I hope you get the idea by now, how to use my own User code to work properly with the MVC template? For example to work along with the class AccountViewModels.cs.

I know those are a lots of questions (I've got plenty more)... As I said, I'm new to this and want to get the hand of it, any suggestions on how to improve my code is welcome.

Regards

\$\endgroup\$
2
  • 3
    \$\begingroup\$ New requirement: the website is going to need to be deployed in Germany, Italy, and France. Do static localized attribute values sound like fun now? Localization is never a concern... until it becomes one. Code that's written with localization in mind is much easier to localize when the time comes. Just saying ;-) \$\endgroup\$ Aug 10, 2016 at 20:59
  • \$\begingroup\$ @Mat'sMug hi, thanks for answering, I get you are speaking of the ErrorMesagge Language? \$\endgroup\$ Aug 11, 2016 at 1:17

1 Answer 1

4
\$\begingroup\$

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

\$\endgroup\$
5
  • \$\begingroup\$ Thanks for answering, I'm starting with those links! By the way, I will also try to apply your first suggestion, referencing my class inside ApplicationUser. The main reason for all this is because I believe it's a good idea and practice to have my code separated by Projects (eventually namespaces), through their similar behaviour, wouldn't you agree? As I said, wouldn't want to have User logic in Web App Project, and (for example), Company, Office and any other Domain Classes inside another Project (which I happened to call Domain, thanks to my advanced originality). Upvoted your answer! \$\endgroup\$ Aug 11, 2016 at 20:35
  • \$\begingroup\$ I would go through the MVA training before making those architectural decisions. In this case, I think it would be wise to follow the guidance and conventions, even though it may not feel right at first. Your life will be much easier. Instead of using separate projects as you describe, it would be better to use Namespaces to create the logical application tiers. \$\endgroup\$ Aug 12, 2016 at 15:59
  • \$\begingroup\$ I've already seen first 2 videos and i'm getting a better idea of all these, at least some reassurance of what good practices are, just as you said! It seems better to actually put every "Domain" classes in Models for my project, and just as it is mentioned in the second video, if you already have some logic in other projects, then you may bother to decide how to use separate projects. For now, lets put everything in the web app project and follow MVA guidances, thank you! \$\endgroup\$ Aug 12, 2016 at 20:28
  • \$\begingroup\$ That's great news! You'll probably want to follow up with "Implementing Entity Framework" course and it's a good idea to understand how "Razor syntax" is used by MVC to emit HTML also. \$\endgroup\$ Aug 12, 2016 at 20:38
  • \$\begingroup\$ Thanks! I'll follow those courses as soon as I finish the first one you suggested \$\endgroup\$ Aug 12, 2016 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.