DotNetNuke Linq to Sql Model Adapter

More details about this project may be found on my blog.

Description

A DataContext adapter allowing DotNetNuke modules using Linq to Sql to automatically apply an end user's database owner and object qualifier at runtime.

This project enables a commercial DotNetNuke module development path for those who wish to leverage Linq to Sql.

Goals

  • Run-time adjustment of an attributed MetaModel based on DotNetNuke database owner and object qualifier
  • Continued use of the Visual Studio Linq to Sql designer
  • No tedious changes in the compiler-generated code
  • Continued use of an attributed-meta model (and thereby no external schema deployment files)

Background

The .NET-supplied DataContext allows development using the attributed model automatically generated by the Visual Studio designer. However, when deployed within DotNetNuke, these attributes would need to be adjusted at runtime for whatever contextual object qualifier and database owner were in use by the end-user. Unfortunately, the MetaModel used by the Linq to Sql system is overwhelmingly read-only (and, frustratingly, the AttributedMetaModel subclasses are internal and sealed) and there was no simple remedy to this problem. Until now.

This solution utilizes a two-step approach. First, an AttributedMetaModel is generated based on the attributes automatically created through the designer. Next, the model is walked to generate an adapted model, which is cached and returned (models are thread-safe). Walking is reasonably inexpensive, and since the models are cached by type, performance is not affected.

Dependency injection is used to adapt attributed entities (database, table, and function names) into their adapted counterparts. A DotNetNukeAdapter is included for this particular domain. Additional adapters could easily be constructed for other domains, should any arise (to illustrate this point, a NonTransformingAdapter is included in the release, which performs no transformation).

Usage

  1. Download the latest release
  2. Add a reference to the adapter assembly
  3. Open your DBML designer file in Visual Studio
  4. Change the base class of the DataContext to BrandonHaynes.ModelAdapter.DotNetNukeDataContext
  5. Ensure that the adapter assembly is deployed to your DotNetNuke bin directory.

Important

Module developers must develop against a DotNetNuke installation with no object qualifier. This adapter does not store the design-time object qualifier for later removal. Any design-time database owner may be used; the design-time value is removed via a regular expression.

Feedback Appreciated!

Feedback about your experiences is needed, and greatly appreciated!

Last edited Sep 27, 2010 at 12:31 PM by BrandonHaynes, version 12