Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. enter image description here 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

like image 889
Androme Avatar asked Jun 12 '11 12:06

Androme


People also ask

What is event in MVC?

ASP.NET MVC 5 for Beginners An event is an action or occurrence such as a mouse click, a key press, mouse movements, or any system-generated notification.

What is MVC grid?

The grid control for ASP.NET MVC is an efficient display engine for tabular data. It will pull from a datasource, such as List of collections, OData web services, or DataManager, binding data fields to columns and displaying a column header to identify the field.


2 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.

like image 61
Paul Avatar answered Sep 23 '22 13:09

Paul


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.

like image 36
Kon Avatar answered Sep 24 '22 13:09

Kon