Generated code - WCF Data Services support

LLBLGen Pro Runtime Framework supports the WCF Data Services shipped by Microsoft. The support is for .NET 4, similar to WCF Ria Services support. LLBLGen Pro Runtime Framework comes with a full Data Service Provider implementation which makes it real easy to create your own WCF Data services based on a model in Adapter or SelfServicing code.

The WCF Data Services is designed to be usable with existing Visual Studio.NET tooling. The following information describes how to define a WCF Data Services for generated code in Adapter or SelfServicing. The example below will add a service for 'Northwind', using Adapter, however you can use the same code for SelfServicing.

Creating a WCF Data Services Tutorial

using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using SD.LLBLGen.Pro.ODataSupportClasses;
using RootNamespace.Linq;

namespace MyNamespace
{
	public class NorthwindService : LLBLGenProODataServiceBase<LinqMetaData>
	{
		// This method is called only once to initialize service-wide policies.
		public static void InitializeService(DataServiceConfiguration config)
		{
			// TODO: set rules to indicate which entity sets and service operations 
			// are visible, updatable, etc.
			// Examples:
			// config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
			// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
			config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
		}
	}
}

protected override LinqMetaData CreateLinqMetaDataInstance()
{
	// adapter
	return new LinqMetaData(new DataAccessAdapter());
	// selfservicing:
	// return new LinqMetaData();
}

protected override ITransactionController CreateTransactionControllerInstance()
{
	// adapter
	return new DataAccessAdapter();
	// selfservicing:
	//return new Transaction(System.Data.IsolationLevel.ReadCommitted, "Trans");
}

protected override IUnitOfWorkCore CreateUnitOfWorkInstance()
{
	// adapter
	return new UnitOfWork2();
	// selfservicing
	// return new UnitOfWork();
}
	

protected override string ContainerName
{
	get { return "NorthwindService"; }
}

The easiest way to consume the service in a .NET application is to add a Service Reference (in VS.NET) of the service you just created. This way VS.NET will generate classes for you, one for each entity exposed by the service and a context class to work with the entity classes on the client.

Using JSONP with the WCF Data Service

If you want / have to use JSONP with the data service, e.g. because a control used on the client requires JSONP support in the WCF Data Service, please follow the following steps:

After this, the service is capable of handling $callback and $format elements properly and is able to handle and return JSon.

Limitations

We did our best to support WCF Data Services in full to make sure everything in your LLBLGen Pro model/generated code is exposable through the service. However as it turned out, WCF Data Services in its current version (.NET 4 framework) has limitations and these limitations can make it a problem to expose the generated code through a service, even though the generated code works OK when used in normal .NET code. These limitations are described below.

Most of them will result in an error either when you obtain the $metadata or when you fetch data. If you run into one of these limitations, it's best to create a copy of the LLBLGen Pro project file and remove the entities/fields/types which cause a problem and re-generate the code, and use that in the service. If that's not doable, please look at WCF Ria Services instead. This other framework is also supported by LLBLGen Pro and has less limitations (but also less features in some areas).

No enum / DateTime2 etc. support

As WCF Data Services builds an entity data model (EDM), the types are based on the EDM type system. This means that it can't deal with fields which have an Enum type. The LLBLGenProODataServiceBase class filters out fields with Enum types and all other types which aren't mappable to an EDM type. These fields don't show up in the entity exposed by the service. The types which are supported are listed in the list of DataTypes on this page: http://msdn.microsoft.com/en-us/library/dd723653.aspx. Our provider doesn't use the Reflection provider, but the same list of types applies.

No byte[] type support for PK fields.

A field in the primary key of an entity isn't allowed to be of type byte[]. This is a limitation of WCF Data Services, and a left-over from Entity Framework, which has the same limitation.

No navigators supported in subtypes.

This is a limitation of WCF Data Services in .NET 4. A future version of WCF Data Services will support this, and when this is released we'll look into lifting the filters we added to our code to filter out navigators on subtypes. This limitation means that if you have inheritance in your entity model, any subtype can't have a relationship with another entity, all relationships have to be defined on the root of the hierarchy. This is quite a limiting factor for many people. If you run into this, consider WCF Ria Services instead (or a normal WCF service).

Many to Many relationships are single sided and read-only

In LLBLGen Pro, many to many relationships are always read-only and this won't change in a WCF Data Service. To update many-to-many relationships, you have to append entities over the intermediate entity and the 1:n/m:1 relationships instead, like you'd do normally when using LLBLGen Pro code.

Paging support uses Skip/Take and have to be in page size blocks.

Paging is done through Skip/Take methods in Linq, and as LLBLGen Pro has paging per page, the .Skip(n).Take(m) combination has a limitation: n has to be 0 or more multiplications of m. So .Skip(9).Take(3) works, but .Skip(9).Take(4) doesn't, as the skip value is used to calculate how many pages to skip.

Acknowledgements

We'd like to thank Brian Chance for his work in this area. His templates (which used the RelfectionProvider) to bring WCF Data Services support to LLBLGen Pro were a great source of information during the development of the data source provider.


LLBLGen Pro v3.5 documentation. ©2012 Solutions Design