views:

119

answers:

3

I'd like to collect statistical information, such as PageViews, Number of comments for each user, etc. I've thought about using the observer pattern. How would I implement this in ASP.NET MVC?

A: 

The observer pattern doesn't really fit here. The functionality you describe should be captured using whatever model your application is using. So for example, if you want to track number of comments for each user, then that means you likely have a comments database table. You can simply write a SQL query that gives you a list of users and a count of the comments they have created in that table.

something like

select userid, count(*) from comments group by userid
Joel Martinez
+1  A: 

For the cases where you can't collect the information via a direct query against the database, I would think about implementing this via filter attributes. Develop a custom filter that collects the information that you need and archives it. Decorate the actions that you want the filter to collect information on with the filter. See this MSDN page on how to create a custom action filter.

 [PageViewCounter]
 public ActionResult Index()
 {
 }

 public class PageViewCounterAttribute : ActionFilterAttribute
 {
      public override OnActionExecuting( ActionExecutingContext filterContext )
      {
         ...
      }  
 }
tvanfosson
I really like the use of this in this case. This also allows you to not record a PageView when the action is a post, which would inflate your statistics. I also recently saw OnActionExecuted and OnActionExecuting used in tandem as a way to persist ModelState after validation.
Anthony Potts
Anyone care to explain the downvote? I'd be interested in hearing opinions on why using filters isn't a good idea.
tvanfosson
It's too simplistic and heuristic mode.
diadiora
If by that you mean that you have to remember to apply the filter, then I guess so. I would say that it's also flexible in that it only gets applied to the actions you care to track. On the other hand, you can write unit tests to make sure that the filter is applied. You can get a similar effect by creating a base controller that overrides actionexecuting/actionexecuted but you have a similar problem in making sure your controllers derive from the base controller.
tvanfosson
I want to have an implementation smth similiar with event listeners for NHibernate.Event namespace such as IPostLoadEventListener, IPreDeleteEventListener. I must agree with you in the use of a custom ActionFilterAttribute, but my question was not in this attribute, but in the way the structure for observer can be accomplished. I mean some more generic and nice way.
diadiora
But the filters are essentially that -- the framework has hooks to make sure the filter gets called when the action (or any action in a controller, if the filter is applied at the controller level) is invoked. Just consider the "event" as the action being invoked and the filter as the "event handler". If I'm misunderstanding you, perhaps you could add a concrete example for which this wouldn't work to your question.
tvanfosson
A: 

recently I discover DDD events and ThreadStatic attribute used in this cntext, what do you think?

diadiora