PetaPoco – Why I’m Using A Micro-ORM

As I mentioned a few posts ago, in the last couple of years I moved away from writing native SQL to a full blown ORM and back again. Well not all the way back. Here is where PetaPoco comes in.

I had been using my own SQL to Object mapper for a while before I stumbled over PetaPoco, and it had very similar ideas to mine. It has gone one step further though, with nice support for paging, inserts, updates, deletes and multiple database support.

Since forking the repository at Github, I have implemented and suggested many changes that have made it back (thanks to Brad) into the main repository. Some of these include support for Oracle (including sequences for PK), DB field to Object field name conventions (and the ability to customize these per project).

So why have I come the full circle on this.

  1. Well firstly, SQL is a great query language for RDMS databases. It is very very powerful and I don’t think that it is all that difficult to learn.
  2. Just give me my data. I just want to create a sql query and map it to my object so I can use it. I don’t want to have explicit mapping files/code.
  3. The pain is just not worth it IMO. Why should I have to spend excess time trying to figure out if this query is going to produce a select n+1 or that I have to traverse 4 deep to get a piece of data?
  4. One file. Its not 1 DLL or 10 DLLs. Its one cs file that you just drop in to your project. It doesn’t get much simpler than that.
  5. Performance. I just don’t have to worry that I’m losing any performance between the database and my object. If there are performance issues, its either the query or the business logic.

And those are just some of the reasons.

To see how to use the basic features of PetaPoco, please visit the PetaPoco home page.

At the moment my fork contains a few extra features that are not currently in Brad’s repository. These include multiple primary key support for legacy database, concurrency using a version column, an OnCommandExecuted extension point and a IDatabase interface. I also have the PetaPoco.cs file set to “No Dynamic” at the moment as I’m using it with .NET 3.5. To enable the dynamic object mapping support, comment out the follow line (currently line 11).

#define PETAPOCO_NO_DYNAMIC

In future blog posts I intend to go through a few examples of how I have using it and how the extension points enable you to do some pretty interesting things.

Now I know this won’t be for everyone but unless forced, I won’t be going back to a full blown ORM.

Adam

8 Responses to PetaPoco – Why I’m Using A Micro-ORM

  1. Cesar says:

    I appreciate the fork and the bullet points. I was looking for how to handle composite primary keys, and I’m glad that you implemented something like that.

  2. scope_creep says:

    Whats the attribute markup for multiple primary key support. Tried various combo’s but no luck.

  3. Schotime says:

    It will only work on my branch and all you do is seperate the primary keys by comma. eg. [PrimaryKey("Id,Name")]

  4. Vlad says:

    hi, you have mentioned version column.
    how do you setup/use this?

    if you have an example or something it would be appreciated…

  5. Abrian says:

    I have a table CustomerContact for composite primary key CustomerId, ContactId. When saving a row I get the error: The object doesn’t have a property matching the primary key column name CustomerId,ContactId.
    Any help will be appreciated!

  6. Schotime says:

    It seems like you are not using my branch.

  7. tcmaster says:

    I like petapoco, but it’s codebase is 2000+ lines long and it’s really the time to bring it to a full project. I really don’t mind add an extra dll, but i do mind reading 2000 line single file.

  8. Varun Maggo says:

    Hello PetaPoco experts,

    After googling for 4 hours, i came back to this article again, read your couple of blog articles on PetaPoco.

    I am using PetaPoco as a data access for an online penny auction site with MVC3 as front end.

    We had implemented a project with entity framework, but not quite happy with its performance.

    We are certain that the kind of performance we are looking for can be achieved with Peta Poco. Also it can be a full blown ORM in the near future as it already has good support for helpers, Extensions, one to many and many to many support etc.(Personally it helps a developer save some time using existing helpers, extensions, multi table mapper,)

    I have made up mind on using PetaPoco, just wanted to know from your end about the pattern i could follow and best practices to be followed during the development phase.

    Thanks
    Varun Maggo

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>