Using the Jedi Mind Trick in .NET

Category: ASP.NET
Tags: .net
Last Modified: May 2 2017
Feb 11 2014

In the original Star Wars movie there is a classic scene where Obi-Wan and Luke enter the Tatooine Space-port.

Storm trooper: Let me see your identification.
Obi-Wan: [with a small wave of his hand] You don't need to see his identification.
Storm trooper: We don't need to see his identification.
Obi-Wan: These aren't the droids you're looking for.
Storm trooper: These aren't the droids we're looking for.
Obi-Wan: He can go about his business.
Storm trooper: You can go about your business.
Obi-Wan: Move along.
Storm trooper: Move along... move along.

Obi-Wan uses the “Jedi Mind Trick” on the Storm trooper to “suggest” what he should do.

Sometimes we need to do the same thing in .NET.

Consider the scenario where you have developed a class – MyClass - in a Library that you distribute - MyLibrary.  You want to reorganize your libraries and create a new – MyNewLibrary - and move MyClass to the new asembly.  The problem is that all your existing applications expect MyClass to be in MyLibrary not MyNewLibrary.

In .NET there is an assembly level attribute  – TypeForwardedTo(typeof(MyClass)). This can be applied to the “old” version of the Library, and your applications will not break.

The procedure is as follows:

  1. Move the MyClass code from MyLibrary to MyNewLibrary .
  2. Put TypeForwardedTo(typeof(MyClass)). in MyLibrary for the MyClass type.
  3. Compile the newly created MyNewLibrary .
  4. Add a reference of MyNewLibrary into MyLibrary .
  5. Recompile MyLibrary (because MyClass used to be located in that).

When your application attempts to get a reference to MyClass from MyLibrary  – it will be redirected to the correct MyNewLibrary.

Or as Obi-Wan would say - “This is not the assembly you’re looking for.”

I am currently investigating whether this could be an approach to the “Breaking Changes” issue, as we move forward with DNN development

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Tags