Few weeks ago, I release the version 1.3 of the LINQ to SharePoint DSL Extension. In this version, I introduced a new feature to generate automatically a repository layer pattern from a LINQ to SharePoint data model. You can find 3 tutorials about this feature in my previous post:

http://blogs.msdn.com/b/ocarpen/archive/2011/04/23/repository-pattern-with-linq-to-sharepoint-dsl-extension-part-1.aspx
http://blogs.msdn.com/b/ocarpen/archive/2011/04/24/repository-pattern-with-linq-to-sharepoint-dsl-extension-part-2.aspx
http://blogs.msdn.com/b/ocarpen/archive/2011/04/24/repository-pattern-with-linq-to-sharepoint-dsl-extension-part-3.aspx

This generator is only a T4 file (TT), and this file can generate automatically in Visual Studio some code. It means that it's really easy to implement your own generator in your projects. If you do that, you can generate anything that you want from a LINQ to SharePoint data model. Your model (l2sp) file could be the input of some code generation. For the next versions, I'm thinking about new TT items to add in the project, like for example a "Repository light pattern" (same pattern but without the helpers methods). If this post, I will try to explain you how to develop your own code generator from a LINQ to SharePoint data model. 

Please note that this tutorial is not about the basics of the T4 programmation, if you don't know this technology or if you need more information about it, you can read the MSDN section about Code Generation and T4 Text Templates.

 

How is it working ?

First, you need to understand that all the objects that you manage in your model (lists, content types, enumerations, mapped properties, etc.) are constructed as an object model. It means that you can load in objects an existing data model contained in a L2SP file. In a TT file in Visual Studio, it's really easy thanks to the "DSL directive processor".

For example, create a new LINQ to SharePoint data model in your project if you don't have it (read this tutorial if you need to know how to do that). Now add a new TT file in a Visual Studio 2010 project (right click on the project name, Add and Add new item) :

Click on the Add button.

A new T4 file is created with the following content:

<#@ template debug="false" hostspecific="false" language="C#" #>

<#@ output extension=".txt" #>

 

You can remark that this text template will generate a text file as output, and that this generated file is attached with your generator:

  

Now erase all the content of your tt file, and replace it by this new code:

<#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost
.ModelingTextTransformation" hostspecific="true" #>

<#@ output extension="cs" #>

<#@ Linq2SharePointDsl processor="Linq2SharePointDslDirectiveProcessor" requires="fileName='YourModel.l2sp'" #>

 

Change the value of the "filename=" attribute by the relative path from this file to the l2sp schema that you want to use. Be sure that the path to this file is good, and save your TT file. If the generation is succedded, now the text file has been replaced by a C# file:

Now you are ready to generate what you want from this model. Indeed, the line containing the reference to the L2SP file automatically loaded the content to tranform it in objects in the current context

In fact, the collection is already loaded in your TT context, and you can browse the model elements through the "this.Linq2spModel.Elements" property.

 

A simple sample

For example, if you want to write in your CS file all the lists contained in a LINQ to SharePoint Data Model, you can write the following code: 

<#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" hostspecific="true" #>
<#@ output extension="cs" #>
<#@ Linq2SharePointDsl processor="Linq2SharePointDslDirectiveProcessor" requires="fileName=YourModel.l2sp'" #>

//This text is generated from a custom TT file
//This text contains all the list of the l2sp input file:
<#
foreach (BaseMappingElement element in this.Linq2spModel.Elements)
{
    if (element is List)
    {
       List list = element as List;
       string listName = list.Name;
       string sharePointListName = list.MappedListName;
#>
//Detected list entity called '<#=listName#>' mapped to the SharePoint list '<#=sharePointListName#>'
<#
    }
}
#>

 

For my model, the result of this template file is:

 

To conclude, it's easy to implement some generators from an input LINQ to SharePoint model. This tip can help you to generate anything you want from elements contained in a schema. A complete class documentation about the object model of the L2SP content will be available online soon.

If you want to write some useful generators and share it with the community, please send it to me, I could be interested to redistribute it by default with the extension.