Living in the Tech Avalanche Generation

A practitioner’s introspective on technology

IronRuby and the Entity Framework - Part 1.

First of all let’s be absolutely clear - you get no Entity Model Designer, so we wont be building Entities in IronRuby. What we will be doing is building our Entity Model inside a C# library project and we will consume our model from IronRuby code. Admittedly I would expect support for the aforementioned design experience eventually but for now we can consume the Entity Framework in IronRuby in this way.

Some house keeping first! If you want to move quickly through this code and enjoy using Visual Studio then I suggest downloading the free Alpha version of RubyInSteel (for IronRuby only) here.

Given the following (ubiquitous) Northwind Entity Model (built in a C# library project).

ironruby_entity_model

with a ‘partial class’ augmentation on the ObjectContext as follows:

public partial class NorthwindEntities :
                     global::System.Data.Objects.ObjectContext
{
    public List<Customers> GetAllCustomers()
    {
        return this.Customers.ToList();
    }
}

We can write the following IronRuby code:

require ‘System.Windows.Forms, Version=2.0.0.0, ‘ +
    ‘Culture=neutral, PublicKeyToken=b77a5c561934e089′
require ‘D:/simon.segal/Local Working/Org.Techavalanche.IronEntities’ +
    ‘/Org.Techavalanche.EntityLibsForRuby/bin/Debug’ +
    ‘/Org.Techavalanche.EntityLibsForRuby.dll’
require ‘System.Data, Version=2.0.0.0, ‘ +
    ‘Culture=neutral, PublicKeyToken=b77a5c561934e089′
require ‘System.Data.DataSetExtensions, ‘ +
    ‘Version=3.5.0.0, Culture=neutral, ‘ +
    ‘PublicKeyToken=b77a5c561934e089′
require ‘System.Data.Entity, Version=3.5.0.0, ‘ +
    ‘Culture=neutral, PublicKeyToken=b77a5c561934e089′
require ‘System.Core, Version=3.5.0.0, ‘ +
    ‘Culture=neutral, PublicKeyToken=b77a5c561934e089′

providerName = “System.Data.SqlClient”;
serverName = “boomer”;
databaseName = “Northwind”;

#Initialize the connection string builder for the
#underlying provider.
sqlBuilder = System::Data::SqlClient::
                SqlConnectionStringBuilder.new

#Set the data source properties
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
sqlBuilder.IntegratedSecurity = true;

#//Build up the provider string part of the connection string
providerString =
    sqlBuilder.ToString();

#Initialize an EntityConnectionStringBuilder and set its provider name
entityBuilder = System::Data::EntityClient::
    EntityConnectionStringBuilder.new
entityBuilder.Provider = providerName;

#Sets the part of the connection string that is provider specific
entityBuilder.ProviderConnectionString = providerString;

#Points to the location of the model schema files
entityBuilder.Metadata =
    “res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl”

#get the entire connection string
con = entityBuilder.ToString()

#new up an ObjectContext from the C# library
session = Org::Techavalanche::
        EntityLibsForRuby::NorthwindEntities.new(con)

#ask for all the customers
custs = session.GetAllCustomers

#print out the customer company name
for cust in custs
    puts cust.CompanyName
end

Currently the implementation of IronRuby does not fully work with LINQ and trying to call the extension methods directly on an ObjectContext<T> yields errors. You can however build your own data access layer in another language such as C# or VB.Net and call into that library as demonstrated in this post. I have made the code available here and will continue to follow the development of IronRuby and how it fits into the scheme of things with Data Access as it progresses towards release.

Share/Save/Bookmark

2 Comments so far

  1. [...] time we looked at taking IronRuby to the Entity Framework ‘via the cape’ so to speak, by building our Entity Model in a C# library that we consumed from IronRuby. That [...]

  2. [...] you want from the entity framework. I showed this to some extent previously with IronRuby in these two posts. Perhaps with a bit of collaborative work the DLR and ADO.NET teams can do a mind meld and [...]

Leave a reply

Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia