Working on a C# WinForms program that is written in a MVC ( actually Model-View-Presenter) style and I want to add a few lines of code that is responsible for logging some events. Where should I write two or three lines of code that I need? Should I write it in the Presenter section?
To get an idea, here is some lines of sample code that already exists in the Save() metohd in Company.MyApplication.Presenter.MyPresenter.cs class:
he has written codes lie the following in this part of presenter:
private void Save(Helper.SaveStatusEnum status)
{
if (notification.CheckLocks(orderIdCollection))
{
using (new HourglassController())
{
controller.FireActiveCellLeaving();
ViewDocumentedValues();
int result = saveController.Save(status);
if (result == Helper.SAVE_SUCCESSFUL)
{
// IS IT OK TO WRITE MY COUPLE LINES OF CODE IN HERE???????????
model.Dirty = false;
if ((model.CurrentStatus == Helper.OrderStatusEnum.Complete) || (model.CurrentStatus == Helper.OrderStatusEnum.Corrected))
{
controller.EnableDisableSheet(false);
}
CheckApplicationState();
SheetHelper.ClearUnsavedDataRowImage(view.ActiveSheet);
}
else
{
MessageBox.Show("An unexpected error occuring trying to save.");
}
}
}
}
5
Logging is a cross-cutting concern. There is no right place for logging; you do it where you need to.
MVC is agnostic in that regard; it has nothing to say about where you put lines of code that call your logger.
It depends on the goal you have with the logging. If the thing you want to log is a successful Save
exactly and only at the position in your code you marked above, then it will obviously be the right place. But if you want to log every sucessful call to saveController.Save
in your code, you better place the logging call within that method.
And don’t think too much about the MVC architecture with logging – logging is a cross-cutting concern, which can make sense in any layer, depending on what you want to achieve. For example, if you are logging for debugging purposes, it could even make sense in the view layer.
Probably the model would be the best place since it is the closest part to the system, and logging is done on the system (usually).
View and Presenter are more likely responsible with showing data to user and handling user actions.
EDIT:
If you want to add logging just for ad-hock debugging and than immediately remove it so that it doesn’t get into production you can write it to wherever you want. However if I would want to add long-term logging I would add it in model.
But I don’t see these things as absolute bad or good solutions. If you have no way, for some well understood reasons, to add it to the models you can add it to the presenter also. Probably the worst place the log could be is the view, so avoid it as much as you can.
2
I recommend watching Putting your controllers on a diet by Jimmy Bogard. It’s for MVC 2, but it’s still very relevant in MVC 4. It’s how I now code my MVC applications. It’s simple and it creates separation of concerns.
Video Covers:
- Dependency Injection
- AutoMapper
- Model Binding
- Action Results
You can squeeze in logging wherever you want.. but the patterns described in the video result in efficient MVC development..
The beauty of the approach is it eliminates the nested if statements commonly found in validation and data-retrieval code in an application entry point (i.e. button clicks)
update
I just realized the question is for MVP in Windows Forms, I still believe that many of the idea’s in Jimmy’s video apply.
1