I’m trying to grasp DDD here, and I’ve read somewhere that it is possible to reuse the domain entities as viewmodels. I’ve created a simple CRUD app, but at the U(pdate), I get a bit stuck at “?????”:
// snippet of the content of the ItemController:
public ViewResult Edit(Guid id)
{
Item item = _itemDomainService.GetFromCurrentUser(id);
return View(item);
}
[HttpPost]
[Transaction]
public ViewResult Edit(Item item)
{
// Validate here
//????
return Redirect("Item", "Details", item.Id);
}
I’m using nhibernate with the session per request pattern (“Transaction” is a custom attribute). But “item” is not “tracked” (it is transient) to the nhibernate session. What is the preferred method of getting the changes in “item” into the corresponding row in the database?
I’m thinking of the folowing options myself:
-
Map all properties to the persistent “item”, so (at the “????”):
var fromDB = _itemDomainService.GetById(item.Id); fromDB.Name = item.Name; fromDB.Description = item.Description // etc -
Attach the item to session:
// of course session would be abstracted away, but for brevity pretend it gets injected into the controller session.Update(item); - My design is plain wrong, and I need to (for example) not use the session-per request…
What are your thoughts on this?
Hmm, tricky because I know MVC but not NHibernate. I do know Entity Framework though so I’ll assume they are similar enough for this to still be useful I hope:
I would do something like: