Tag archive for 'linq-to-sql'

23
Jul

LINQ to SQL T4 template reloaded

The topic of modifying the code generation phase of LINQ to SQL comes up quite often and the limited T4 template I published here last month was good at showing the potential but wasn’t a practical replacement for the code generation phase.

I am please to make available the next version, which now…

  • Runs from the DBML therefore keeping the designer in the loop
  • Generate all the attributes for columns and tables including UpdateCheck, IsDbGenerated etc.
  • Supports associations including those with a foreign key
  • Generates appropriate attributes and code for both serialization modes

In short it generates code that is now functionally equivalent to SQL Metal with the following limitations:

  • C# only – VB.NET can be added if there is some interest
  • Stored procedures – not yet supported

Download LINQ-to-SQL-T4-0.3.zip (5 KB)

To use the template :

  • Extract the archive and add the two files to your project
  • Right-click on the L2ST4.tt file, choose Properties and set the Custom Tool to blank
  • Rename DataClasses1.tt to the same name as your DBML file (but keeping the .tt extension) and open it
  • Click save and watch a freshly generate C# DataContext pop out
  • Switch off the LINQ to SQL designer generated CSharp by either setting the Custom Tool on the DBML file to blank or setting the Build Action on the generated CS to None.

L2ST4.tt contains a lightweight wrapper around the DBML which is processed using LINQ to XML which makes the template easier to work with and provides a central point if you wish to change the naming rules etc.

This code should be treated as a sample and hasn’t received much testing yet so feel free to leave comments or feedback here.

Some places you could take this template:

  • Generate an interface for your data context to improve mocking
  • Alternative naming and defaults
  • Splitting output into multiple files
  • New languages
[)amien
25
Jun

Experimental LINQ to SQL template

Whilst SqlMetal does a good job of turning your SQL schema into a set of classes for you it doesn't let you customize the code generation process.

Usefully there is now a templating system built into Visual Studio 2008 called Text Templates (T4 for short).

Here is a short (369 line) experimental proof-of-concept T4 template I wrote last night that will generate a data context and associated entity classes as a starting point similar to that produced by SqlMetal.

Download LINQ to SQL template for T4 v0.1 (ZIP) (3 KB)

Once downloaded unzip and drop the DataContext.cs.tt into your project and edit line 17 to set the connection string. You can also edit lines 18 and 19 to set the namespace and class name. The lightweight wrappers around database, table and column can be found at the end of the file - they simply wrap the SQL Server Information_Schema views as briefly as possible.

Within seconds Visual Studio should have created a code-behind file for the DataContext named DataContext.cs.cs with your generated code ready to use :) If you don't like the way the template generates your context you can change it :)

There are limitations with this experimental proof-of-concept including:

  • Processes all and only tables in the database (no views or SP's)
  • Foreign-key relationships are not implemented
  • Column attributes for IsDbGenerated, UpdateCheck and AutoSync not implemented
  • C# only (sorry Julie)
  • Plural and singular naming rules are incomplete
  • Can't modify schema as you could with a designer stage
To learn more about T4:

[)amien

01
Apr

Joining the LINQ to SQL team at Microsoft

I've been quiet on my blog lately largely because I have been preparing to change job and relocate half-way around the world to Vancouver in the beautiful province of British Columbia (where I spent my 2004 summer holiday).

In February I travelled out to Redmond for three days of interviews (one position grew to two, then three). Having read the Microsoft Jobs Blog I was prepared for long hard days but in reality the process was incredibly enjoyable and exciting.

So much so I wanted to find a desk and move in right then.

With some luck I also found myself at Hanselman's geek dinner which involved some great discussions and the chance to meet Scott himself, Brad Wilson and Nikhil Kothari who I knew from .NET on-line community as well as some 35 other developers from both within Microsoft and the outside world. It was one fun evening and my thanks go to Scott for kindly driving me back to my hotel in Redmond town centre.

Many white-boards and a few lunches later (including an unexpected one with Phil Haack, Nikhil and two more guys from ASP.NET team - I wish I could remember all the names of the people I met!) I found myself with the hard task of choosing a position.

I settled on a developer role within the LINQ to SQL team starting mid-May and am counting down the days...

[)amien

29
Jan

Language Integrated Query: An introduction presentation online

This evening's presentation on Language Integrated Query (LINQ) is now available from my presentations page.

The event went well with 12 developers (it's a small island) and took a less time to produce because I didn't write a script for but slides to lead us through areas I wanted to cover.

This means the on-line presentation is of less useful than previous ones however I am pondering the transcript/audio-track option (in my best radio voice of course).

We are now considering topics for February and with something a bit special for March.

Stay tuned!

[)amien

05
Jan

LINQ presentation at Guernsey Developer Forum

I will be giving a talk at the Guernsey Software Developer Forum at the end of the month on Microsoft's new Language Integrated Query (LINQ) with particular emphasis on the capabilities and object-relational mapping characteristics of LINQ to SQL.

Now confirmed for:

  • Date & time Tuesday 29th January, 6pm
  • Location Guernsey Training Agency above Smith Street Post Office
  • Open to Everybody

This presentation is now available on-line.

[)amien

03
Sep

LINQ in 60 seconds

Microsoft's Language INtegrated Query (LINQ) aims to provide a way of selecting objects with a common syntax independent of the data source.

By integrating query into the language instead strings parsed by an external provider at runtime we gain IntelliSense prompting for fields, members and table names and full compile-time syntax checking and a unified syntax.

They will be included in the .NET Framework 3.5 and delivered as part of the Visual Studio 2008 'Orcas' release which is currently available in beta and comprises of:

LINQ syntax

The language extensions themselves. An example in C# might look like:

GridView1.DataSource = from country in countries
                       where country.Continent == 'Europe'
                       orderby population descending
                       select country;

LINQ to Objects

A core part of the .NET Framework 3.5 and allows you to query against any IEnumerable<T> collection and test or sort against any of T's properties.

In the above example imagine Country is a business class and Countries is a List<Country>. Continent is a string property and population a numeric one.

LINQ to SQL (formerly known as DLinq)

LINQ to SQL works by mapping classes and properties to tables and fields as any normal Object-Relational Mapping (ORM) tool would.

It achieves this by marking the classes and properties with attributes to indicate how they map to the underlying database. A visual modeling tool is provided that can generate and manipulate such classes for you from an existing SQL database.

The ORM functionality includes tracking of changed objects and change persistence. It also ensures that you will not obtain multiple objects for the same underlying row in the database (Fowler's Identity Map).

Other components

There are many other components in the LINQ family including:

LINQ to XML (formerly known as XLinq)

Similar to LINQ to SQL but along the idea of querying XML documents using LINQ syntax rather than the XPath/XQuery syntax.

LINQ to Entities (ADO.NET Entities)

A more advanced ORM solution that allows more extensive mapping and manipulation between how the object appears and the underlying data source.

Third-party support

Mono is actively implementing LINQ as are the people behind the NHibernate ORM.

[)amien




Feed subscription

Blog topics