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);
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).
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.