Unit of Work Problem

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I implemented an Unit of Work pattern over a Entity Framework model
and I am able to use it as follows:

ISession session = EFContext();
ArticleRepository articleRepository = new ArticleRepository(ISession);
ProductRepository productRepository = new ProductRepository(ISession);
Article article = articleRepository.GetById(2);
article.Title = "New title";
productRepository.DeleteById(3);
session.Commit();

So I submit all changes on session.Commit();

Now the problem is that I am using ASP.NET MVC as follows:

First I get a model by mapping an Article to ArticleFormViewModel and
return to the view (Html page):

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Edit(Int32 id) {
ArticleFormViewModel model = Mapper.Map<Article,
ArticleFormViewModel>(articleRepository.GetById(id));
return View(model);
} // Edit

In the form the ArticleFormViewModel is updated and posted:

[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken,
ValidateInput(false)]
public ActionResult Edit(ArticleFormViewModel model) {
Article article = Mapper.Map<ArticleFormViewModel, Article>
(model);
session.Commit();
return View(model);
} // Edit

As you can see I create a new Article from the ArticleFormViewModel
but when I commit the session the Article is not saved ... For me it
is like the Article was not obtained from the session ...

Anyway, I am not sure what is the correct way to solve this Update
problem on an Unit of Work pattern.

Without the ASP.NET MVC example it would be something like:

ISession session = EFContext();
Article article = new Article { Id = 2, Title = "some title };
session.Commit();

I know that article with Id 2 exists ... I need to update it but I
receive it from somewhere else and not by using the repository ...

Thanks,
Miguel
 
shapper said:
Hello,

I implemented an Unit of Work pattern over a Entity Framework model
and I am able to use it as follows:

ISession session = EFContext();
ArticleRepository articleRepository = new ArticleRepository(ISession);
ProductRepository productRepository = new ProductRepository(ISession);
Article article = articleRepository.GetById(2);
article.Title = "New title";
productRepository.DeleteById(3);
session.Commit();

So I submit all changes on session.Commit();

Now the problem is that I am using ASP.NET MVC as follows:

First I get a model by mapping an Article to ArticleFormViewModel and
return to the view (Html page):

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Edit(Int32 id) {
ArticleFormViewModel model = Mapper.Map<Article,
ArticleFormViewModel>(articleRepository.GetById(id));
return View(model);
} // Edit

In the form the ArticleFormViewModel is updated and posted:

[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken,
ValidateInput(false)]
public ActionResult Edit(ArticleFormViewModel model) {
Article article = Mapper.Map<ArticleFormViewModel, Article>
(model);
session.Commit();
return View(model);
} // Edit

As you can see I create a new Article from the ArticleFormViewModel
but when I commit the session the Article is not saved ... For me it
is like the Article was not obtained from the session ...

Anyway, I am not sure what is the correct way to solve this Update
problem on an Unit of Work pattern.

Without the ASP.NET MVC example it would be something like:

ISession session = EFContext();
Article article = new Article { Id = 2, Title = "some title };
session.Commit();

I know that article with Id 2 exists ... I need to update it but I
receive it from somewhere else and not by using the repository ...

Thanks,
Miguel

Hi,

I think You need to attach the freshly created object to the context /
session.
See : http://msdn.microsoft.com/en-us/library/bb896271.aspx

-Zsolt
 
Back
Top