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?
views:
119answers:
3The 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
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 )
{
...
}
}
recently I discover DDD events and ThreadStatic attribute used in this cntext, what do you think?