Type forwarding

Type forwarding

Suppose I have developed a wonderful library. It contains some base types and extended functionalities on top of the base types. I publish the library and people are happily using it. Life is wonderful.

Now I am up to develop the v2 version of the library.I discovered that my implementation fo the extended functionality isn’t the best of the world and I have a way to improve it. As I move along, I realize that something isn’t right. I have applications using v1 of my library, and applications using v2 of my library. When the two kinds of applications want to exchange the base types I defined in my library, it receives a InvalidTypeCast exception because now the base types now live in different assemblies!

I can force both applications to use v2 version of my library by using a publisher policy assembly. But my new implementation is likely to break existing applications that uses v1 of my library.

I guess I have to leave the library as it is forever, and add the new implementation to a new assembly. But it sucks that I have to keep the inferior implementation forever.

I so wish that I have shipped two assemblies originally. One only contains the base types, and the other one contains the extended functionalities. This way I can freely change base types between applications, while innovate in the functionality space.

Well, in .Net framework 2.0, you can use so called “Type forwarding” feature, to break the assembly in two pieces, and still maintain the backward compatibility.

In .Net framework 2.0, a custom attribute TypeForwardedTo is introduced to achieve this. In our example, you can move the implementation of the extended functionalities to a different assembly, and rebuild the original assembly with the extended functionalities forwarded to the new assembly. The re-built assembly contains the definition of extended functionalities but it does not contain the real implementation. At certain point in the future, when everyone has moved to the new implementaion, you can safely deprecate the old implementation.