December 31, 2012

Using PetaPoco with SQL Server Stored Procedures

For quite a while now I've been using PetaPoco for almost all of my data access needs in .Net, and it's been a great experience. For those who may not be familiar with PetaPoco, it is a light weight micro object relational mapper which helps you work with a database and map the results to your own POCO objects.

The big advantage of PetaPoco and other micro ORM's is that they are very simple compared to other "heavier" products like NHibernate or EntityFramework. Of course there is nothing wrong with NHibernate or EntityFramework, however, there is a much bigger learning curve with these technologies and many things you can do with them you can also accomplish much easier with a micro ORM. Some other micro ORM's that I have had success with are Simple.Data and Service Stack ORM Lite.

PetaPoco works great and the examples are excellent when you are working with SQL that you generate in your C# classes. However when you start to work with stored procedures it can get a little tricky figuring out the exact syntax that PetaPoco requires. Therefore I put together a few GISTS to demonstrate how to call SQL Server stored procedures from PetaPoco:

Using PetaPoco to call a SQL Server stored procedure and return 1 result to your poco object:

Using PetaPoco to call a SQL Server stored procedure and return a list of strings:

Using PetaPoco and calling a SQL Server stored procedure that returns a value with return statements in TSQL:

April 12, 2012

Inject Cool Stuff Into an HTML Head Tag with ASP.Net HttpModule

Have you ever wanted to inject some dynamic text into all of the ASP.NET Web Form pages in your site whether or not they use a master page?

 This sample project shows how to do just that: inject some dynamic text into all of the web page head tags in an ASP.NET Web Forms project.

It will work for pages that use master pages to generate their header and for pages that do not.

To accomplish this we create an ASP.Net HttpModule which takes advantage of the ASP.NET pipeline and runs our code to create the custom header information responding to the correct page life-cycle event.

More details to come...

April 9, 2012

Conditional Builder Design Pattern

Have you ever wanted to use the Builder design pattern to create a really cool fluent interface for building objects but you wanted to add conditions to the building? The good news is that you can do something like so:

Notice how in this contrived example a ninja only gets the "hide in shadows" skill after reaching 6th level? To handle a conditional builder take a look at the following code:

For a complete sample project check it out on my GitHub account!