Linq to SQL + mapping files ~= ObjectSpaces

Most of the samples that have been around on Linq to SQL (formerly called DLinq) use CLR types that are annotated using attributes, such Column and Association. The System.Data.DLinq.DataContext class extracts the metadata during runtime. With this information it knows how to build SQL statements from the expression trees created by C# 3.0 statements.

Linq to SQL also supports the use of a mapping file, where all metadata is condensed into a single XML file. A MappingSource object delivers the same information to the DataContext as the attributes do.Let me first explain how this works.

I used SqlMetal.exe to generate source code and create a mapping file by running SqlMetal.exe from the directory C:Program FilesLINQ PreviewBin.

SqlMetal.exe /server:. /database:KillerBlog /code:BlogsDomain.cs /map:BlogsMapping.xml  /namespace:KillerApps.Blogs.Domain /pluralize

Both files are added to a Class Library project and the BlogsMapping.xml file gets a build action of Embedded Resource. This should all compile. Next you can get at the objects in methods like so:

public static Blog GetBlog(Guid blogID)
  Assembly assembly = Assembly.GetExecutingAssembly();
  Stream stream = assembly.GetManifestResourceStream(“KillerApps.Blogs.Domain.BlogsMapping.xml”);
  XmlMappingSource mappingSource = XmlMappingSource.FromStream(stream);
  BlogDataContext context = new BlogDataContext(Properties.Settings.Default.development_KillerBlog_dbo, mappingSource);
  Blog blog = context.Blogs.FirstOrDefault<Blog>(b => b.BlogID == blogID);
  return blog;

This method will materialize a Blog object from the backing datastore.

When you take a longer look at how this works, the mechanism is not that different from the way ObjectSpaces used to work. In fact, most of it is the same. The only important difference is that the OPath expressions have been superseded by the C# 3.0 expressions. I suspect that newer build of Linq to SQL will have another designer besides the Class Designer, that might just look a lot like the one that was in Visual Studio 2005 Beta 1 (up until the May 2005 CTP).

Will this be anything like the designer for mapping relational schemas to CLR objects?

Who knows? Tell us, then.

This reminds me of the term POCO: Plain Old CLR Objects, once introduced by Andrew Conrad as a variation to POJO (where J is for Java). He used it to indicate that the classes used in the mapping have no special requirements, such as a base class or attribute decoration. This is the case with the mapping version of Linq to SQL as well.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s