Okay, I've been struggling with this for a while now.
I have a standard MVC2 project and use EF4 to access data objects. Many of my data objects (and therefore database tables) have two common fields: UpdateUserAccountID and UpdateDate.
In each of my controllers, I am currently setting this fields manually within each controller on the Edit action:
// POST: /Modes/Edit/2
[HttpPost]
public ActionResult Edit(int id, FormCollection formValues)
{
Mode mode = _repository.GetMode(id);
//Set update flags
mode.UpdateUserAccountID = _userRepository.GetUserID(User.Identity.Name);
mode.UpdateDate = DateTime.Now;
try
{
UpdateModel(mode);
_repository.Save();
return RedirectToAction("Details", new { id = mode.ModeID });
}
catch
{
return View(mode);
}
}
Problem is, I want to update these two fields in a central place, regardless of the type of the controller. I am using Repository/IRepository pattern for my data objects. I also have set up a BaseController as I thought I could pass in my 'mode' object in the example above to a generic function in a base controller to set these two fields. But, I couldn't figure that out and the generic function has no awareness of these specific fields.
Should I be creating a base class for my repositories instead? I can't envisage how that would work within the MVC framework. In webforms, I would just have had an interface that all my models implement with the common fields but i can't seem to fit it in to an MVC approach.
Any help as to the approach to take would be great.