Blog - Microsoft .NET, ASP.NET, AJAX and more

Ruby OData Update v0.0.7

by Damien White 7/6/2010 8:48:49 PM

OData_logo_MS_small I  just wanted to give an update on the ruby_odata progress.  If you don’t know about ruby_odata or want a refresher, I blogged about it a few weeks ago.  Anyway, I just released a new version of the gem, v0.0.7.  With this new version, you can now perform batch saves (multiple adds, deletes, and updates) and types are being supported.  The library is getting more mature with each revision.  Below is the state of the current gem:

  • <Collection> dynamic methods are available after you instantiate the service.  You can chain query operations like order_by, filter, expand, etc. on to the dynamic methods.
     
  • For querying, $orderby, $top, $skip, $filter, and $expand are supported.  These are represented by the methods order_by, top, skip, filter, and expand (respectively) on the OData::QueryBuilder class (which is returned from the dynamic collection methods), for example: svc.Products.top(10).
     
  • The ability to add entities.  AddTo<Collection> dynamic methods are available after you instantiate the service, allowing you to pass new entities to the methods.
     
  • The ability to update entities though the update_object method on the OData::Service.
     
  • The ability to delete entities though the delete_object method on the OData::Service.
     
  • The ability to batch saves, which allows you to pass multiple objects to be added, updated, or deleted and have them persisted as a batch when you call the save_changes method on the OData::Service.  Note that with batch saves, the changes either complete or fail as a batch.
     
  • Explicit data type support for the following: (Everything outside of this list currently is represented as a string.)
    • Edm.Int16
    • Edm.Int32
    • Edm.Int64
    • Edm.Decimal
    • Edm.DateTime – Note that DateTime is a bit odd.  There’s no time zone data passed from OData services (at least not with WCF Data Services), so it is assumed that the time zone is UTC for all DateTime types.
       
  • Support for Complex Types – From OData.org: “Complex Types are structured types also consisting of a list of properties but with no key, and thus can only exist as a property of a containing entity or as a temporary value.”  You’ll typically see these to logically group properties together.  For example, with the Netflix service, they expose a BoxArt complex type on a Title.  The BoxArt type consists of 3 properties, SmallUrl, MediumUrl, LargeUrl.  This makes accessing these clearer and cleaner, for example movie.BoxArt.MediumUrl.
     
  • Works with Ruby 1.8.7 and Ruby 1.9.1.  It also works with ActiveSupport 3.0.0.beta4.  These were tested on Windows due to using WCF Data Services for the test suite, although, if something Ruby works in Windows there is a good chance everything should be fine on Mac or Linux as well.  I could always change the test URLs to point at a remote machine to do my testing on a Mac, but I wish there was an OData producer like WCF Data Services that would work with ActiveRecord (or similar).
Shout it kick it on DotNetKicks.com Bookmark and Share

Introducing a Ruby OData Client Library

by Damien White 6/12/2010 9:37:28 PM

OData_logo_MS_small Ever since the ADO.NET team started development on Astoria (pre-release), I have loved the concept.  Since its release (it was called ADO.NET Data Services and is now WCF Data Services), I’ve used it a ton (you may remember the example from this post).  Back at MIX10, Microsoft announced a commitment to the Open Data Protocol (OData).  WCF Data Services enables you to create services that use OData to expose and consume data, both with .NET 4 and .NET 3.5 SP1.  OData services are very powerful and there are quite a few live producers such as Netflix and Nerd Dinner.

OData is so powerful because it’s REST based and you can access the services from just about everywhere, including just a simple URL.  For example, using the Netflix OData service to access the best movie ever made by title: http://odata.netflix.com/Catalog/Titles?$filter=Name eq 'Office Space'.  Pretty simple.  Of course accessing things solely based on URLs in code isn’t the best, and hence the reason for my post.

Client Libraries

When using OData, it’s convenient to utilize an SDK to access the services.  For example, in Silverlight you can access OData services using LINQ.  There’s also a fantastic AJAX Library (which I used in a previous post).  There are SDKs for PHP, Objective-C, and many others, but there was one missing that I wanted to use… Ruby.  I am assuming this comes as no surprise given my new found love for Ruby.   More...

