How to handle cross-cutting concerns in an ASP.NET MVC 3 Application?

1.6k Views Asked by At

I've got an ASP.NET MVC 3 web application, with various components/layers such as:

  1. Web
  2. Services (cache, external API's, cloud services, etc)
  3. Core (domain logic, POCO's, etc)
  4. Repository (Entity Framework)

Now, when i do something in my website (e.g submit a form, aka a POST) - in the worst case scenario, all layers might need to be notified.

Now, i could build all this logic into my controllers HTTP POST action, but it gets really fat and weighty in logic.

I've dabbled with using the Publisher-Subscriber pattern (AOP), but i haven't found a very good .NET implementation as of yet, and i've also had people telling me this is not good practice for web applications.

Can anyone give me any advice here, or am i stuck with code like this:

[HttpPost]
public ActionResult Do(Something model)
{
    _repository.Save(model); // this should be the only line IMO
    _service.DoSomething();
    _repository.DoSomethingElse();
    _domain.DoSomethingMore();
}

I'm not talking about multi-threading or anything here, but simply taking the onus off the controller, and putting it on the components which care about these actions.

2

There are 2 best solutions below

1
On

If you haven't totally given up on PubSub you can take a look at RabbitMQ. http://www.rabbitmq.com/

I don't understand why it's not 'best-practice' for web applications, those kinds of absolutes are rarely correct anyway.

0
On

One good PubSub script would be Faye, which is built on Node.js. It is really good and can be used for your case.

Faye is a publish-subscribe messaging system based on the Bayeux protocol. It provides message servers for Node.js and Ruby, and clients for use on the server and in all major web browsers.