Saturday, September 10, 2011

Generics For Dummys (re:NHibernate)

I am working on a rogue project that is not being paid for by the company for which I work. This is building something that just helps me do my job, so I have to be very quick. I am using NHibernate, and wanted to get listable data without writing even the smallest functions of my own. So I created this (not for production use):

   public IList GetAll()
        {                     
            return Session.GetSession()
            .CreateCriteria(typeof(T))
            .List();
         }


Because NHibernate uses generics, this function can act as a pass-through, requesting the List method be run, but not much more. My ASP .NET webforms application has to give it the table name of interest.

Again, just a fun way to rapidly get a "table viewer" application running but also providing some code value for potential full life cycle development later.

* Re-reading years later, I think I meant
 GetAll(T) 
as the Type would have to be passed to the function.

No comments: