MVC Repositiry模式 [英] MVC Repositiry Pattern

查看:109
本文介绍了MVC Repositiry模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用不同的层开发简单的任务,一切都运行正常,但我有一个场景,我需要在点击保存,一个员工表和其他员工定位表时插入两个表。这是我的代码。

在服务类中

  public   void 更新(EmployeeViewModel employeeViewModel)
{
员工员工= 员工()
{
EmployeeId = employeeViewModel.EmployeeId,
FirstName = employeeViewModel.FirstName,
LastName = employeeViewModel.LastName,
PhoneNumber = employeeViewModel.PhoneNumber,
Address = employeeViewModel。地址,
EmployeePositions = new 列表
{
EmployeePosition
{
PositionId = employeeViewModel.PositionId,
EmployeeId = employeeViewModel.EmployeeId
}
}
};

if (employee == null
throw new ArgumentNullException( 员工更新);
_employeeRepository.Update(employee);
_employeePositionRepository.UpdateCollection(employee.EmployeePositions);
}



在存储库类中

  public   void  UpdateCollection(ICollection collection)
{
if (collection == null
throw new ArgumentNullException( collection is null);
foreach var item in collection)
{
DataContext.Entry(item).State = EntityState.Deleted;
}
this .DataContext.SaveChanges();
}

和COntroller类



 [HttpGet] 
public ActionResult Edit( int ?id)
{
var employee = _employeeService.GetById(( int )id);
if (employee == null throw new ArgumentNullException( 找不到员工信息);
return 查看(员工);
}

[HttpPost]
public ActionResult编辑(EmployeeViewModel员工)
{
_employeeService.Update(雇员);
return RedirectToAction( 查看 员工);
}





现在,当发生更新时,我需要删除该employeeid的employeeposition表中的记录并插入它。 />
如何执行此操作

解决方案

您似乎只是尝试编辑特定EmployeeId的数据。正如您所说当更新发生时,我需要删除该employeeid的employeeposition表中的记录并插入它。为此,我们通常只编辑或更新该记录。我在下面的文章中使用了存储库模式来完成它:



ASP.NET MVC 5中的WCF RESTful服务和WebGrid - 第1部分 [ ^ ]



在ASP中使用部分视图和jQueryUI的CRUD操作。 NET MVC4 - 第1部分 [ ^ ]



如果您使用的是ADO.NET或其他数据库,则需要在Repository中相应地编写查询。感谢。

Hi, I am developing simple task using different layers and everything is working fine but I have scenario where I need to insert to two tables when click on save ,one employee table and other employeeposition table. Here is my code.
In Service class

public void Update(EmployeeViewModel employeeViewModel)
        {
            Employee employee = new Employee()
            {
                EmployeeId = employeeViewModel.EmployeeId,
                FirstName = employeeViewModel.FirstName,
                LastName = employeeViewModel.LastName,
                PhoneNumber = employeeViewModel.PhoneNumber,
                Address = employeeViewModel.Address,
                EmployeePositions = new List 
                {
                    new EmployeePosition 
                    {
                        PositionId=employeeViewModel.PositionId,
                        EmployeeId=employeeViewModel.EmployeeId
                    }
                }
            };           

            if (employee == null)
                throw new ArgumentNullException("Employee Update");
            _employeeRepository.Update(employee);
            _employeePositionRepository.UpdateCollection(employee.EmployeePositions);
        }


In Repository Class

public void UpdateCollection(ICollection collection)
        {
            if (collection == null)
                throw new ArgumentNullException("collection is null");
            foreach (var item in collection)
            {
                DataContext.Entry(item).State = EntityState.Deleted;
            }
            this.DataContext.SaveChanges();
        }

and COntroller class

[HttpGet]
        public ActionResult Edit(int? id)
        {
            var employee = _employeeService.GetById((int)id);
            if (employee == null) throw new ArgumentNullException("Employee Information Not Found");
            return View(employee);
        }

        [HttpPost]
        public ActionResult Edit(EmployeeViewModel employee)
        {
            _employeeService.Update(employee);
            return RedirectToAction("View", "Employee");
        }



Now when ever update happens I need to delete records in employeeposition table for that employeeid and insert it.
How can I do this

解决方案

It seems you just trying to edit data for a particular EmployeeId. As you said "when ever update happens I need to delete records in employeeposition table for that employeeid and insert it." For that often we just edit or update that record. And I have done it using repository pattern in below articles:

WCF RESTful service and WebGrid in ASP.NET MVC 5 - Part 1[^]

CRUD Operations using Partial View and jQueryUI in ASP.NET MVC4 - Part 1[^]

If you are using ADO.NET or other DB you need to write your query accordingly in Repository. Thanks.


这篇关于MVC Repositiry模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