Search code examples
asp.netdesign-patternsmethodology

asp.net observer pattern


I'm working on a project where a central class (the subject), will contain alot of data. There will be an aspx page that displays this data, using .net ajax. When the subject is updated from any page, I want all the open pages to update. I will be using the observer pattern for this. THe question is, do I make the actual aspx page the observer, or is there a better way to do this?


Solution

  • An observer pattern doesn't make sense in this case.

    The web (and ASP.NET) works by way of requests (e.g. from a web browser) and responses (e.g. from a web server).

    An observer pattern requires that the subject (e.g. a web server) maintains references to it's observers (e.g. web browsers) and pushes notification updates to them. Clearly this is not applicable to a web browser-web server scenario.

    I agree with Sergio that the best method is probably to make your clients (web browsers) periodically check for updates from the resource (web server). The neatest method of doing this would be an AJAX call.

    A good example is Gmail, new messages appear in the inbox soon after they are received on the server without the user having to make an explicit request. This is done by AJAX calls which periodically check the Gmail server for new messages.

    UPDATE (2012.03.01):-

    Recent developments have allowed the pushing of data from web server to client to become a more realistic possibility, e.g. SignalR.