Geeks With Blogs

News Meta
Gruff Code Code and tech musings from Jesse Taber. If I call it a blog I'll feel bad when I don't update it every week.

I’ve been spending some time lately digging into SubSonic 3 and have really enjoyed working with it so far. I love the how “low friction” it is to get up and running. I’ve been particularly impressed with the SimpleRepository in this regard. It definitely lives up to it’s name by providing truly simple data access functionality in a pretty sane and straightforward way. That said I don’t think it’s the best choice for every project, but if you don’t have to care much about the implementation details of your database then I can see it being a really useful tool.

For whatever reason I’ve been choosing a “blog engine” as my domain of choice lately when working up sample code (I guess I finally got tired of endlessly building ‘employee management’ or ‘product inventory’ models) and decided to see how I might be able to leverage SimpleRepository in a scenario like this.

I won’t go too far into the mechanics of using SimpleRepository (great walkthroughs are available at the SubSonic project site) but the basic idea is that you create POCO classes to define the data that you want to be able to persist. For the purposes of this example I started with a very simple ‘Post’ entity that ended up looking like this:

    public class Post
        public int PostID { get; set; }

        public string Title { get; set; }

        public string Body { get; set; }

        public string AuthorName { get; set; }

        public DateTime PublishedOn { get; set; }

        public DateTime CreatedOn { get; set; }

SubSonic can take an object like this and automatically create a corresponding table in SQL Server on-the-fly. Taking a “convention over configuration” philosophy, SubSonic sees an int field on the ‘Post’ class called ‘PostID’ and makes that both the primary key and identity on the corresponding ‘Post’ table. When creating a new post it’ll also help you out by bringing back the newly created ‘PostID’ from the database. Creating a new post might look something like this (assuming a valid connection string present in the config file named ‘SampleDB’):

   1:          public int CreatePost(string title, string body, string authorName, DateTime publishDate)
   2:          {
   3:              Post newPost = new Post
   4:              {
   5:                  Title = title,
   6:                  Body = body,
   7:                  AuthorName = authorName,
   8:                  PublishedOn = publishDate,
   9:                  CreatedOn = DateTime.Now
  10:              };
  12:              SimpleRepository repo = new SimpleRepository("SampleDB", SimpleRepositoryOptions.RunMigrations);
  13:              int newPostID = (int)repo.Add<Post>(newPost);
  14:              return newPostID;
  15:          }

The ‘Add<T>’ method of the SimpleRepository returns an ‘object’ which I presumed would be an integer containing the new PostID. I wanted to be able to cast that to a local variable and then return the new ID to the caller of this method. I was somewhat surprised to see that this code blows up on line 13 with an ‘InvalidCastException’. Apparently the object being returned isn’t directly cast-able to an integer. Making a quick change to the code, setting a breakpoint, and digging in some with the ‘Immediate’ window revealed the following:


The ‘Add<T>’ method was returning a decimal instead of an int. The ‘newPost.PostID’ and ‘returnValue’ were both being set to 7 (which was the correct value after looking in the database) but they were typed differently. Now, if I were smart I’d just resign myself to simply using the ‘PostID’ to determine the new identity of the created Post, but where’s the fun in that? ;-)

I decided instead to dig down into SimpleRepository source code and take a closer look at  the ‘Add<T>’ method definition. More on those findings in the next post…

Posted on Sunday, November 15, 2009 6:47 PM | Back to top

Comments on this post: Why does SubSonic’s SimpleRepository ‘Add<T>’ return a decimal instead of an int? (Part 1)

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Jesse Taber | Powered by: