0

我一直在尝试讨论如何在带有子表的表上添加信息。当我初始化用户时,用户需要加密密码并将数据插入到子类中。由于我对存储库、工作单元模式并不完全熟悉,因此我想确保我第一次正确地做到这一点。谢谢!

我的桌子是:

用户,用户角色

我的代码:

[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; }      
}
4

1 回答 1

0

只需创建一个新的用户对象。将 UserRoles 分配给用户。保存用户对象。上下文类充当工作单元 -只有当您调用 Save Changes 时它才会保存所有内容。

  1. 创建一个新用户
  2. 将角色添加到用户(如果需要)
  3. 调用存储库 InsertStudent
  4. Call Save Your code above seems to be doing this except for the roles, so far it seems OK to me.

Some choose to go the ViewModel route (which is recommended) so you would be posting a UserCreateViewModel which looks very similar to your User class. This way if the user class changes or you don't want everything exposed to the user, you can more easily handle these cases.

于 2012-05-09T18:27:52.790 回答