Will Dapper for retrieval and NHibernate for CRUD work for a web application

asp.net-mvc-3 dapper design nhibernate

Question

I am planning to use "Dapper" for retrieval and "NHibernate" for CRUD operations. So, would it be a good design to follow this approach. One of the problems I recently faced is with CRUD Screens.

Suppose I have Edit Order form. I am retrieving the entity (order) from Dapper and while updating it I need to attach these objects to NHibernate session to perform CRUD operations. It is not directly what is required, I mean object.delete().

Could anyone provide suggestions on this design and is there a possibility to make it better. It is web application developed using asp.net mvc 3.


Questions on the reply:

  1. Does session filter on the action mean what we are using for current operation. If so, for the GET operation, should it be [DapperSession] instead of [NHSession]?

    [NHSession] <<--------[DapperSession] GET ACTION RESULT

    • DapperSession.Get.Entity(1000)
    • Return view
  2. I am still trying to understand PRG pattern you've posted, I'll post if I have any doubts.

  3. Since all this happening for the "EDIT" operation, would be wise to just to get the object using NHibernate too. This get rids of all this process with cost of little overhead.

Accepted Answer

At first glance this seems like a logical choice use Dapper (or another other micro ORM's) for GET and NHibernate for any POST action methods on controllers. See pseudo-code):-

[DapperSession]
GET ACTION RESULT
  - DapperSession.Get.Entity(1000)
  - Return view

and for the post method

[NHSession, DapperSession]
POST ACTION RESULT
  If Model.State is valid
    - entity = NHSession.Get.Entity(1000)
    - update entity
    - redirect to ...
  end if

 - DapperSession.Get.Entity(1000)
 - return view

As you can see this is not that elegant, as you may have to different action filters to your POST action result. To tidy this up you could utilise the Post Redirect Get (PRG) pattern so GET controllers use Dapper and POST controllers only have access to NHSessions.

See this accepted answer on SO for details on how to set up this pattern..

Now your GET and POST action results would look like this:-

[DapperSession, ImportModelStateFromTempData]
GET ACTION RESULT
  - DapperSession.Get.Entity(1000)
  - Return view

and for the post method

[NHSession, ExportModelStateToTempData]
POST ACTION RESULT
  If Model.State is valid
    - entity = NHSession.Get.Entity(1000)
    - update entity
    - redirect to ...
  end if

 - return redirect to GET

However as you can see all I am really doing is substituting one action filter attribute for another one. BUT (and this is a big but in my opinion) you do get the benefits of utilising the PRG pattern! To be honest this probably doesn't answer our question directly but it is a good method to follow.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why