.NET 3.5 and LINQ

I had a little spare time last week and finally dug into .NET 3.5, mostly extension methods, lamda expressions, and LINQ. Pronounced LINK, it is the embedded query language in .NET 3.5. I am not going to outline what it does or how it works, there’s plenty of that on the internet, but I will continue my tradition to outline some funny annoyances :)

The nicest think IMHO about LINQ is the SQL bridge, which essentially implements a full OR-mapping framework like Hibernate in the .NET framework. Let’s dive into an example and map the following class to a database table:

[Table(Name = "c_schema")]
public
class LinqSchema
{
private long id;
private string uuid;
private string name;
[Column]
private long id_creator = 1;
[Column]
private long id_owner = 1;
[Column]
private int version = 1;
[Column]
private bool is_public = true;
[Column(Name = "id", IsPrimaryKey = true)]
public long Id
{
get { return id; }
set { id = value; }
}
[Column(Name = "uuid")]
public string Uuid
{
get { return uuid; }
set { uuid = value; }
}
[Column(Name = "name")]
public string Name
{
get { return name; }
set { name = value; }
}
}

So far so good, that looks almost like an annotated Hibernate mapping in Java. Now let’s read some existing data out of the database:

string
connectionString = @”…”;

DataContext db = new DataContext(connectionString);
Table
<LinqSchema> schemata = db.GetTable<LinqSchema>();

// nice little LINQ expression
var
query1 = from ss in schemata where ss.Name == “minimal” select ss;

// iterate of the query result
foreach
(var qq in query1)

Console.WriteLine(string.Format(“Schema {0}”, qq.Uuid));

That’s pretty self-explanatory. Now the funny caveats. Initially, one of the columns was an integer type, and in .NET I put in a long. One would think integers can get casted into longs, yet the error I got was this:

Error Snaphot

I was quite stunned that the SqlDependency extensions for query caching which were introduced in SQL Server 2005 and .NET 2.0 didn’t work with LINQ at all. There are a few hacks out on the internet, but I couldn’t get them to work reliably.

I found association mappings to be somewhat inelegant, especially compared to Hibernate which doesn’t require you to redundantly map the IDs and the referenced objects (1-1 mapping shown in this example):

// column maps the id
[Column(Name="id_schema")]
public
long id_schema;

// entity reference holds the reference object
private
EntityRef<LinqSchema> _Schema;

// the actual association. Storage refers to the entity reference,
// ThisKey to the variable containing the id.
[Association(Storage = "_Schema", ThisKey = "id_schema", OtherKey = "Id")]
public
LinqSchema Schema
{
get
{ return _Schema.Entity;}
set
{ _Schema.Entity = value; }
}

All in all, however, a big step in the right direction. On the downside, .NET now has multiple competing options for remoting and persistence, and interoperability between all these options is not necessarily given.

 

Comments

Leave a Reply




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

-->