[英]Best strategy for adding or updating a collection of related entities when using the EF4?
[英]Best practice for updating related entities in ASP.NET MVC + EF4 without loading entity first
我已经看到了一些与主题相关的问题,但是找不到这个场景的答案。
我有类似的结构
我控制器的一部分
//
// GET: /Person/Edit/5
public ActionResult Edit(int id)
{
var viewModel = new PersonViewModel(PersonRepository.Get(id));
return View(model);
}
//
// POST: /Person/Edit
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(PersonViewModel model)
{
PersonRepository.Update(model.Person, model.Phones);
return RedirectToAction("Index");
}
在我的存储库中我做这样的事情:
public void Update(Person person, ICollection<Phone> phones)
{
using (var unitOfWork = UnitOfWork.Current)
{
Attach(contact);
UpdatePhones(contact, phones);
unitOfWork.Commit();
}
}
public Person Attach(Person person)
{
Context.AttachTo("Persons", entity);
Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
return entity;
}
public void UpdatePhones(Person person, ICollection<Phone> phones)
{
if (phones == null || phones.Count == 0) return;
foreach (var phone in phones.Where(o => o.IsDeleted && !o.IsNew))
{
PhoneRepository.Delete(phone);
}
foreach (var phone in phones.Where(o => !o.IsDeleted && o.IsNew))
{
party.Phones.Add(phone);
}
foreach (var phone in phones.Where(o => !o.IsDeleted && !o.IsNew))
{
phone.PartyID = party.ID;
PhoneRepository.Attach(phone);
}
}
IsDeleted和IsNew不会持久保存到db中并以动态形式使用。 PhonesRepository Attach()是一样的。
我正在做这样的所有更新,因为我需要尽可能减少数据库调用次数。 也许这有一个已知的最佳实践?
谢谢=)
这一点都不错。 与我们的设置非常相似。
几个指针:
1 - 对您的存储库使用泛型。 您的Attach代码非常简单,不需要在实体之间重复。 如果您创建了GenericRepository<T>
,那么您的附加代码将是:
public T Attach<T>(T entity) where T : class
{
Context.GetEntitySet<T>.Attach(entity); // pluralization on (typeof)T.name to get entity set
Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
return entity;
}
2 - 我会将UpdatePhones
方法分成单独的方法。 我不会依赖标志(IsDeleted等)来确定行动方案。 我会更明确。
3 - 看起来你有一个PhoneRepository
? 为什么? Person
是您的聚合根,并且Phone
始终与特定Person
相关,因此您应该只在此聚合边界中具有PersonRepository
。 您应该附加到特定人员的Phones
ObjectSet。
顺便说一句 - 我假设您已禁用延迟加载 ? 如果不是,那些通过ICollection的LINQ操作将导致无声往返。
除了以上几点(主要是设计相关的),优化方面你的代码对我来说很好。
最后一点 - 还有另一种推荐技术(Alex James)用于更新称为“stub techinque”的独立上下文(也称为MVC)中的实体。
我几天前问了一个问题(并自己解决了)如果你有兴趣, 请查看 。
HTH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.