PetaPoco - What's new in v4.0

Wednesday, June 1st, 2011     #petapoco #everything

PetaPoco v4 is almost ready. Here's a write up on what's new and changed.

This post is about PetaPoco - "a tiny ORMish thing for your POCOs". Read more on the PetaPoco Project Page.

Replaced the Transaction property with a method

I knew that making PetaPoco's Transaction property a property was a mistake when I did it, but somehow it got through. The big problem with this is the debugger will attempt to evaluate it and inadvertently start new transactions - oops.

The Transaction property has been marked as obsolete in the github master branch for a while now, but if your just using the NuGet packages you'll find this property has gone missing and has been replaced with the GetTransaction() method.

You'll need to change:

using (var scope = db.Transaction)

to this:

using (var scope = db.GetTransaction())

For all the changes in the release, this tiny change is the reason for clocking the major version to 4 - since it breaks the API and I'm complying with semantic versioning.

Multi-Poco Queries

The major new feature in this version is support for Multi-Poco queries. I've written about this before (see here).

The functionality hasn't really changed except I've removed the <T1, T2, T3, T4, T5, TReturn> generic overloads. I've done this because .NET 3.5 only supports up to 4 argument parameters. If you're using .NET 4.0 and need the extra arguments it should be trivial to write these as extension methods (though I haven't actually tried to confirm this works).

Alternatively you can call MultiPocoQuery directly:

IEnumerable<TRet> MultiPocoQuery<TRet>(Type[] types, object cb, string sql, params object[] args)

This method accepts the POCO types as an array - rather than as generic arguments.

Support for IDbParameters as SQL arguments

PetaPoco now supports directly passing IDbParameter objects to a query. This is handy if PetaPoco doesn't correctly map a property.

For example the SQL Server driver doesn't handle assigning DbNull to a VarBinary column unless the parameter is configured with the correct type. To work around this you can now do this:

databaseQuery.Execute("insert into temp1 (t) values (@0)", 
                new SqlParameter() { SqlDbType = SqlDbType.VarBinary, Value = DbNull.Value });

One interesting side effect of this is that you can also return an IDbParameter from the PetaPoco.IMapper interface to globally override PetaPoco's default parameter mapping functionality.

(Thanks to Adam for this idea)

Ability to disable auto-select generation on per-query basis

PetaPoco does a reasonable job of guessing when it should automatically insert SELECT clauses - but it's not perfect and there are various statements that it doesn't work correctly with. Previously to work around this you had to turn off the EnableAutoSelect property, run your query and then set it back again (all the while handling exceptions).

Now, you can escape any SQL with a leading semicolon to indicate that select clauses should not be inserted. PetaPoco removes the semicolon before passing the query to the DB.


// Leading semicolon in query will be removed...
db.Query<mytype>(";WITH R as....");

T4 Template Improvement - Ability to plugin custom clean up functions

You can now replace the standard T4 template method that's used to cleanup table and column names. In your T4 template, before the call to LoadTables just set the global CleanUp property to a delegate:

CleanUp = (x) => { return MyCleanUpFunction(x); }

T4 Template Improvement - Ability to include views and filter by schema

The T4 template can now generate classes for either all schemas in the DB, or just a single schema. To only include tables in a particular schema, set the global SchemaName property before calling LoadTables. You might also like to qualify the generated classes with a class prefix:

SchemaName = "MySchema";
ClassPrefix = "myschema_";

If you want the primary schema and a specific other schema, or multiple schemas, just have multiple files with a different SchemaName setting in each.

You can now also get the T4 template to generate classes for views (in addition to tables):

IncludeViews = true;

These changes are pretty experimental, because I haven't had time to test on all the different database engines... if you find problems, please try to find a fix an let me know.

Improved multi-threaded support with ReaderWriterLockSlim

PetaPoco now uses ReaderWriterLockSlim for protecting access to shared data which should improve multi-threaded performance - such as in web apps.

Support for protected constructors and properties

PetaPoco can now access a POCO's private and protected members - this includes private constructors and property accessors.

New Page<>.Context Property

I've found a number of cases where I've been using the PetaPoco Page object as the strongly typed model object for MVC views and needed to pass one extra piece of data. Rather than use ViewData or declaring a new strong type just for that view, I've added a Context property to the Page object. Its defined as type Object and can be used to pass a little extra context to the view.

For example, in Showcase Lane I have a partial view that displays a page of site preview thumbnails. This works fine when I have a page of sites to show, but when the list is empty I want a context sensitive "blank slate" message to be displayed. This could be "You have no favourites", or "No More Sites", or "You haven't liked any sites", etc...

To handle this, in the controller I set the Context property to something that indicates the type of blank slate message to display if there's no data.

Bug Fixes

  • Unsigned integral types in MySQL now generated correctly
  • Fixed T4 template when a generated column property name conflicts with the generated class name
  • T4 template support for SQL Server Time type
  • Fixed support for SQL Server Geometry and Geography query parameters


All this is available in the github master branch now. NuGet package coming soon.

« Older - PetaPoco - Mapping One-to-Many and Many-to-One Relationships Newer - Startups and .NET »