SELECT DISTINCT with NHibernate

I found a blog post on Stackoverflow about how to do the equivalent of SQL’s SELECT DISTINCT in nHibernate, but I can’t seem to find it again so here is what I got from it. Thanks to whoever it was on that site!

To return a list of the distinct combination of two columns (MyColumn1 and MyColumn2) from a table MyTable do the following within your custom repository (I’m assuming that you’ve already created the MyTable class, IMyTable interface and appropriate class map of course!:

public IList<IMyTable> GetAllDistinctCombinations()
{
            NHibernate.ICriteria criteria = Session.CreateCriteria(typeof(IMyTable));

            criteria.SetProjection(
                    Projections.Distinct(Projections.ProjectionList()
                        .Add(Projections.Alias(Projections.Property("MyColumn1"), "MyColumn1"))
                        .Add(Projections.Alias(Projections.Property("MyColumn2"), "MyColumn2"))
                           ));

            criteria.SetResultTransformer(
                    new NHibernate.Transform.AliasToBeanResultTransformer(typeof(MyTable)));



            return criteria.List<IMyTable>();
}

Note that although this returns a list of MyTable objects, only the properties that correspond to MyColumn1 and MyColumn2 will be populated on each object.

Advertisements

Automating JavaScript Testing with QUnit

I haven’t had a time to look at this yet, but keen to get some more testing goodness and order into my JS/JQuery.

Automating JavaScript Testing with QUnit

Generating PDFs and returning them from a controller method in MVC

The PDFSharp library for .Net is an excellent and relatively easy way of generating PDFs, but how to return them from a controller method in MVC?

The answer is pretty straightforward – use the File() helper, code example returning after a post action shown below.

Generate the PDF in a service class that implements the below interface, using PDFSharp, and return the PDF as a MemoryStream (it’s easy to create the stream using PDFSharp). Note that the stream has to be closed before returning.


public interface IMyPDFService
{
        MemoryStream GeneratePDF();
}

Within controller:

private IMyPDFService _pdfService;

[HttpPost]
public ActionResult ReturnPDF(MyViewModel thisView)
{
       MemoryStream reportStream = _pdfService.GeneratePDF();
       return File( reportStream, "application/pdf") ;
            
}

This was just a brief post, as I didn’t have much time. Instructions on how to create PDFs using PDFSharp are available at their website, but I will add in a brief example function if anyone is interested.

Error Handling with Elmah

I’ve been looking at error handling lately and come across ELMAH.

So far so good – it is easy to configure and has a variety of options for storing errors.

It also allows for enterprise wide reporting whereby different applications use the same error store.

Ace!

You can also use Elmah for error  signalling so that you can log errors that arise not from unhandled exceptions but from managed circumstances (such as a web service time out or some such).

Finally you can also get ELMAH to play nice with MVC’s HandleError attribute, ¬†so that it logs all controller errors (which would be bypassed if decorated with [HandleError] as it only deals automatically with UNhandled exceptions. Details of how to do this are contained in this StackOverflow post (incidentally apparently SO use a fork of ELMAH n’all!)

(ADDED: This was useful for deployment to IIS 7)

ADDED by Andrew: Also, the applicationName attribute of the errorLog tag is very useful when you have more than one application logging to the same database, e.g.:

<errorLog type="Elmah.SqlErrorLog, Elmah" applicationName="My App Name"
  connectionString="..." providerName="System.Data.SqlClient" />