You just need to make sure SaveChanges gets called before your request finishes. At the bottom of a controller action is an ideal place. My controller actions typically look like this: public ActionResult SomeAction(...) { _repository.DoSomething(); ... _repository.DoSomethingElse(); ... _repository.SaveChanges(); return View(...); } This has the added benefit that if an exception gets thrown, then SaveChanges will not get called.
And you can either handle the exception in the action or in Controller.OnException.
– drasto Mar 22 at 19:15 1 The problem with that is what if (in my example) DoSomething() finishes and adds a valid change to the repository. But then DoSomethingElse() fails, and so at this point I no longer want to save any changes? If you automatically called SaveChanges after all requests no matter what, it could lead to corrupt data.
Basically the pattern above is a poor man's transaction.It would be even better to really run all of these things in a real transaction. – Matt Greer Mar 22 at 19:18 @Matt Greer isn't there some way to check if there was exception during action execution from action filter? That would solve problem you are addressing and it would be still 'automatic'... – drasto Mar 22 at 19:29 @drasto - I'm sure there is.
If you really want to automatically call SaveChanges you could probably devise something that would work. Something like set a flag in Controller. OnException and if that flag is set don't call SaveChanges.
IMO it'd be more work than it was worth. But then again, I'm also generally not a fan of "magic" like that happening, I like to keep explicit control for the most part. – Matt Greer Mar 22 at 19:52 1 For me I return IEnumerable as I don't think the controller should be massaging data.It should ask the repo a question, get an answer, and just pass the answer on to the correct view.
The controller shouldn't know or care whether the data the repo just gave it is in memory or still in the database. But that's just my opinion, other people like to return IQueryable and let the controller filter/change/whatever the data before the actual db query takes place. – Matt Greer Mar 22 at 20:40.
It's going to be no more or less efficient than calling a stored procedure that many number of times (with respect to number of connections that need to be made). Nominally, you would make all your changes to the object set, then SaveChanges to commit all those changes. So instead of doing this: mySet.Objects.
Add(someObject); mySet.SaveChanges(); mySet.OtherObjects. Add(someOtherObject); mySet.SaveChanges(); You just need to do: mySet.Objects. Add(someObject); mySet.OtherObjects.
Add(someOtherObject); mySet.SaveChanges(); // Commits Both Changes.
Usually your data access is wrapped by an object implementing the repsitory pattern. You then invoke a Save() method on the repository. Something like var customer = customerRepository.
Get(id); customer. FirstName = firstName; customer. LastName = lastName; customerRepository.SaveChanges(); The repository can then be wrapped by a service layer to provide view model objects or DTO's Isn't that little inefficient?
Don't prematurely optimise. When you have a performance issue, analyse the performance, identify a cause and then optimise. Repeat.
Update A repository wraps data access, usually a single entity. A service layer wraps business logic and can access multiply entities through multiple repositories. It usually deals with 'slim' models or DTO's.An example could be something like getting a list of invoices for a customer public Customer GetCustomerWithInvoices(int id) { var customer = customerRepository.
Get(id); var invoiceList = invoiceRepository. GetAllInvoicesFor(id); return new Customer { Customer = customer, Invoices = invoiceList }; }.
– drasto Mar 22 at 19:12 1 @drasto I've added an update for you. – David Glenn Mar 22 at 19:31.
In my ASP.NET MVC application I need to implement persistence of data. I've choose Entity Framework for its ability to create classes, database tables and queries from entity model so that I don't have to write SQL table creation or Linq to SQL queries by hand. So simplicity is my goal.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.