MS MVC, Spring.NET, NHibernate, jQuery and Ajax

I’m finding these tools/technologies to be a great fit for each other.    In my application I’m allowing Spring.NET to inject my Dao objects with Spring.NET handling the session management of NHibernate. In addition, each Dao that is used by a MS MVC controller is injected into the controller by Spring.NET.

So my first real test is loading a Company screen with MS MVC.  The overall UI design is that each aspect of a company will be viewable using jQuery tabs with a tab view.  ie. CompanyInfo, CompanyAddress, CompanyBilling, CompanyBanking, CompanyEmployees, etc… The customer wants quick and easy accesses.  They load a company via a simple search and view this information.

To faciliate this, I created a MS MVC view control for each of the above.  Each tab renders each control.

Initially I noticed a problem here: with NHibernate lazy-load, the view was making many database calls to populate each tab view, regardless if the tab was even viewed.  ie. Billing and Banking info are in the query, and loaded, even if the customer is just looking at ‘company info’.

With some quick and easy jQuery to the rescue and using MS MVC RESTful controller/actions, I was able to solve this problem rather quickly, and quite honestly, I’m very happy with the results.  The approach is that jQuery tabs can capture that the user clicks on the tab.  When this occurs the data is loaded into a MS MVC control using ajax (the user sees a loading progress indicator) and displayed in the View.  This takes advantage of NHibernate lazy load as well.  NHibernate will not query out to that object until it’s accessed. 

If I get a chance, I’ll include some code snippet and some pretty pictures…  Let me know if that would be helpful.


8 thoughts on “MS MVC, Spring.NET, NHibernate, jQuery and Ajax

  1. Pretty much everything 🙂

    I’m particularly interested in how you managed to ‘lazy-load’ your collections (or whatever you need) from a simple jQuery AJAX call.

    your help would be much appreciated, as I’ll have to do something similar soon.

  2. ok, the ajax query call is simply calling a controller in a restful way.

    ie. let’s say I want to grab all orders from a customer on a tab press and show the results:

    ActionResult LoadOrders(string id)

    var orders = OrderService.GetOrderByCustomer(id);
    return View(“OrdersViewControl”, orders);

    Behind this the OrderService is using OrderDao to make a NHibernate query to get all orders by CustomerID.

    With a lazy-load, you’d get two calls by default – assuming the mapping file is lazy and you have an orders collection off the customer:
    ActionResult LoadOrders(string id)

    var customer= CustomerService.GetCustomer(id);
    var orders = customer.Orders;
    return View(“OrdersViewControl”, orders);

  3. I see. But I thought that when you clicked on the tab, it would load the orders for the given customer using AJAX. However it seems you are returning a View (I guess it’s an other .aspx page). So am I wrong in thinking there is a postback occuring here ?

    By doing
    var customer= CustomerService.GetCustomer(id);

    you’re not exactly using lazy loading since you are retrieving again the Customer entity before lazy-loading the orders. I guess this is something you have to do anyway because the nhibernate session was closed, right ?

  4. Yes, all calls go back to the server. Ajax is just doing it asynchronously. That is fundamentally the only difference. Both are ‘gets’ or ‘posts’.

    All code is executed on the server and sent down to the client and displayed in html.

    When you do an ajax call – it is going to the server and getting the results and sending it back down.

    I’m using the ajax as a deferred way of only retrieving data when it’s used. ie. I could load all the tabs and their divs with the information in one call.

    Instead, I’m making a design decision to only load a tab’s content when requested.

    One isn’t better than the other really, depends on how much data, how often it’s used, etc…

    If you are familiar with webform update panels, I’m basically mimicking that – I’m making an ajax call and loading a view into a div – using mvc views and controllers vs. webform postbacks/callbacks with update panels.

    Your right on the second part, in that senario I’m not lazy loading it. If my orders collection is set to lazy, I would have 2 query calls there. One to get the customer and one to get the orders.

    Part of that is going to be another design time decision, do I want to eager load or fetch these orders every time I get a customer or not. ie. if in my business logic, 99% of the time that I get a customer I’m going to look at his orders, then I’d be better off to not lazy it.

  5. The problem with lazy loading a customer is that I don’t have any state – I’m in an state-less environment.

    It would certainly be nice if they clicked a tab and I could just say Customer.Orders

    But I don’t have a customer in state like I would in a windows app.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s