Unit of Work Problem


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
 
Ad

Advertisements

M

miher

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top