Shout it kick it on DotNetKicks.com Bookmark and Share

Behavior Driven Development (BDD) with Cucumber and ASP.NET MVC

by Damien White 6/10/2010 6:18:40 PM

cucumber_logo So as I’m sure you have guessed by my last bunch of posts, I’ve been hooked on Ruby and Rails development.  The more I use it, the more I love it.  I felt I was loving it a bit too much, so decided to divert my attention back to ASP.NET MVC for a while.  While developing with Rails, I was practicing BDD with Cucumber.   I felt that Cucumber scenarios (written Gherkin) were far more useful for the majority of my testing on a web application.  Sure unit tests are worthwhile for bits of code, but I’ve found that they don’t always make sense when testing a web application (like Steve Sanderson has mentioned).  Some may argue that these tests are brittle, but honestly, I feel it defines my application better and it definitely tests the actual interactions better.  I’m fully on board for unit tests for complex processes, but the majority of web work just isn’t that complex.

I’m not going to go fully into what BDD is here, but my goal was to use a similar process when developing ASP.NET MVC applications as I did in Rails.  With Rails, it’s simple, Rails, Cucumber, Pickle, ActiveRecord, Factory Girl, Webrat, etc. work perfectly together.   In the .NET world, there are options, but nothing that really fit together as nicely.  I started with just wanting to test my basic application (menus, breadcrumbs, etc) and didn’t venture into model work yet, so I figured it would be as simple and efficient as it is in Rails.  Not so much.  I tweeted about this exact thing a while ago.  More...

Shout it kick it on DotNetKicks.com Bookmark and Share

Ruby Beauty – Rendering a Rails Partial for a Collection

by Damien White 4/27/2010 10:48:16 PM

Continuing with my exploration of Ruby on Rails as a .NET guy, here’s an awesome example of Convention over Configuration at work.  I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails.  If you are interested in more, see my other Ruby posts.

Today’s post illustrates how elegantly the render partial helper method in Rails works.  Assuming you’ve done some MVC development, you should be very familiar with a partial view.  Partials are used to keep your view nice and clean.  When things start getting complex within a view, you may want to think about adding a partial. 

Take for example a library or bookstore application.  One of the models in the application is, of course, a book.  The books are displayed in a list as well as individually on a details page.  During the course of development, you notice that you have quite a bit of repeated code between the index and show pages.  This is a good opportunity to use a partial to DRY up your code.  You take the repeated book view code (which I won’t post here since it doesn’t matter what it is), and put it into a partial (_book.html.erb).  Within the partial we are referencing a local object simply called book

Single Partial

On your detail page (a single book), it’s a simple replacement like so:

<%= render :partial => "book", :object => @book %> 

But even better, since our partial is named the same as our model, we can shorten up the syntax:

<%= render :partial => @book %> 

Now that’s some convention goodness right there.  

More...

Shout it kick it on DotNetKicks.com Bookmark and Share

Ruby Beauty – Rails and Time Zones

by Damien White 4/21/2010 7:52:05 PM

Continuing with my exploration of Ruby on Rails as a .NET guy, here’s an awesome example of Convention over Configuration at work.  I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails.  If you are interested in others, see my other Ruby posts.

Time zone handling has always been one of those things that I’ve tried to avoid.  A recent ASP.NET app I was working on needed to support time zones and it was quite a pain to implement.  Well, within the context of Rails, I was astonished at how easy it was to handle time zones. 

By default, ActiveRecord stores datetimes in the database as UTC (although you can change this behavior, however, I’m not sure where this would actually be helpful).  This makes it very easy to convert a datetime to another time zone.

Conversion Magic

At the application level, you can set a default time zone.  Within your environment.rb file (sort of like an ASP.NET web.config file), you can just simply set:

config.time_zone = "Eastern Time (US & Canada)"

Now, when you display a date within a view it will be converted to EST –5:00 (or EDT –4:00 like it is now).  Also, when a user enters a date, it will be entered as Eastern Time, and it will be converted and saved to the DB as UTC. 

More...

Shout it kick it on DotNetKicks.com Bookmark and Share