Blog

Data Access and Silverlight 4

December 20, 2010 by Damien White

You’ve decided to create a new business application (meaning some n-tier application) using Silverlight as the frontend. Is Silverlight the best choice? That’s a debate for another post… Anyway, now it comes to deciding on a data access strategy, which one do you choose? Well this is one of those things where there is no clear winner. To make a choice, Shawn Wildermuth has a great post on the “State of Data Access in Silverlight 4.” Looking at his awesome chart comparing plain WCF, WCF Data Services, and WCF RIA Services, you’ll see that some of the things on the “Cons” side for all of the approaches could be a deal breaker. So which one should you choose? Let me take you through a deliberation that I was involved in recently when forced to make this decision. There is no right answer here (as with most interesting topics), but I would be interested in your feelings on the topic. Let’s get started…

Initial Breakdown

Given the three strategies that Shawn had identified, we had pretty much focused on just WCF Data Services and WCF RIA Services. Plain old WCF services are useful for a lot of things, but they aren’t very flexible on their own. Things like client-side shaping, sorting, filtering or paging are rough, plus I don’t see the value in writing boilerplate service code for CRUD operations. I suppose you could use something like a T4 template for that, but for “standard” web services the thing I really don’t like is SOAP. I won’t wax poetic about how REST makes so much more sense than SOAP, but if you are interested, you should check out RESTful .NET: Build and Consume RESTful Web Services with .NET 3.5 by Jon Flanders, it’s a fantastic book. Sure, you could have a standard WCF Service using REST instead of SOAP, but I don’t believe that you can generate a proxy against a WCF REST service (I don’t mind WebClient/HttpWebRequest code). That isn’t a deal breaker, but it is when writing a lot of code against a lot of objects. We decided that standard WCF just wasn’t the easiest option for the lion’s share of the work.

So that leaves us with WCF Data Services or WCF RIA Services. For a previous project we had discounted using WCF RIA Services for the majority of the data access (though, we did use it for user roles and such). Trying to go into a new project with an open mind, we thought we should dig a little deeper into RIA, after all, it “seems” to be the approach that you should use for a business application as you’ll see.

Read More »

Ruby OData Update v0.0.7

July 06, 2010 by Damien White

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:

  • 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** 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).

Introducing a Ruby OData Client Library

June 12, 2010 by Damien White

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.

Read More »

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

June 10, 2010 by Damien White

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. Read More »

Ruby Beauty – Rendering a Rails Partial for a Collection

April 27, 2010 by Damien White

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.   Read More »

Ruby Beauty – Rails and Time Zones

April 21, 2010 by Damien White

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.

Read More »

Ruby Beauty – Method Naming Conventions

April 12, 2010 by Damien White

Continuing with my exploration of Ruby as a .NET guy, here’s another example of Ruby’s expressive syntax in action. 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.

Naming conventions can be a good thing providing there is actually some rhyme and reason to them. We’ve seen a lot of changes with conventions, like Hungarian Notation for example, who’s still using that? Within the context of .NET, there are defined conventions by Microsoft to which I would say I mostly follow (I still usually prefix private global member variables with an underscore). The guidelines for .NET are really good, much better than something very arbitrary like Hungarian Notation where no two developers use the same syntax on things.

There are two method naming conventions in Ruby that I really like. Within Ruby a method name can end in either a question mark (?) or an exclamation point (!). Having these conventions makes for nicer syntax. Let’s look at them in a little more depth. Read More »

ActiveSupport Singularize Problem

April 09, 2010 by Damien White

I just happened to encounter a problem with the ActiveSupport inflector in my Rails app. Here’s the problem: words that end in ess seem to be an issue. For example:

'dress'.singularize # => 'dres'
'business'.singularize # => 'busines'
'address'.singularize # => 'addres'

D’oh, where did that ending ‘s’ go?

Turns out that “Rails has a longstanding policy of not taking further inflector patches. Use an initializer in your application instead,” a quote from Mike Gunderloy.   What exactly does this mean? Simple…

Within your Rails application, open the config/initializers/inflections.rb file and just add the following:

ActiveSupport::Inflector.inflections do |inflect|
    inflect.singular(/ess$/i, 'ess')
end

All better…

'dress'.singularize # => 'dress'
'business'.singularize # => 'business'
'address'.singularize # => 'address'

Ruby Beauty – Optional Parentheses on Method Calls

April 08, 2010 by Damien White

Continuing with my exploration of Ruby as a .NET guy, here’s another example of Ruby’s expressive syntax in action. 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.

Optional parentheses on method calls are such simple things, but they render some pretty code. Ruby allows you to omit parentheses on method calls in a lot of cases. Take the following code:

puts "Hello World"
puts("Hello World")

The two lines are equivalent, yet the first is just cleaner, don’t you think? Being a diehard C# guy, I do like parentheses, curly braces, and semi-colons, but I’ve got to say Ruby does yield some beautiful code.

In a recent Rails app I used Ryan Bates’ CanCan gem for authorization. It’s a really nice gem and allows you to define abilities for a user elegantly. Using CanCan, you can see how nice and clean the code looks. For example:

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.admin?
      can :manage, :all
    else
      can :read, :all
    end
  end
end

The code flows nicely; what can an admin do? “can manage all.”

If we were required to add parentheses it’s just is a bunch of extra, unneeded noise, and it breaks up the flow:

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.admin?()
      can(:manage, :all)
    else
      can(:read, :all)
    end
  end
end

user.admin?()<gag> right?() :)

Ruby Beauty – Dynamically Invoking Methods

April 07, 2010 by Damien White

Continuing with my exploration of Ruby as a .NET guy, here’s another example of Ruby’s expressive syntax in action. 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.

Within the context of Ruby, you can call a method in one of two ways. The first, and most frequently used, is the standard “dot” notation. Take the following class:

class SampleClass
    def say_hello(name)
        puts "Hello #{name}"
    end
end

To then call that method:

obj = SampleClass.new
obj.say_hello('Damien')

This works fine for things you know about, but what if you wanted to call a method dynamically at runtime? Read More »