Archive for the ‘Blog’ Category

Adding an alias to an action parameter for model binding in ASP.NET MVC

This is the introduction of a new Nuget package called ActionParameterAlias.

Have you every had a URL:

http://example.com?productid=123456

where you wanted to bind it with an ASP.NET MVC action like this?

   1:  public ActionResult Search(Product product)
   2:  {
   3:      //...
   4:      return View(product);
   5:  }

 

Chris Missal was discussing this for a project here at Headspring, so we worked together and came up with an action filter that enables the use of aliases.  More specifically, the following allows the same action to accept url with a query string parameter matching it or any of the aliases.

   1:  [ParameterAlias("Product", "pid", Order = 1)]
   2:  [ParameterAlias("Product", "p_id", Order = 2)]
   3:  [ParameterAlias("Product", "productid", Order = 3)]
   4:  public ActionResult Search(Product product)
   5:  {
   6:      //...
   7:      return View(product);
   8:  }

Just head on over to Nuget and go grab it.  This is a feature that I’d like to see make it into the next version of the framework.  It’s so tiny of a feature, but so many folks have parameter names that are just a bit off because there is no way to add aliases currently.

CropperCapture[1]

To check out the code, clone it from BitBucket here: https://bitbucket.org/jeffreypalermo/action-parameter-alias or on the Nuget gallery at http://nuget.org/packages/ActionParameterAlias/4.0.0.

New docking station for Samsung Series 7 Windows tablet

Yesterday, the docking station for the Samsung Series 7 tablet came in the mail.  I hooked it up to the computer and set it up as the computer running our 50” plasma TV in the main room from which we do our stand-up meetings in the morning.&#160…

Installing Windows 8 on the Samsung Series 7 tablet

Earlier this week, Headspring received shipment of some Samsung Series 7 tablet computers from the Microsoft Store.  Kurt Schindler volunteered to installed Windows 8 on the computer, so he made a rather large USB drive bootable with the Windows…

Could the app be the solution to the failed software project problem?

In Fred Brooks’ book, The Mythical Man-Month, Mr. Brooks provides several essays that share his experience in the software industry going back to 1959.  One of the common themes found through several essays is that the size of the project i…

New Windows tablet PC unboxed

Here at Headspring, I purchased some of the Samsung Series 7 Slate PC’s from the online Microsoft store.  The computer comes with Windows 7, which is touch-enabled, and uses a stylus as well as your finger.  It’s only single touch, though…

Challenging non-local session scope (session-per-request)

I posted the following on the nhusers mailing list, and Steve Bohlen was nice enough to weigh in.  I was hoping to get broader feedback on this, so I’m posting it here. 

======================================================

First, some context.  I’ve been an NHibernate user since version 0.8 on .Net 1.1.  I can sling HBM mappings like nobody’s business.  Over time, I’ve used NHibernate on no less than 26 projects.  Rolling custom user types? no problem.

I’ve used it as my choice ORM all along even while keeping up with what new tools arise.  I was part of the group dubbed "The NHibernate Mafia" in this infamous podcast on .Net Rocks.  I was one of the vocal people in the room at an early Entity Framework design review at the MVP Summit of 2007 that sparked the Alt.Net movement, and I’ve spoken on NHibernate at many conferences including Microsoft Tech Ed.

I want to challenge a presumed best practice with NHibernate.  I’m challenging it honestly given my experience using it and helping many clients use it as well.

On the official NHibernate community website, nhforge.org, there is an article on session-per-request.  I have used this technique for most of the years I’ve used NHibernate.  I have been able to use it effectively.  But, I couldn’t get over the fact that all of our clients have problems with this and end up in situations where lazy-load queries happen at all layers of the application.

My problem, then, is that this pattern, while technically sound and useful, introduces complexity that requires a deeper understanding of NHibernate to manage.  Because of this, we have begun disposing of the session after each and every transaction.  See here:

public Employee GetByUserName(string userName)
{
    using (ISession session = DataContext.GetSession())
    {
        IQuery query =
            session.CreateQuery(
            "from Employee emp left join fetch emp.Roles where emp.UserName = :username");
        query.SetParameter("username", userName);
        var match = query.UniqueResult<Employee>();
        return match;
    }
}

Here, I get the employee, dispose of the session, and pass the object back.  For each variation of loading, we have a different method that eager fetches the right level of association or collection.  This way, the application has the data it needs, and there aren’t any lazy-load exceptions.

This approach simplified the code in a few ways:

  • Each query to the database is known, and there is a method for each
  • All queries are executed while the call stack is in the data layer (no UI-level lazy loading)
  • It forces the developer to think about each data scenario

In coaching and training other developers (we actually teach NHibernate in our Agile Boot Camp training class), we have come up with a simple message for how to think about NHibernate and other ORMs.

  1. When moving from stored procedures to an ORM, don’t forget what was good about sprocs (you had a list of every query that would be run against your database)
  2. Use an ORM for just Object to Relational Mapping.  That is the core strength.  The other features, like lazy-loading, can be useful, but they also add complexity
  3. Keep all your relationships and collections mapped as lazy, but don’t allow them to lazily-load
  4. Dispose of a session immediately after using it.
  5. Keep the NHibernate reference out of the your core library and behind data access interfaces.

I’ll pause here because there is nothing wrong with session-per-request technically.  I successfully used it on many projects.  The problem is that it is complicated.  I understand it just fine, but I’m also an NHibernate expert.  Lots of other developers just want to use it and be done with it.  They don’t want surprises.  Elevating the session past local scope up to a global variable brings unintended consequences.  When developing, I might dereference another collection on an object, and NHibernate happily runs another select statement against my database.  As that developer, I don’t have profiler perpetually open to see this happen.  There is absolutely no signal that I just put something undesirable in my application.  Developers need fast feedback on what they are doing.  If there was no session hanging around, the lazy-load invocation would throw and excepting right then.  The developer would then go back and modify the query or decide that we need a new one.  We have used session-per-transaction on two projects now, and it hasn’t led to any other difficulties, so I view it very favorably.

A "best practice" should generally lead a developer to a desirable result.  I think that here, the best practice needs to lead to simplicity, even while the "advanced" practice may still be session-per-request.

I have not drawn a final conclusion, but given the bad effects I’ve seen from this, wouldn’t it be a simpler (better) practice to just dispose of the session immediately and create a few more explicit repository methods for different loading levels? 

I appreciate any discussion that arises from this.  The best place would be on the NHibernate Users mailing list.

Steiner Ranch fire update 9/5/2011

 For anyone who is concerned about our well-being:  First, thank you for your concern.  Our neighborhood, Steiner Ranch, is completely evacuated, and 30 houses has been lost so far to the large fire in the neighborhood.  Latest offi…

How to access controller methods from a view in ASP.NET MVC

This question has been posed in several places.  One of the places is on StackOverflow here.  The use case here is that my controller has important state or an important function, and I’d like to leverage it from the view.  It’s not …

I am speaking at the Austin Cloud User Group on May 24, 2011 (Tuesday)

Topic: How Headspring Does Business in the Cloud: A Technical Perspective If you’d like to come, RSVP here: http://www.eventbrite.com/event/1707145117 The meet is held at the offices of Pervasive Software. AGENDA (from website): 6:30-8:00 Jeffrey…

Growing a professional services company–my experience, critical drivers, metrics, and business intelligence

In September of 2007, I joined Headspring Systems as the CTO.  Now I serve as the Chief Operating Officer (COO).  When I joined, there were 4 other employees including the original found serving as CEO.  He founded the company in 2001.&#…