locked
How to change the default ASP.Net MVC code to Repository-Service pattern RRS feed

  • Question

  • User1242168447 posted

    Hi I'm willing to use Repository-Service pattern using the default asp.net mvc 4 code in visual studio 2013 Pro but I'm stuck.

    So far, I'm about to move the class/files related to the user accounts to the domain Model already which is (IdentityModels.cs and AccountViewModels.cs)

    but I'm a bit confuse as reading Repository-Service pattern I must only have one Model class for the user account and its own repository.

    can you please assist?

    These are the three Repository manual that I used.

    Not sure if it's not enough but I understand it fully.

    https://youtu.be/BJWQmq709kE

    - https://youtu.be/ZoSno6HcQHA

    https://exceptionnotfound.net/the-repository-service-pattern-with-dependency-injection-and-asp-net-core/

    I just wanted to practice now from my own side project.

    IdentityModels class

    //You can add profile data for the user by adding more properties to your ApplicationUser class

    public class ApplicationUser : IdentityUser
    {
    
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public DateTime Date { get; set; }
    
    public static HttpContext Current { get; set; }
    
    }
    
    
    
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
    public ApplicationDbContext()
    : base("DefaultConnection")
    {
    }
    }

    AccountViewModels.cs

    public class ExternalLoginConfirmationViewModel { [Required] [Display(Name = "User name")] public string UserName { get; set; } } public class ManageUserViewModel { [Required] [DataType(DataType.Password)] [Display(Name = "Current password")] public string OldPassword { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "New password")] public string NewPassword { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm new password")] [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")] public string ConfirmPassword { get; set; } } public class LoginViewModel { [Required] [Display(Name = "User name")] public string UserName { get; set; } [Required] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [Display(Name = "Remember me?")] public bool RememberMe { get; set; } } public class RegisterViewModel { [Required] [StringLength(100, ErrorMessage = "First name field cannot be empty", MinimumLength = 3)] [Display(Name = "First name")] public string FirstName { get; set; } [Required] [StringLength(100, ErrorMessage = "Last name field cannot be empty", MinimumLength = 3)] [Display(Name = "Last name")] public string LastName { get; set; } [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email")] public string Email { get; set; } //[Required] [Display(Name = "User name")] public string UserName { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } [DataType(DataType.Date)] [Display(Name = "Date time")] public DateTime Date { get; set; } }

    Monday, June 22, 2020 9:02 PM

Answers

  • User475983607 posted

    So far, I'm about to move the class/files related to the user accounts to the domain Model already which is (IdentityModels.cs and AccountViewModels.cs)

     Identity an API for managing user accounts.  Why do you want to rewrite Identity?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 22, 2020 9:12 PM
  • User-474980206 posted

    You should fully understand a design pattern and use cases before implementing one. What reason are you using this pattern? If this is just a test project to learn the pattern, then that’s ok. But in that case, you just stick with unit tests. No need for code.

    if this is for a real project, then as you don’t understand the pattern, you should start with an implementation like entity framework.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 23, 2020 12:40 AM

All replies

  • User475983607 posted

    So far, I'm about to move the class/files related to the user accounts to the domain Model already which is (IdentityModels.cs and AccountViewModels.cs)

     Identity an API for managing user accounts.  Why do you want to rewrite Identity?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 22, 2020 9:12 PM
  • User-474980206 posted

    You should fully understand a design pattern and use cases before implementing one. What reason are you using this pattern? If this is just a test project to learn the pattern, then that’s ok. But in that case, you just stick with unit tests. No need for code.

    if this is for a real project, then as you don’t understand the pattern, you should start with an implementation like entity framework.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 23, 2020 12:40 AM
  • User1242168447 posted

    You should fully understand a design pattern and use cases before implementing one. What reason are you using this pattern? If this is just a test project to learn the pattern, then that’s ok. But in that case, you just stick with unit tests. No need for code.

    if this is for a real project, then as you don’t understand the pattern, you should start with an implementation like entity framework.

    Thank you Mark.

    But how do I start with an implementation like entity framework?  Do you have any good tuto or youtube video?

    as I watched so far two youtube videos that are clear and read a web site on repository pattern and practiced that's why I'm now trying to convert my existing MVC Web App to Repository pattern to grasp the concept.

    I use repository-service pattern at work in a large enterprise software developed by Senior Dev and Software Architecture in my company but I'm only doing maintenance on that web app (add new features and fixing bugs) so I didn't really developed the web app from scratch... and I see myself not capable of writing such a large enterprise web app from scratch that's why I decided to convert my own side project to repository-pattern from scratch.

    Tuesday, June 23, 2020 7:49 AM
  • User1242168447 posted

    mgebhard

    Identity an API for managing user accounts.  Why do you want to rewrite Identity?

    Thanks mgebhard.

    I'm using the default code generated from asp.net mvc with authentication.

    How do I identify an API for managing useraccounts?

    Tuesday, June 23, 2020 7:52 AM
  • User1686398519 posted

    Hi LetMeCode,

    You can refer to this link to realize how to change to repository pattern.

    This link uses repository pattern and combines with EF to implement CRUD.

    Best Regards,

    YihuiSun

    Tuesday, June 23, 2020 9:00 AM
  • User475983607 posted

    How do I identify an API for managing useraccounts?

    By definition, ASP.NET Identity is an API for managing accounts.  You should learn the basics.

    Tuesday, June 23, 2020 10:22 AM
  • User1242168447 posted

    LetMeCode

    So far, I'm about to move the class/files related to the user accounts to the domain Model already which is (IdentityModels.cs and AccountViewModels.cs)

     Identity an API for managing user accounts.  Why do you want to rewrite Identity?

    I don't intend to rewrite Identity, I'm just adding fields so that when a user create an account, he must also fill in other info such as Firstname, Surnname...

    There's no other way to do. I asked that on Stackoverflow month ago and they suggested me to do that that way.

    Wednesday, June 24, 2020 8:55 PM
  • User475983607 posted

    I don't intend to rewrite Identity, I'm just adding fields so that when a user create an account, he must also fill in other info such as Firstname, Surnname...

    There's no other way to do. I asked that on Stackoverflow month ago and they suggested me to do that that way.

    Are you asking how to require a field in MVC?  

    Wednesday, June 24, 2020 10:05 PM