repo.SaveChanges()
Entity Framework 5リポジトリでメソッドを公開するかどうかについて、別の開発者と話し合っています。
次のような変更を自動的に保存するリポジトリを作成することを検討しています(迅速で汚い例)。
public class Repo {
private OurEfContext _context = new OurEfContext();
public void PersistCustomer(Customer cust)
{
// update customer code here
_context.SaveChanges(); // <-- is this okay?
}
}
検討している他のオプションは、次のような別のRepo.PersistChanges()メソッドを公開することです。
public class Repo {
private OurEfContext _context = new OurEfContext();
public void PersistCustomer(Customer cust)
{
// update customer code here
// no call to _context.SaveChanges();
}
public void PersistChanges()
{
_context.SaveChanges();
}
}
私が見たほとんどの例では、2番目のパターンを使用しています。1つのパターンは他のパターンよりも「正しい」ですか?