views:

211

answers:

2

Hi!

I've implemented a service layer in my application like: http://www.asp.net/learn/mvc/tutorial-38-cs.aspx

(I use Linq2SQL). Now I've trouble in implementing the Edit ActionResult. In the Create (Post) ActionResult I take the service method:

if (_service.CreateMovie(movie))
{
     return RedirectToAction("Details", new { id = movie.ID });
}
else
{
     return View(movie);
}

Thats okay. Now my problem in the Edit ActionResult is: how do I implement the update of an entity?

In the Repository I have following Update method:

public bool UpdateMovie(Film movieToUpdate)
{
    try
    {
        _db.SubmitChanges();
        return true;
    }
    catch
    {
        return false;
    }
}

The Service then calls the Repository. But the changes made in the Form, are not "sended" to the model, so the entity was not updated by the new values.

I could call "UpdateModel" in the Controller, but then I must call also the Validate in the Service. But then the validation logic are no longer in the service than in the Controller.

I hope you understand my question.

A: 

You will need to first retrieve the newly inserted movie so that L2S knows about it. Then apply whatever changes happened from movieToUpdate to this newly retreived movie object and that will persist the changes. Remember that you want to apply SaveChanges to the same context as you did to get the movie, otherwise L2S won't know what to do with it.

RailRhoad
A: 

I don't found any method to update the model, without basic-type validation. So I've implemented UpdateModel and a custom DefaultBinderMessage. At the moment this is adequate for my claim. Elsewhere I could implement the Error-Interface to do all validations with the service layer.