ASP.NET MVC Events

lately have i had this idea in my head about using events in a ASP.NET MVC application. For an example When a user created a comment, will this on the server side trigger an event, that for an example the account controller can be hooked on to, and thus triggering account specified code on a event. Have any 1 tried this or is it even possible, to use events as such on a web application?

Here is an example. A uses created a comment on my website, though the comments controller, this will make the Comments controller trigger an event in my static event manager. This will execute all the methods that have been tied to this event. In this example is "OnCommentCreate()" in the AccountController attached. Now all this should happen on another thread then the users, so the page for the user will continues as there were no events, while the events are handling behind. My thought where to use this with a persistent connection on a timeline, or it can be used to decentralize code. I could for an example have an event that would be called when a username changes, there will go through the log files and change the name of the user to the new username.

I hope this clarifies better

Answers


You can absolutely do this if you want to, DoomStone. ASP.NET MVC itself doesn't do events using the .NET event model, but what you're describing isn't really .NET events either; it's more of a publish & subscribe bus in your app.

One thought is that depending on how many events you start doing, and what you have them do, you might look into using purpose-built eventing frameworks or WF, or possibly a message queue.

That said, I would also encourage considering other options of accomplishing your goal. The examples you have given don't really strike me as the sweet spot for event-based programming, unless I'm misunderstanding something.

** Update ** I had another thought after your comment. So, short answer is that 'yes' this is a well-known pattern, especially for decoupling services that maybe exist on other servers and maybe (in some cases) aren't even owned by you. There are available frameworks out there, but what you're describing could be completely home-grown. You can either make a method on your WKO for each event (like your example shows), or if you want more flexibility you can define a generic action (.e.g. Publish or Send) which takes in a custom Message class, and then folks calling the WKO can send a message which is typed to the event (in other words, rather than calling OnCommentCreated(), they might call Publish(new CommentCreatedMessage({data})); Doing it that way tends to make the solution more able to grow.


ASP.NET MVC Framework is not event driven, so (as far as I know) your only option is to check for and handle events manually via client-side code, such as AJAX calls to controller Action methods.

So in your scenario, where comment is submitted and saved to the database, you would need to poll for new comments via periodic AJAX calls to the server.


Need Your Help

Do I need custom allocators for shared pointers?

c++ memory-management shared-ptr

If I override operator new and operator delete (or passing a custom deleter), what use is passing an allocator? Will the shared_ptr at some other time than construction and deconstruction do alloca...

How do you avoid over-populating the PATH Environment Variable in Windows?

windows path environment-variables executable

I would like to know what are the approaches that you use to manage the executables in your system. For example I have almost everything accessible through the command line, but now I come to the l...