0

私は子テーブルを持つテーブルに情報を追加する方法について議論してきました。ユーザーを初期化するとき、ユーザーはパスワードを暗号化し、データを子クラスに挿入する必要があります。私がRepository、Unit of Work Patternsをよく知っているわけではないので、私はこれを最初に正しく行うようにしたいと思います。ありがとう!リポジトリパターンを使用してユーザーパスワードを保存しようとしています

私のテーブルは以下のとおりです。

ユーザー、UserRoles

マイコード:コメントから

[HttpPost] 
    public ActionResult AddUser(User user) 
    { 
     try 
     { 
      if (ModelState.IsValid) 
      { 
       uow.UserRepository.Insert(user); 
       uow.Save(); 
       return RedirectToAction("Index", "User"); 
      } 
     } 
     catch (DataException) 
     { 
      ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); 
     } 
     return View(user); 

    } 

public class UserRepository : IUserRepository, IDisposable 
{ 
    private StudentSchedulingEntities _context; 

    public UserRepository(StudentSchedulingEntities context) 
    { 
     if (context == null) 
      throw new ArgumentNullException("context"); 

     _context = context; 
    } 
    public IEnumerable<User> GetUsers() 
    { 
     return _context.Users.ToList(); 
    } 
    public User GetUserByID(int id) 
    { 
     return _context.Users.Find(id); 

    } 
    public void InsertStudent(User user) 
    { 
     _context.Users.Add(user); 
    } 
    public void DeleteStudent(int userID) 
    { 
     User usr = _context.Users.Find(userID); 
     _context.Users.Remove(usr); 
    } 
    public void UpdateStudent(User user) 
    { 
     _context.Entry(user).State = EntityState.Modified; 
    } 
    public void Save() { 
     _context.SaveChanges(); 
    } 
    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 
    protected virtual void Dispose(bool disposing) 
    { 
     if (disposing) 
     { 
      if (_context != null) 
      { 
       _context.Dispose(); 
       _context = null; 
      } 
     } 
    } 
} 

public interface IUserRepository : IDisposable 
{ 
    IEnumerable<User> GetUsers(); 
    User GetUserByID(int userID); 
    void InsertStudent(User user); 
    void DeleteStudent(int userID); 
    void UpdateStudent(User user); 
    void Save(); 
} 

public class UnitOfWork : IDisposable 
{ 
    private StudentSchedulingEntities _context = new StudentSchedulingEntities(); 
    private GenericRepository<User> userRepository; 

    public GenericRepository<User> UserRepository 
    { 
     get 
     { 

      if (this.userRepository == null) 
      { 
       this.userRepository = new GenericRepository<User>(_context); 
      } 
      return userRepository; 
     } 
    }   

    public void Save() 
    { 
     _context.SaveChanges(); 
    } 

    private bool disposed = false; 

    protected virtual void Dispose(bool disposing) 
    { 
     if (!this.disposed) 
     { 
      if (disposing) 
      { 
       _context.Dispose(); 
      } 
     } 
     this.disposed = true; 
    } 

    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 
} 

更新:

[HttpPost] 
    public ActionResult AddUser(AddUserViewModel auvm) 
    { 
     try 
     { 
      if (ModelState.IsValid) 
      { 
       uow.UserRoleRepository.Insert(auvm.UserRole); 
       uow.UserRepository.Insert(auvm.User); 

       uow.Save(); 
       return RedirectToAction("Index", "User"); 
      } 
     } 
     catch (DataException) 
     { 
      ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); 
     } 
     return View(auvm); 

    } 

public class AddUserViewModel 
{ 
    public User User { get; set; } 
    public UserRole UserRole { get; set; } 
    public UserRoleType UserRoleType { get; set; } 
    public IEnumerable<SelectListItem> UserRoleTypes { get; set; }  
} 

答えて

0

単に新しいユーザーオブジェクトを作成します。 UserRolesをユーザーに割り当てます。ユーザーオブジェクトを保存します。コンテキストクラスは作業単位として機能します。変更の保存を呼び出すと、のみがに保存されます。

  1. 新しいユーザーを作成します
  2. (必要な場合)上記 あなたのコードの保存
  3. コールリポジトリInsertStudent
  4. コールはこれまでに、役割を除き、これをやっているように見えるのユーザーにロールを追加します。それは私にとっては問題ないようです。

あなたのUserクラスによく似たUserCreateViewModelを投稿するように、ViewModelルート(推奨)を選択する人もいます。このようにして、ユーザークラスが変更された場合、またはすべてがユーザーに公開されないようにする場合は、これらのケースをより簡単に処理できます。

+0

私はあなたを正しくしているかどうかはわかりませんが、私は既にView Modelを追加しましたが、私が望むようにこの作業をするために何が必要なのか分かりません。私は 'USER'テーブルのパスワードを暗号化し、UserRoleテーブルに' UserRole'を追加する必要があります。 – xivo

関連する問題