TAKE TWO!
After much hesitation and doubt, and the advice of numerous people over the past year, I’ve returned to the ddd version of my app and have made a lot of progress. However, I do have some things that concern me. I will try and express those concerns here in hopes that one of you will have an answer for me.
1) Entity Traversal
In a typical ddd application one entity may own one or more of another type of entity. New owned entities are added to the owning entity and then the owning entity is then saved.
Foo parent = fooRepository.SelectById(42);
Bar child = new Bar("something_unique");
parent.AddBar(child);
fooRepository.Update(parent);
Ok, this is fine, but what is happening in my application is when a self referencing entity adds children, and then those children have children, and so on for several generations.
If somebody is viewing the details of such an entity four generations deep (for example), and that entity has no back referencing, how am I expected to build a navigation bar like the one at the top of this very page?
SitePoint Forums > Forum Index > Program Your Site > .NET > Need MVC expert to verify I am on right track.
Consider this following action of the forum controller:
public ActionResult View(int id)
{
Forum entity = forumRepository.SelectById(id);
return View(Mapper.Map<Forum, ForumViewModel>(entity));
}
Without back referencing, I have no way for the view to build a navbar (via HtmlExtension method).
- ViewModel Missmatch
In some cases, I require a confirmation before an action will proceed. Deletions are such a case. Here is an example:
public class ForumDeleteModel
{
public int Id { get; set; } // entity id
public string Title { get; set; } // used to prompt user
public bool Confirmed { get; set; } // user to get permission
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Delete(ForumDeleteModel model)
{
ConsumeErrors(validationService.Validate(model));
if (!ModelState.IsValid) return View(model);
// the rest is skipped secondary to explanation below
}
The problem in the above example is that the view only presents the user with a prompt like “Do you really want to delete forum named: {0}” where Title is formatted in. The model property Title is not however saved in a hidden field and is therefor not set upon post. If validation fails, and the model re-presented, there is no value for Title.
I have two solutions here. One is to drop it onto the form in a hidden field, and that may make sense for this one form, but what about forms that display a great deal more temporary info? It doesn’t seem logical to persist all that data in hidden fields. The second solution is to re-construct the model before sending it to the view. This also seems to be a little wrong, as I don’t have to do it with other actions and models.
I am not sure how to proceed with this.
I am sure I will come up with more, but for now, this will do. Please advise.